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
 
Input
The first line of input will contain a numberT(1T30) 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(1p104) which represents the number of ponds she owns, and the other is the number m(1m105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1vi108) 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
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算和


#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
原创粉丝点击