hdu5438- Ponds (深搜+拓扑排序)

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 vv.

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 Format

The first line of input will contain a number T(1 \le T \le 30)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 \le p \le 10^4)p(1≤p≤104)which represents the number of ponds she owns, and the other is the number m(1 \le m \le 10^5)m(1≤m≤105) which represents the number of pipes.

The next line contains pp numbers v_1,\ ...,\ v_pv1​, ..., vp​, where v_i(1 \le v_i \le 10^8)vi​(1≤vi​≤108) indicating the value of pond ii.

Each of the last mm lines contain two numbers aa and bb, which indicates that pond aa and pond bb are connected by a pipe.

Output Format

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.

样例输入

1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7

样例输出

21

题目来源

ACM-ICPC 2015 Changchun Preliminary Contest

先用拓扑排序删除边,再用深搜搜索连通的是否为奇数,是奇数就累加进去

开始没注意v的范围,只给了int,一直挂

AC:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
vector<int> map[10240];
int w[10240],indegree[10240];
int n,m,flag;
long long sum,ans;
int vis[10240];

void init()
{
    ans=0;
    memset(w,0,sizeof(w));
    memset(map,0,sizeof(map));
    memset(indegree,0,sizeof(indegree));
    memset(vis,0,sizeof(vis));
}

void topo()
{
    queue<int> que;
    for(int i=1;i<=n;i++)
        if(indegree[i]==1)
            que.push(i);
    while(!que.empty())
    {
        int top=que.front();
        que.pop();
        indegree[top]--;
        for(int i=0;i<map[top].size();i++)
            if(map[top][i])
            {
                indegree[map[top][i]]--;
                if(indegree[map[top][i]]==1)
                    que.push(map[top][i]);
            }
    }
}

void dfs(int x)
{
    flag++;
    sum+=w[x];
    vis[x]=1;
    for(int i=0;i<map[x].size();i++)
    {
        if(indegree[map[x][i]]&&!vis[map[x][i]])
            dfs(map[x][i]);
    }
}


int main()
{
    int t,a,b;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            map[b].push_back(a);
            map[a].push_back(b);
            indegree[b]++;
            indegree[a]++;
        }
        topo();
        for(int i=1;i<=n;i++)
        {
            flag=0;
            sum=0;
            if(!vis[i]&&indegree[i]>=2)
                dfs(i);
            ans+=sum*(flag%2);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41183791/article/details/81530280
今日推荐