hdu 5438(类似拓扑排序)

Ponds

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


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
 
题意:一些池塘,能够破坏其的条件是其只与一个池塘相连或者不与任何池塘相连,问最后每个连通分量里面池塘数为奇数的点的权值之和为多少??
被坑了!。。刚刚只考虑了度为1的点,度为0的被忽视了。WA三次。。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int N = 10005;
const int M = 100005;
struct Edge{
    int v,next;
}edge[2*M];
int head[N];
LL value[N];
int indegree[N];
int tot;
int vis[N];
queue<int> q;
void init(){
    while(!q.empty()) q.pop();
    memset(indegree,0,sizeof(indegree));
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    tot=0;
}
void addEdge(int u,int v,int &k){
    edge[k].v = v,edge[k].next = head[u],head[u]=k++;
}
LL weight;
int cnt;
void dfs(int u){
    vis[u]=1;
    cnt++;
    weight+=value[u];
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v=edge[k].v;
        if(!vis[v]){
            dfs(v);
        }
    }
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) {
            scanf("%lld",&value[i]);
        }
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            indegree[a]++,indegree[b]++;
            addEdge(a,b,tot);
            addEdge(b,a,tot);
        }
        for(int i=1;i<=n;i++){
            if(indegree[i]==1) q.push(i);
        }
        while(!q.empty()){
            int u = q.front();
            vis[u]=1;
            q.pop();
            for(int k = head[u];k!=-1;k=edge[k].next){
                int v = edge[k].v;
                if(!vis[v]){
                    indegree[v]--;
                    indegree[u]--;
                    if(indegree[v]==1) q.push(v);
                }
            }
        }
        LL res = 0;
        for(int i=1;i<=n;i++){
            weight=0,cnt=0;
            if(!vis[i])
            dfs(i);
            if(cnt&1&&cnt>1){
                res+=weight;
            }
        }
        printf("%lld
",res);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5568291.html