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 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(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≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) 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

HINT

题意

给你一个图,然后要求把度数小于2的点全部删去,然后问你奇数集合的点权和是多少

注意,你删去了一个点之后,可能会使得一些点又变成了度数小于2的

题解:

用类似拓扑排序的思想去做就ok啦

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int N=200100;
long long a[N],ans;
int n,m,T,cnt,ok[N],vis[N],pre[N],nxt[N],to[N],tot[N],col;
vector<int> s[N];
queue<int> q;

void dfs(int x,int fa)
{
    s[col].push_back(x);
    ok[x]=0;
    for(int p=pre[x];p!=-1;p=nxt[p])
    {
        if((!vis[p])||(!ok[to[p]])) continue;
        if(p==(fa^1)) continue;
        dfs(to[p],p);
    }
}
void makeedge(int x,int y)
{
    to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
    to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(tot,0,sizeof(tot));
        memset(pre,-1,sizeof(pre));
        memset(ok,1,sizeof(ok));
        memset(vis,1,sizeof(vis));
        ans=0LL;cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            tot[x]++;tot[y]++;
            makeedge(x,y);
        }
        while(!q.empty()) q.pop();
        for(int i=1;i<=n;i++)
        {
            if(tot[i]<2)
            {
                q.push(i);
            }
        }
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            ok[x]=0;
            for(int p=pre[x];p!=-1;p=nxt[p])
            {
                vis[p]=0;
                tot[x]--;
                tot[to[p]]--;
                if(ok[to[p]]&&tot[to[p]]<2)
                {
                    q.push(to[p]);
                }
            }
        }
        col=0;
        for(int i=1;i<=n;i++)
        {
            col++;
            s[col].clear();
            if(ok[i])
            {
                dfs(i,cnt+10);
                if(s[col].size()%2==1)
                {
                    for(int j=0;j<s[col].size();j++)
                    {
                        ans+=a[s[col][j]];
                    }
                }
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-27 02:49:01

hdu 5438 Ponds 拓扑排序的相关文章

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, a

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

HDU 4917 Permutation 拓扑排序的计数

题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以只一张有向无环图.这样子,我们就可以把排列计数的问题转化为一个图的拓扑排序计数问题. 拓扑排序的做法可以参见ZJU1346 . 因为题目中点的数量比较多,所以无法直接用状压DP. 但是题目中的边数较少,所以不是联通的,而一个连通块的点不超过21个,而且不同连通块之间可以看做相互独立的.所以我们可以对

hdu 4857 逃生 拓扑排序+优先队列,逆向处理

hdu4857 逃生 题目是求拓扑排序,但不是按照字典序最小输出,而是要使较小的数排在最前面. 一开始的错误思路:给每个点确定一个优先级(该点所能到达的最小的点),然后用拓扑排序+优先对列正向处理,正向输出.这是错误的,如下样例: 1 5 4 5 2 4 3 2 1 3 1 正确的解法:是反向建边,点大的优先级高,用拓扑排序+优先队列,逆向输出序列即可. 根据每对限制,可确定拓扑序列,但此时的拓扑序列可能有多个(没有之间关系的点的顺序不定).本题要求较小的点排到前面,则可确定序列. (1)如果点

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

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

ACM: hdu 2647 Reward -拓扑排序

hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble

HDU 2647 Reward(拓扑排序)

Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,and some