Ponds----hdu5438

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438

题意:有n个池塘和m个管道;每个池塘的价值是v, 现在由于资金问题要删除池塘;但是删除的池塘必须是最多只连接一个管道,否则会爆炸;

求最后相连的池塘有奇数个的价值总和是多少;

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
#define N 100100
#define INF 0xfffffff

int v[N], f[N], du[N], vis[N], a[N], b[N], sum[N];
vector<vector<int> > G;

int Find(int x)
{
    if(x!=f[x])
        f[x] = Find(f[x]);
    return f[x];
}

int main()
{
    int T, n, m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);

        for(int i=1; i<=n; i++)
        {
            scanf("%d", &v[i]);
            f[i] = i;
            sum[i] = du[i] = vis[i] = 0;
        }
        G.resize(n+1);
        G.clear();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d", &a[i], &b[i]);

            du[a[i]]++;
            du[b[i]]++;
            G[a[i]].push_back(b[i]);
            G[b[i]].push_back(a[i]);
        }
        queue<int>Q;
        for(int i=1; i<=n; i++)
        {
            if(du[i]<2)///把度为<2的入队,并删除;
            {
                vis[i] = 1;
                Q.push(i);
            }
        }
        while(!Q.empty())
        {
            int p=Q.front();
            Q.pop();
            int len = G[p].size();
            for(int j=0; j<len; j++)
            {
                int q = G[p][j];
                du[q]--;
                if(vis[q]==0 && du[q]<2)
                {
                    Q.push(q);
                    vis[q] = 1;
                }
            }
        }

        for(int i=1; i<=m; i++)///让有联系的放到一个集合里
        {
            if(!vis[a[i]] && !vis[b[i]])
            {
                int pa = Find(a[i]);
                int pb = Find(b[i]);
                if(pa != pb)
                    f[pb] = pa;
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(!vis[i])
                sum[Find(i)]++;///记录每个连通图的个数;
        }
        long long ans = 0;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i] && sum[f[i]]%2==1)///判断是否有奇数个;
                ans+=v[i];
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

时间: 2024-10-21 22:22:31

Ponds----hdu5438的相关文章

hdu5438 Ponds dfs 2015changchun网络赛

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 533    Accepted Submission(s): 175 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, an

hdu5438 ponds

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 985    Accepted Submission(s): 328 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, an

hdu 5438 Ponds dfs

Time Limit: 1500/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) 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. Ea

hdu 5438 Ponds(长春网络赛 拓扑+bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2237    Accepted Submission(s): 707 Problem Description Betty owns a lot of ponds, som

HDU 5438 Ponds

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 282    Accepted Submission(s): 86 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and

hdu 5438 Ponds 拓扑排序

Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621 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

Ponds

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 467 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, a

hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2837    Accepted Submission(s): 891 Problem Description Betty owns a lot of ponds, so

hdu5438 dfs+并查集 长春网赛

先dfs对度小于2的删边,知道不能删为止. 然后通过并查集来计算每一个分量里面几个元素. #include<iostream> #include<cstring> #include<vector> #define maxn 10010 #define LL __int64 using namespace std; int in[maxn],vis[maxn],p,pa[maxn],cou[maxn]; LL sum[maxn]; vector<int>mp[

HDU5438 拓扑排序

题目here 有n个水池,每个水池有价值v,有m条水管连接 a b 两个水池 ,现在要求把只有两个以下相邻水池的水池去掉,直到不能去为止,最后将各组水池中,集合数为奇数的组的水池价值总和加起来. 拓扑排序练习题 #include<cstdio> #include<cmath> #include<queue> #include<cstring> #include<vector> #include<iostream> #include&l