HDU 5438 Ponds (拓扑排序应用+DFS)
来源:互联网 发布:英伦对决影评知乎 编辑:程序博客网 时间:2024/04/30 16:55
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1292 Accepted Submission(s): 429
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 numberT(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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438
题目大意:一个图,不断删去度数小于2的点,求剩下的点数为奇数的连通子图的点数和
题目分析:拓扑排序删点,DFS算和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438
题目大意:一个图,不断删去度数小于2的点,求剩下的点数为奇数的连通子图的点数和
题目分析:拓扑排序删点,DFS算和
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define ll long longusing namespace std;int const MAXM = 1e5 + 5;int const MAXN = 1e4 + 5;int n, m, cnt, num;int head[MAXN], deg[MAXN];bool vis[MAXN];ll tmp, ans, val[MAXN];struct EDGE{ int to, next;}e[MAXM];queue <int> q;void Init(){ while(!q.empty()) q.pop(); cnt = 0; ans = 0; memset(head, -1, sizeof((head))); memset(deg, 0, sizeof(deg)); memset(vis, false, sizeof(vis));}void Add(int u, int v){ e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt ++;}void TopoSort(){ for(int i = 1; i <= n; i++) { if(deg[i] == 0) vis[i] = true; if(deg[i] == 1) { vis[i] = true; q.push(i); } } while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].to; deg[v] --; if(deg[v] == 0) vis[v] = true; if(deg[v] == 1) { vis[v] = true; q.push(v); } } }}void DFS(int u, int fa){ vis[u] = true; num ++; tmp += val[u]; for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].to; if(!vis[v] && v != fa) DFS(v, u); } return;}int main(){ int T; scanf("%d", &T); while(T --) { Init(); int u, v; scanf("%d %d", &n, &m); for(int i = 1; i <= n; i++) scanf("%I64d", &val[i]); for(int i = 1; i <= m; i++) { scanf("%d %d", &u, &v); Add(u, v); Add(v, u); deg[u] ++; deg[v] ++; } TopoSort(); for(int i = 1; i <= n; i++) { if(!vis[i]) { num = 0; tmp = 0; DFS(i, 0); if(num & 1) ans += tmp; } } printf("%I64d\n", ans); }}
0 0
- HDU 5438 Ponds (拓扑排序应用+DFS)
- HDU 5438 Ponds(拓扑排序 + DFS)
- HDU 5438 --Ponds【拓扑排序 && DFS】
- hdu 5438 Ponds(拓扑排序+dfs)
- HDU 5438 Ponds(拓扑排序+dfs)
- hdu 5438 Ponds(拓扑应用、DFS)
- hdu 5438 Ponds 拓扑序+简单dfs
- HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online
- hdoj 5438 Ponds 【拓扑 + DFS】
- HDU 5438 Ponds 拓扑序+并查集/DFS
- hdu 5438 Ponds DFS
- hdu 5438 Ponds(拓扑排序+并查集)
- HDU 5438 Ponds(拓扑排序+并查集)
- hdu 5438 Ponds 拓扑排序 + 并查集
- hdu 5438 Ponds(toposort+DFS)
- HDU 5438 Ponds dfs模拟
- HDU 5438 Ponds 拓扑+并查集
- hdu 5438 Ponds(线段树+dfs)
- sql 经典命令行建立数据库 (增删改查)
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- 准备ndk环境
- 中国省份及其地级市整理JSON版(2015-08-23)
- STL 之 deque的用法
- HDU 5438 Ponds (拓扑排序应用+DFS)
- ng-class用法
- 前台向后台传递中文参数,防止乱码
- FAT12中,如何定位大于一个扇区(512B)的文件内容
- [BZOJ 3040] 最短路(road) · 堆优化dijkstra
- OJ 系列之从M个不同字符中任取N个字符的所有组合
- c++ primer 学习笔记-第十四章
- CentOS下ifconfig eth0:unknown interface:no such device
- Base64编码与图片互转