HDU 5438 Ponds
来源:互联网 发布:linux acpi 编辑:程序博客网 时间:2024/06/05 18:23
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2861 Accepted Submission(s): 901
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
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 numberp(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 containsp numbers v1,...,vp , where vi(1≤vi≤108) indicating the value of pond i .
Each of the lastm lines contain two numbers a and b , which indicates that pond a and pond b are connected by a pipe.
For each test case, the first line contains two number separated by a blank. One is the number
The next line contains
Each of the last
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
17 71 2 3 4 5 6 71 41 54 52 32 63 62 7
Sample Output
21
Source
2015 ACM/ICPC Asia Regional Changchun Online
简单的搜索题目。每次将度为1的池塘删掉,问最后剩余的池塘中,相连池塘数为奇数的权值和。
每次对度为1的点进行搜索,将对应边删除并将相连点的度减一,最后遍历池塘,对度不为0的池塘进行搜索并记录结果。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;struct edge{ int v; int next;}e[200010];int d[10010];struct node{ int u; int w; friend bool operator < (node a ,node b) { return d[a.u]>d[b.u]; }}N[10010];int head[10010];int tol,n,m;long long ans;bool vis[200010],flag[10010];void addedge(int u,int v){ e[tol].v=v; e[tol].next=head[u]++; head[u]=tol++; e[tol].v=u; e[tol].next=head[v]++; head[v]=tol++; return ;}long long bfs(int st,int sum){ long long num=N[st-1].w; queue<int >q; q.push(st); //cout<<st<<" "; while(q.size()) { st=q.front(); q.pop(); for(int i=head[st];i!=-1;i=e[i].next) { if(!vis[i]&&!flag[e[i].v]) { //cout<<e[i].v<<"*"; sum++; num+=N[e[i].v-1].w; q.push(e[i].v); vis[i]=true ; vis[i^1]=true ; flag[e[i].v]=true ; } } } if(sum&1) return num; else return 0;}void DFS(int st){ for(int i=head[st];i!=-1;i=e[i].next) { if(!vis[i]) { vis[i]=true ; vis[i^1]=true ; d[st]--; d[e[i].v]--; if(d[e[i].v]==1) DFS(e[i].v); return ; } } return ;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",&N[i].w),d[i+1]=0,N[i].u=i+1; tol=0; ans=0; memset(head,-1,sizeof(head)); while(m--) { int u,v; scanf("%d%d",&u,&v); d[u]++; d[v]++; addedge(u,v); } memset(vis,false ,sizeof(vis)); memset(flag,false ,sizeof(flag)); for(int i=1;i<=n;i++) { if(d[i]==1) { DFS(i); } } for(int i=1;i<n;i++) { if(d[i]&&!flag[i]) ans+=bfs(i,1); } cout<<ans<<endl; } return 0;}
0 0
- hdu 5438 Ponds DFS
- HDU 5438 Ponds
- hdu 5438Ponds
- HDU 5438 Ponds
- HDU 5438 Ponds
- HDU 5438 Ponds
- HDU 5438 Ponds
- HDU 5438Ponds
- HDU 5438Ponds
- HDU 5438 Ponds
- hdu 5438 Ponds(toposort+DFS)
- HDU 5438 Ponds (搜索)
- HDU 5438 Ponds dfs模拟
- Ponds HDU
- HDU 5438 Ponds 拓扑+并查集
- hdu 5438 Ponds(强连通)
- hdu 5438 Ponds(线段树+dfs)
- hdu 5438 Ponds(拓扑应用、DFS)
- Redis键时间老化的测试
- 数组
- tomcat下多应用log4j冲突:Web app root system property already set to different value
- ubus数据结构和接口介绍
- iOS开发证书问题
- HDU 5438 Ponds
- elasticsearch中的API
- 搜索算法总结
- opencv 皮肤光泽评判
- jquery跨域问题
- 深入浅出讲解:php的socket通信
- .Net下实现可扩展的编程方法简述
- 菜鸟学习Node.js--express(一)
- JAVA--抽象工厂模式--设计模式二