c语言一维数组怎么快速排列
274
2022-08-30
HDU - 5438 (拓扑排序+dfs)
Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value vv. Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode. Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
Input
The first line of input will contain a number T(1≤T≤30)T(1≤T≤30) which is the number of test cases. For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104)p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105)m(1≤m≤105) which represents the number of pipes. The next line contains pp numbers v1,...,vpv1,...,vp, where vi(1≤vi≤108)vi(1≤vi≤108) indicating the value of pond ii. Each of the last mm lines contain two numbers aa and bb, which indicates that pond aaand pond bb are connected by a pipe.
Output
For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
Sample Input
1 7 7 1 2 3 4 5 6 7 1 4 1 5 4 5 2 3 2 6 3 6 2 7
Sample Output
21
题目大意:
给出n个点,每个点都有一个价值w,给出m条边,要求先把度数小于2的点删除,然后计算联通块的点为奇数的价值和。
思路:
首先删除点可以用拓扑排序,标记一下。
然后找联同块可以用dfs跑一边。求和。
代码:
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~