2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 (拓扑排序+并查集)

Ponds

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

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(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

先把边存起来,度计算出来,按照拓扑序删点(单点要注意,这里wa了一次),然后扫描一遍边,有删掉的点就忽略,用并查集维护连通分量的结点数和总权值。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5,maxm = 2e5+10;
int n,m;
int val[maxn];
int head[maxn],nxt[maxm],to[maxm];
int deg[maxn],ecnt;
bool rmvd[maxn];

void addEdge(int u,int v)
{
    to[ecnt] = v;
    nxt[ecnt] = head[u];
    head[u] = ecnt++;
    deg[u]++;
}

void topo()
{
    queue<int> q;
    for(int i = 1; i <= n; i++){
        if(deg[i] <= 1){
            rmvd[i] = true;
            q.push(i);
        }
    }
    while(q.size()){
        int u = q.front(); q.pop();
        for(int i = head[u]; ~i; i = nxt[i]){
            int v = to[i];
            if(!rmvd[v] && --deg[v] == 1){
                q.push(v); rmvd[v] = true;
            }
        }
    }
}
long long sum[maxn];
int pa[maxn],cnt[maxn];
int fdst(int x) { return x==pa[x]?x:pa[x]=fdst(pa[x]); }

int main()
{
    //freopen("in.txt","r",stdin);
    int T; scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++) scanf("%d",val+i);
        memset(head,-1,sizeof(head));
        memset(deg,0,sizeof(deg));
        memset(rmvd,0,sizeof(rmvd));
        ecnt = 0;
        for(int i = 0; i < m; i++){
            int u,v; scanf("%d%d",&u,&v);
            addEdge(u,v); addEdge(v,u);
        }
        topo();
        for(int i = 1; i <= n; i++) pa[i] = i,sum[i] = val[i],cnt[i] = 1;
        for(int i = 0,M = 2*m; i < M; i += 2){
            int u = to[i], v = to[i^1];
            if(!rmvd[u] && !rmvd[v]){
                int a = fdst(u), b = fdst(v);
                if(a != b){
                    pa[a] = b;
                    sum[b] += sum[a];
                    cnt[b] += cnt[a];
                }
            }
        }
        long long ans = 0;
        for(int i = 1; i <= n ;i++){
            int f = fdst(i);
            if(!rmvd[f]){
                if(cnt[f]&1){
                    ans += sum[f];
                }
                rmvd[f] = true;
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-11-29 04:17:00

2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 (拓扑排序+并查集)的相关文章

2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 46    Accepted Submission(s): 20 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is trave

2015 ACM/ICPC Asia Regional Changchun Online Pro 1008 Elven Postman

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very lon

(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2061    Accepted Submission(s): 711 Problem Description Jack likes to travel around the wo

2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 329 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very

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

2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v)满足两点之间每一条边都不超过x,问有多少对 思路:首先我想到的是dfs求出每个查询小于等于他的一个连通块,然后num*(num-1)就是答案,但是时间只有一秒,这个复杂度是5*1e8,超时了(亲身体验了) 然后我们想这个是离线的,我们可不可以由小到大来推,这样前面的贡献到后面就依然能用了,但是我们

(线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 591 Problem Description In Land waterless, w

HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)

[题目链接]:click here~~ [题目大意]: HDU 5444 题意:在最初为空的二叉树中不断的插入n个数.对于每个数,从根节点开始判断,如果当前节点为空,就插入当前节点,如果当前节点不为空,则小于当前节点的值,插入右子树,否则插入左子树. 接着q次询问,每次询问一个值在二叉树中从根节点开始的查找路径. 3 直接用二叉树模拟整个插入和询问的过程 代码: /* * Problem: HDU No.5444 * Running time: 0MS * Complier: G++ * Aut

hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online

很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间注意到了这一句——"Not only that, when numbering the rooms, they always number the room number from the east-most position to the west." 它告诉我们,东边的点总是比西边的点