HDU 5438 Ponds(拓扑排序 + DFS)
2015-09-19 09:55
288 查看
Ponds
[align=left]Problem Description[/align]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
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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个泳池,一共有m条通道将其中的泳池连接起来,现在需要拆除那些相连最多一个的泳池,求剩下连通块中泳池为奇数个的这些连通块的权值之和。抽象出来就是删去无向图中度为0或者1的结点,再求剩下的连通块中结点为奇数个连通块的权值。
先用拓扑排序删去度为0或者1的结点,再用DFS对每个连通块的结点遍历计数即可。
代码如下:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <queue> #include <algorithm> using namespace std; typedef long long ll; const int maxm = 1e5 + 5; const int maxn = 1e4 + 5; int n,m,cnt,num; int head[maxn],deg[maxn];//deg为结点的度数 int u,v; bool visited[maxn]; ll tmp,ans,val[maxn]; struct Edge { int to,next; }edge[maxm]; queue <int> q; void addEdge(int u,int v) { edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; } void init() { while(!q.empty()) { q.pop(); } cnt = ans = 0; memset(head,-1,sizeof(head)); memset(deg,0,sizeof(deg)); memset(visited,false,sizeof(visited)); scanf("%d %d",&n,&m); for(int i = 1;i <= n;i++) { scanf("%lld",&val[i]); } for(int i = 1;i <= m;i++) { scanf("%d %d",&u,&v); addEdge(u,v); addEdge(v,u); deg[u]++; deg[v]++; } } void topoSort()//删点 { for(int i = 1;i <= n;i++) { if(deg[i] == 0) { visited[i] = true;//直接删去独立结点 } if(deg[i] == 1)//度为1的结点入队,因为删去该结点后其连接的结点度数可能变为0或1 { visited[i] = true; q.push(i); } } while(!q.empty()) { int temp = q.front(); q.pop(); for(int i = head[temp];i != -1;i = edge[i].next) { int v = edge[i].to;//temp相邻的结点 deg[v]--; if(deg[v] == 0) { visited[v] = true; } if(deg[v] == 1) { visited[v] = true; q.push(v); } } } } void dfs(int u,int des)//计数遍历回环图,des记录终点(也就是一开始的起点) { visited[u] = true; num++; tmp += val[u]; for(int i = head[u];i != -1;i = edge[i].next) { int v = edge[i].to; if(!visited[v] && v != des) { dfs(v,u); } } return; } int main() { int t; scanf("%d",&t); while(t--) { init(); topoSort(); for(int i = 1;i <= n;i++) { if(!visited[i]) { num = 0; tmp = 0; dfs(i,0); if(num & 1) { ans += tmp; } } } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 周鸿祎的奇酷手机梦
- 图解Linux命令之--cp命令
- 精品软件 推荐 美图秀秀 PS软件
- 黑马程序员--NSString前后缀检查及搜索
- 荷兰政府采用ODF开放文件格式取代微软OpenXML
- Swift 关键词guard(监视)
- 分享的资源
- Java知识整理(四)之SE.正则表达式
- 反射给没有set方法的属性赋值
- 阿里云ECS-搭建环境(jdk8+tomcat8+nginx1.9.5)
- 垂直伸缩菜单——bootstrap手风琴嵌套
- Android的内存管理机制
- IP地址分类
- Xcode 7 修改应用Bundle ident 96f7 ifier
- 数组A中任意两个相邻元素大小相差1
- hadoop倒排索引
- Android:开发中遇到的异常Error解析
- Java知识整理(三)之SE.(String+包装类+Math)
- # hihocoder #1082 : The Marshtomp has seen it all before
- 云平台中UBUNTU14.04 搭建有域名的 WORDPRESS