hdu 5438 Ponds(拓扑排序+dfs)

Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2918    Accepted Submission(s): 917


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

solution:

这题我们先删除入度为1的点,直到没有入度为1的点,然后统计顶点个数为奇数的联通块的边权和。先拓扑排序把入度为1的点去掉,然后dfs查找

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1e5 + 200;
int head[maxn],id[maxn],vis[maxn],a[maxn];
int n, m,tot,pnum;
long long sum;
struct node{
    int to, next;
}e[maxn << 1];
void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
    memset(id, 0, sizeof(id));
    memset(vis, 0, sizeof(vis));
}
void add(int u, int v)
{
    e[tot].to = v; e[tot].next = head[u]; head[u] = tot++;
}
void tuopu()
{
    queue<int>que;
    for (int i = 1; i <= n;i++)
        if (id[i] == 1)
        {
            que.push(i); id[i] = -1;
        }
    while (!que.empty())
    {
        int u = que.front(); que.pop();
        for (int i = head[u]; i != -1; i = e[i].next)
        {
            int v = e[i].to;
            id[v]--;
            if (id[v] == 1)
            {
                que.push(v); id[v] = -1;
            }
        }
    }
}
void dfs(int u)
{
    vis[u] = 1;
    pnum++;
    sum += a[u];
    for (int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].to;
        if (vis[v] == 0 && id[v] > 0)dfs(v);
    }
}
int main()
{
    int t,x,y;
    scanf("%d", &t);
    while (t--)
    {
        init();
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &x, &y);
            add(x, y);add(y, x);
            id[x]++; id[y]++;
        }
        tuopu();
        long long ans = 0;
        for (int i = 1; i <= n;i++)
            if (vis[i] == 0 && id[i]>0)
            {
                sum = 0; pnum = 0;
                dfs(i);
                if (pnum & 1)ans += sum;
            }
        printf("%I64d\n", ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_22522375/article/details/51298074