HDU 5438 Ponds(2015ACM长春网络赛+枚举删点+DFS求联通块)
来源:互联网 发布:帝国cms整站模板 编辑:程序博客网 时间:2024/05/16 16:20
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1026 Accepted Submission(s): 344
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 valuev .
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
//分析:题目求尽可能的不断删掉边数小于2的点 最后求所有独立子块 如果点数为奇数则加起来 否则不加
//英语差真可怕 刚开始题意错了 直接跪了... 其实不难 首先不断求出边数小于2的点 删掉 并将连接的点减1 如果减后小于2 则进队 下次删掉 最后dfs求连通块
#include <iostream>#include <cstdio>#include <string.h>#include <string>#include <vector>#include <queue>using namespace std;const int N=10000+5;vector<int > ve[N]; //邻接表 bool book[N]; //标记删了没 bool flag[N]; //用于求连通dfs标记用 不用回朔 int edge[N]; //点的边数 long long num[N],sum;int n,m;queue<int> q;int child; //用于记录每个联通块的点数 inline void del(){int i;for(i=1;i<=n;i++) if(edge[i]<2){ q.push(i); book[i]=1; } while(!q.empty()){int now=q.front();q.pop();for(i=0;i<ve[now].size();i++){int x=ve[now][i];if(book[x]==1) //删掉了的 continue;edge[x]--;if(edge[x]<2){book[x]=1;q.push(x);}}}}void dfs(int now){int i;for(i=0;i<ve[now].size();i++){int x=ve[now][i];if(book[x]==1||flag[x]==1) continue;child++;sum+=num[x];flag[x]=1;dfs(x);}}int main(){int t,i,a,b;long long re;scanf("%d",&t);while(t--){re=0; scanf("%d%d",&n,&m);// while(!q.empty()) q.pop(); for(i=1;i<=n;i++) { scanf("%lld",&num[i]); book[i]=0; flag[i]=0; edge[i]=0; ve[i].clear(); } for(i=0;i<m;i++){ scanf("%d%d",&a,&b); if(a==b) continue; ve[a].push_back(b); ve[b].push_back(a); edge[a]++; edge[b]++; } del(); for(i=1;i<=n;i++){ if(book[i]==0&&flag[i]!=1){ //还没删 flag[i]=1; sum=num[i]; child=1; dfs(i); if(child%2) re+=sum; } } printf("%lld\n",re);}return 0;}
0 0
- HDU 5438 Ponds(2015ACM长春网络赛+枚举删点+DFS求联通块)
- hdu 5438 Ponds 2015长春网络赛 图论 dfs bfs
- HDU 5438 Ponds(2015ACM/ICPC长春网络赛B)
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
- hdu 5438 hdu 5438 Ponds(长春网络赛 拓扑+bfs)
- DFS求联通块
- hdu 5438 Ponds DFS
- HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online
- Coconuts HDU - 5925 (离散化+dfs求联通块)
- 用DFS求联通块
- hdu 5438 Ponds(toposort+DFS)
- HDU 5438 Ponds dfs模拟
- hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)
- HDU5438(2015长春网赛)——Ponds(拓扑排序+dfs)
- 2012 ACM/ICPC 长春赛区网络赛 1006(dfs)
- HDU---5438-Ponds(DFS)(2015 Changchun)
- hdu 5438 Ponds 2015 ACM/ICPC Asia Regional Changchun Online
- java io流的一部分解析
- Java习题4—IO流与异常
- Linux文件系统
- iOS 平板与 Android 平板的交互与动画
- 对《Java初体验》中代码的再探究
- HDU 5438 Ponds(2015ACM长春网络赛+枚举删点+DFS求联通块)
- JAVA String 不可变对象
- 第3周项目1顺序表的基本运算
- STM32点阵
- HDU 5442 Favorite Donut (2015年长春赛区网络赛F题)
- 修改ubuntu终端里面那个提示符
- Oracle数据库安装成功后,忘记解锁账户和设置密码
- noip2006 明明的随机数 (模拟)
- 辛星收集整理之twig的常见用法