HDU 5438 Ponds (拓撲排序應用+DFS)
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 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
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 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.
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.
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
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
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 long
using 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);
}
}
相關文章
- DFS實現拓撲排序排序
- HDU 4857 逃生(拓撲排序)排序
- 有向圖的拓撲排序——DFS排序
- HDU4857逃生(拓撲排序)排序
- HDU 4857-逃生(反向拓撲排序-按條件排序)排序
- hdu 1811 並查集+拓撲排序並查集排序
- 拓撲排序詳解(梅開二度之dfs版按字典序輸出拓撲路徑+dfs版輸出全部拓撲路徑排序
- 拓撲排序排序
- HDU1285確定比賽名次(拓撲排序)排序
- 拓撲排序,YYDS排序
- 拓撲排序模板排序
- 拓撲排序小結排序
- 圖論——拓撲排序圖論排序
- 筆記:拓撲排序筆記排序
- Reward (圖論+拓撲排序)圖論排序
- 拓撲排序 - Topological Sort排序
- 拓撲排序核心程式碼排序
- AOV網與拓撲排序排序
- 【筆記/模板】拓撲排序筆記排序
- 拓撲排序就這麼回事排序
- 紙上談兵: 拓撲排序排序
- poj 1094 拓撲排序排序
- hdu5222 拓撲+並查集並查集
- poj1094 拓撲排序排序
- 演算法-圖論-拓撲排序演算法圖論排序
- 圖解拓撲排序+程式碼實現圖解排序
- 【圖論】拓撲排序+優先佇列圖論排序佇列
- POJ 3249-Test for Job(拓撲排序&&DP)排序
- CF 274D Lovely Matrix(拓撲排序)排序
- 網路拓撲圖上文字的巧妙應用
- 圖的拓撲排序詳解與實現排序
- 圖(3)--拓撲排序與關鍵路徑排序
- (set+拓撲排序) CF1572A Book排序
- 拓撲排序 (BFS )DAG (有向無環圖)排序
- Mr. Kitayuta‘s Technology CodeForces - 505D(並查集+拓撲排序或dfs找環) 題解並查集排序
- ECharts整合HT for Web的網路拓撲圖應用EchartsWeb
- 1385E. Directing Edges(拓撲序的應用)
- VOL.2 拓撲排序與關鍵路徑排序