HDU 5438 Ponds (拓撲排序應用+DFS)

_TCgogogo_發表於2015-09-15


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
 
Input
The first line of input will contain a number T(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 number p(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
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算和


#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);
    }
}



 

相關文章