hdu 5438(类似拓扑排序)
2016-06-07 20:52
405 查看
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3178 Accepted Submission(s): 988
[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
[align=left]Sample Output[/align]
21
题意:一些池塘,能够破坏其的条件是其只与一个池塘相连或者不与任何池塘相连,问最后每个连通分量里面池塘数为奇数的点的权值之和为多少??
被坑了!。。刚刚只考虑了度为1的点,度为0的被忽视了。WA三次。。
#include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> #include<queue> using namespace std; typedef long long LL; const int N = 10005; const int M = 100005; struct Edge{ int v,next; }edge[2*M]; int head ; LL value ; int indegree ; int tot; int vis ; queue<int> q; void init(){ while(!q.empty()) q.pop(); memset(indegree,0,sizeof(indegree)); memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); tot=0; } void addEdge(int u,int v,int &k){ edge[k].v = v,edge[k].next = head[u],head[u]=k++; } LL weight; int cnt; void dfs(int u){ vis[u]=1; cnt++; weight+=value[u]; for(int k=head[u];k!=-1;k=edge[k].next){ int v=edge[k].v; if(!vis[v]){ dfs(v); } } } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { init(); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%lld",&value[i]); } for(int i=1;i<=m;i++){ int a,b; scanf("%d%d",&a,&b); indegree[a]++,indegree[b]++; addEdge(a,b,tot); addEdge(b,a,tot); } for(int i=1;i<=n;i++){ if(indegree[i]==1) q.push(i); } while(!q.empty()){ int u = q.front(); vis[u]=1; q.pop(); for(int k = head[u];k!=-1;k=edge[k].next){ int v = edge[k].v; if(!vis[v]){ indegree[v]--; indegree[u]--; if(indegree[v]==1) q.push(v); } } } LL res = 0; for(int i=1;i<=n;i++){ weight=0,cnt=0; if(!vis[i]) dfs(i); if(cnt&1&&cnt>1){ res+=weight; } } printf("%lld\n",res); } return 0; }
相关文章推荐
- jdbc 连 oracle 12c
- Spring JdbcTemplate方法详解
- CocoaPods安装
- 练字在现代社会的意义还大不大,尤其是电脑普及的情况下,花费大量的时间去练字还值得么?
- 编外话题外
- Jquery--------图片轮放
- HDU 1171 Big Event in HDU (母函数)
- Python 装饰器 学习笔记
- php数组概述
- Android注解找控件
- Proxy 示例
- 武器类属性
- 设置numberpicker、DatePicker中间分割线颜色
- 今天突然发现www.erphome.net又回来了,哥还是版主
- graph-tool文档(一)- 快速开始使用Graph-tool - 2.属性映射、图的IO和Price网络
- windows命令行命令
- GC&JVM
- MVP模式与MVC模式 概念
- 命令模式(Command Pattern)
- Minor GC、Major GC和Full GC之间的区别