hdu 5438 Ponds dfs

Time Limit: 1500/1000 MS (Java/Others)    

Memory Limit: 131072/131072 K (Java/Others)

Problem Description

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 v.

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) 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) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) indicating the value of pond i.

Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b 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

Source

2015 ACM/ICPC Asia Regional Changchun Online

一个图,无向边,每一个节点有一个权值

若某一个节点的相连的边长数<=1,则这个节点可以去掉

主人公会继续去掉节点直到所有节点都不可以去掉为止

现在剩下的节点是一个个的连通分量,

主人公想知道所有所在连通分量的节点数为奇数的节点的权值总和

简单题

记录每一个节点的入度

1.第1次dfs,去掉可以去的节点

2.第2次dfs,找出哪些连通分量的节点数为奇数,并把这些节点的总权值加入到ret

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4
  5 using namespace std;
  6
  7 #define ll long long
  8 #define push_back pb
  9
 10 const int maxp=1e4+5;
 11 const int maxm=1e5+5;
 12
 13 struct Edge
 14 {
 15     int to,next;
 16 };
 17 Edge edge[maxm<<1];
 18
 19 int head[maxp];
 20 int tot;
 21 bool vis[maxp];
 22 int in[maxp];
 23 ll v[maxp];
 24
 25 void addedge(int u,int v)
 26 {
 27     edge[tot].to=v;
 28     edge[tot].next=head[u];
 29     head[u]=tot++;
 30 }
 31
 32 ll solve(int ,int );
 33 void dfs(int );
 34 void dfs1(int ,int &,ll &);
 35
 36 int main()
 37 {
 38     int test;
 39     scanf("%d",&test);
 40     while(test--){
 41         tot=0;
 42         memset(head,-1,sizeof head);
 43         memset(in,0,sizeof in);
 44
 45         int p,m;
 46         scanf("%d %d",&p,&m);
 47         for(int i=1;i<=p;i++)
 48             scanf("%I64d",&v[i]);
 49         int u,v;
 50         for(int i=0;i<m;i++){
 51             scanf("%d %d",&u,&v);
 52             addedge(u,v);
 53             addedge(v,u);
 54             in[u]++;
 55             in[v]++;
 56         }
 57
 58         printf("%I64d\n",solve(p,m));
 59     }
 60     return 0;
 61 }
 62
 63 ll solve(int p,int m)
 64 {
 65     memset(vis,false,sizeof vis);
 66
 67     for(int i=1;i<=p;i++){
 68         if(!vis[i]&&in[i]<2){
 69             in[i]--;
 70             dfs(i);
 71         }
 72     }
 73
 74     ll ret=0;
 75     memset(vis,false,sizeof vis);
 76     int num;
 77     ll sum;
 78     for(int i=1;i<=p;i++){
 79         if(!vis[i]&&in[i]>1){
 80             num=0;
 81             sum=0;
 82             dfs1(i,num,sum);
 83             if(num%2)
 84                 ret+=sum;
 85         }
 86     }
 87     return ret;
 88 }
 89
 90 void dfs(int u)
 91 {
 92     vis[u]=true;
 93     for(int i=head[u];~i;i=edge[i].next){
 94         int v=edge[i].to;
 95         if(vis[v])
 96             continue;
 97         in[v]--;
 98         if(in[v]<2){
 99             in[v]--;
100             dfs(v);
101         }
102     }
103 }
104
105 void dfs1(int u,int &num,ll &sum)
106 {
107     vis[u]=true;
108     num++;
109     sum+=v[u];
110     for(int i=head[u];~i;i=edge[i].next){
111         int v=edge[i].to;
112         if(vis[v]||in[v]<2)
113             continue;
114         dfs1(v,num,sum);
115     }
116 }

时间: 2024-08-07 04:32:17

hdu 5438 Ponds dfs的相关文章

HDU 5438 Ponds dfs模拟

2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque&

hdu 5438 Ponds(长春网络赛 拓扑+bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2237    Accepted Submission(s): 707 Problem Description Betty owns a lot of ponds, som

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

HDU 5438 Ponds (DFS,并查集)

题意:给定一个图,然后让你把边数为1的结点删除,然后求连通块结点数为奇的权值和. 析:这个题要注意,如果删除一些结点后,又形成了新的边数为1的结点,也应该要删除,这是坑,其他的,先用并查集判一下环,然后再找连通环. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstrin

hdu 5438 Ponds 拓扑排序

Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621 Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one

hdu 5438 Ponds 长春网赛1002

5438 好吉利的题号 不停的删掉度数小于等于1的点并更新各点度数,直至无法删点,然后dfs即可 #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define ll __int64 const int maxm=100008; const int maxn=10008; struct fuck{ int u,v,w,nex

HDU 5438 Ponds

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 282    Accepted Submission(s): 86 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and

hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2837    Accepted Submission(s): 891 Problem Description Betty owns a lot of ponds, so

HDU 4921 Map DFS+状态压缩+乘法计数

算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久,其实因为只能取某个链的前缀,所以直接取链长加+1 然后相乘即可,当然因为会出现都是空的那种情况,要去掉,全部乘完之后,要-1 然后就是算权值了,权值等于当前加进来的点的总和 以及 等级相同的点的加成,并不是特别好算,这时候考虑每个状态下的点对全局的贡献,对,就是这个思想,用状态压缩来表示状态,然后这