HDU5438:Ponds(拓扑排序)
2016-07-08 15:46
288 查看
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3204 Accepted Submission(s): 995
[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
思路:拓扑排序,动态删边,注意结果要用long long类型存储,int 会爆
#include <iostream> #include <queue> #include <vector> using namespace std; const int MAXN=10005; int n,m; vector<int> arc[MAXN]; int val[MAXN],deg[MAXN],vis[MAXN]; void topsort() { queue<int> que; for(int i=1;i<=n;i++) { if(!vis[i]&°[i]<=1) { que.push(i); vis[i]=1; } } while(!que.empty()) { int u=que.front();que.pop(); for(int i=0;i<arc[u].size();i++) { int v=arc[u][i]; if(!vis[v]) { deg[v]--; if(deg[v]<=1) { que.push(v); vis[v]=1; } } } } } void dfs(int u,int& num,long long& sum) { sum+=val[u]; num++; vis[u]=1; for(int i=0;i<arc[u].size();i++) { int v=arc[u][i]; if(!vis[v]) { dfs(v,num,sum); } } } int main() { int T; cin>>T; while(T--) { cin>>n>>m; for(int i=1;i<=n;i++) arc[i].clear(); memset(deg,0,sizeof(deg)); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { cin>>val[i]; } for(int i=0;i<m;i++) { int u,v; cin>>u>>v; arc[u].push_back(v); arc[v].push_back(u); deg[u]++; deg[v]++; } topsort(); long long res=0; for(int i=1;i<=n;i++) { if(vis[i]) continue; int num=0; long long sum=0; dfs(i,num,sum); if(num&1) res+=sum; } cout<<res<<endl; } return 0; }
相关文章推荐
- windows 下使用 zip安装包安装MySQL 5.7
- JS:window.onload的使用介绍
- C#笔记_1-动态生成类,程序集
- Android利用ObjectAnimator实现ArcMenu
- echarts获取注册地图方法
- 适用于头像的圆形的ImageView
- Street
- 有些人为什么那么努力
- ORACLE创建表之前判断表是否存在与SQL Server 对比使用
- android studio代码混淆注意问题
- 创业5
- 计算机启动过程【转载】
- 推荐桌游
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
- CSS3 transform介绍|如何设计炫酷的动画效果
- span 的用法
- Java io操作,poi导出excel,集合自带排序,日志报告
- UIView的clipsToBounds属性和CALayer的masksToBounds属性的比较
- UIView的clipsToBounds属性和CALayer的masksToBounds属性的比较
- SpringMVC