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
 
 
 
 
一个图,无向边,每一个节点有一个权值
若某一个节点的相连的边长数<=1,则这个节点可以去掉
主人公会继续去掉节点直到所有节点都不可以去掉为止
 
现在剩下的节点是一个个的连通分量,
主人公想知道所有所在连通分量的节点数为奇数的节点的权值总和
 
简单题
 
记录每一个节点的入度
1.第1次dfs,去掉可以去的节点
2.第2次dfs,找出哪些连通分量的节点数为奇数,并把这些节点的总权值加入到ret
 
 
 #include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; #define ll long long
#define push_back pb const int maxp=1e4+;
const int maxm=1e5+; struct Edge
{
int to,next;
};
Edge edge[maxm<<]; int head[maxp];
int tot;
bool vis[maxp];
int in[maxp];
ll v[maxp]; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} ll solve(int ,int );
void dfs(int );
void dfs1(int ,int &,ll &); int main()
{
int test;
scanf("%d",&test);
while(test--){
tot=;
memset(head,-,sizeof head);
memset(in,,sizeof in); int p,m;
scanf("%d %d",&p,&m);
for(int i=;i<=p;i++)
scanf("%I64d",&v[i]);
int u,v;
for(int i=;i<m;i++){
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
in[u]++;
in[v]++;
} printf("%I64d\n",solve(p,m));
}
return ;
} ll solve(int p,int m)
{
memset(vis,false,sizeof vis); for(int i=;i<=p;i++){
if(!vis[i]&&in[i]<){
in[i]--;
dfs(i);
}
} ll ret=;
memset(vis,false,sizeof vis);
int num;
ll sum;
for(int i=;i<=p;i++){
if(!vis[i]&&in[i]>){
num=;
sum=;
dfs1(i,num,sum);
if(num%)
ret+=sum;
}
}
return ret;
} void dfs(int u)
{
vis[u]=true;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(vis[v])
continue;
in[v]--;
if(in[v]<){
in[v]--;
dfs(v);
}
}
} void dfs1(int u,int &num,ll &sum)
{
vis[u]=true;
num++;
sum+=v[u];
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(vis[v]||in[v]<)
continue;
dfs1(v,num,sum);
}
}
 
 
 
 
 
 
上一篇:python中Url链接编码处理(urlencode,urldecode)


下一篇:iOS sharedSDK详解