|
|
4 3 2 1 2 2 3 2 4 3 4 1 4 correct answer is 0. http://e-maxx.ru/algo/connected_componentsYou need to output number of components minus 1. Tests: 4 4 0 3 4 1 2 3 1 2 4 0 1 0 0 0 6 0 0 5 6 5 1 1 2 1 4 2 5 3 6 4 5 1 2 1 6 5 0 1 2 1 4 2 5 3 6 4 5 1 6 3 4 1 2 2 3 4 5 2 6 4 0 1 3 3 4 4 6 5 6 1 Edited by author 04.11.2020 21:20 I hope these tests will be useful for you :) 4 5 0 3 4 1 2 3 1 2 4 and 7 7 0 1 2 1 3 2 4 2 5 3 6 3 7 4 7 The output must be 0 in both cases, but if you've done something wrong, it can be -1. Make sure your determination of connected components isn't wrong. you gave the wrong 1 test here's the correct 4 4 0 3 4 1 2 3 1 2 4 answer : 0 You can use Disjoint-set data structure. Too complicated for such an easy problem Please, help. I can't help you without details of your algorithm. I have never encountered WA#5. I have some Stack-Overflow's because of critical mistake in my find_set function for Disjoint Set Union. After fixing AC. Edited by author 06.07.2017 09:50 You have to give us more information Solution is Minimum spanning tree (Kruskal or Prim Algorithm). Put low cost on tunnels, and much higher one on bridges with using priority queue ;) The problem is much easier. You don't need to use Kruskal or Prim to solve it. Well, yes, but we can use this problem to train skills of writing MST algo =) You are trying to kill a fly with a nuclear Bomb :) Edited by author 30.10.2019 13:44 Used bfs instead dfs It does not matter. Both DFS and BFS can be used to solve the problem. Yet, DFS code is shorter and nicer that BFS code. So, DFS is a better choice. So, I used DFS instead of BFS Edited by author 30.10.2019 13:41 Edited by author 30.10.2019 13:41 try this test: 1 0 0 answer: 0 I think counting total amount of graph components and then printing it-1 faster than to do it by MST.We can do it in O(n+k) time and we needn't know anything about bridges. Edited by author 11.07.2011 23:20 No need to read information about bridges! Just count number of connected components in graph and write (number-1). I have right answer on the test on task page. I have right answers on all testcases that i found in this forum. There is a stupid error in my code. Where? [here was a code] answer: forgot initialize all parametres in constructor. AC now :) Edited by author 23.04.2011 20:00 You can consider that the cost of the tunnel is 1, and the bridge is 24000. Then it becomes a MST problem. would be better to make weight tunnel 0, the weight of the bridge 1, and the answer is weight of minimal spanning tree Your idea is interesting, but weird a bit. :) It can be solved with DFS easily. Moreover, you even don't need to know anything about bridges. Lets graph G has n vertices and k components => number of m edges meet the conditions: n-k <= m <= 1/2(n-k)(n-k+1). Therefor k >= n-m. So, min(k) = n-m. We have number of islands = N, and number of tunnels K. Why the answer isn't N-K-1 ? Edited by author 11.02.2007 22:13 Try this test 4 5 0 1 2 1 3 1 4 2 3 3 4 Answer 0, your answer 4 - 5 - 1 = 2 or this 5 4 1 1 2 3 4 4 5 3 5 2 3 Answer 1, your answer 5 - 4 - 1 = 0 Why for the second test answer is 1? There is only one component, so you shouldn't to add any edges. And one more thing, if n < k answer is 0. So what's wrang? In your case, 'wrang' is wrong :) 5 4 1 1 2 3 4 4 5 3 5 2 3 Count of tunnels is 4. So graph with edges 1 2, 3 4, 4 5, 3 5 have 2 components. You should use exactly one bridge (edge 2 3) to make it connected. Read problem statement more carefully. Task is to delete maximal number of "bridges" in given graph. Ok! Thank. I'll try to find another way to solve it. I had done it:) Thanks to all for help!!! Information about bridges is extra, isn't it? It isn't necessary to read it, as graph tonnels+bridges is connected. It's need to count the number of components of the graph and the answer is number-1. Is this all true??? Because using this algo I always have WA 14 :( So do I! Please, someone tell me what's the test 14? Edited by author 04.05.2006 22:49 I haven't overcome this problem yet :(. AlexF, do you think that our idea is wrong? Yes!!! I got AC!! Our idea is right! )) I found the test which my program coudn't pass. Here it is: 9 3 6 6 8 5 8 2 1 1 3 2 3 3 4 7 4 9 4 8 9 Right answer is 5. If You want, KAV, you can give me your mail and I'll send to you my solution. ;) Edited by author 05.05.2006 17:21 Edited by author 05.05.2006 18:10 Edited by author 05.05.2006 18:10 |
|
|