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

Source

2015 ACM/ICPC Asia Regional Changchun Online

题目大意:有一些池塘,每一个池塘都有一个价值,现在想删除一些池塘。

有如下删除条件:1、一个池塘有两个管道连接的不可以删除。

2、求最后剩下的为奇数环的池塘的价值。

解题思路:用拓扑将所有入度为0和1的点都可以删掉,直到删完为止。在一个点一个点搜过去,判断环中是否为奇数个池塘。如果可以就return和,否则就不加,return0即可。

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <queue>
 6
 7 using namespace std;
 8 #define ll long long
 9 const int N=10010;
10
11 ll p,m,v[N],vis[N],indir[N];
12 vector<ll>G[N];
13
14 ll bfs(ll x)
15 {
16     queue<ll>q;
17     q.push(x);
18     vis[x]=1;
19     ll k=0;
20     ll sum=v[x];
21     while (!q.empty())
22     {
23
24         int s=q.front();
25         q.pop(); //cout<<s<<endl;
26         k++;
27         for (int i=0; i<G[s].size(); i++)
28         {
29             if (!vis[G[s][i]]&&indir[G[s][i]]>=2)//删掉的点不可以加进来
30             {
31                 sum+=v[G[s][i]];
32                 q.push(G[s][i]);
33                 vis[G[s][i]]=1;
34             }
35         }
36     }
37     if (k>=3&&k%2==1)
38         return sum;
39     else
40         return 0;
41 }
42
43 int main()
44 {
45     int T,a,b;
46     scanf("%d",&T);
47     while (T--)
48     {
49         scanf("%lld%lld",&p,&m);
50         memset(vis,0,sizeof(vis));
51         memset(G,0,sizeof(G));
52         memset(indir,0,sizeof(indir));
53         for (int i=1; i<=p; i++)
54         {
55             scanf("%lld",&v[i]);
56         }
57         for (int i=1; i<=p; i++)
58             G[i].clear();
59         for (int i=1; i<=m; i++)
60         {
61             scanf("%d%d",&a,&b);
62             G[a].push_back(b);//将b放在a队列的最后一个
63             G[b].push_back(a);
64             indir[a]++;
65             indir[b]++;
66         }
67         int j;
68         for (int i=1; i<=p; i++)
69         {
70             for ( j=1; j<=p; j++)
71             {
72                 if (indir[j]==0||indir[j]==1)
73                 {
74                     break;
75                 }
76             }
77             if (j>p)
78                 break;
79             indir[j]=-1;
80             for (int k=0; k<G[j].size(); k++)
81             {
82                 indir[G[j][k]]--;
83             }
84         }
85         ll ans=0;
86         for (int i=1; i<=p; i++)//搜遍所有的环
87             if (!vis[i]&&indir[i]>=2)
88                 ans+=bfs(i);
89         cout<<ans<<endl;
90     }
91     return 0;
92 }
时间: 2024-12-21 21:18:51

hdu 5438 Ponds(长春网络赛 拓扑+bfs)的相关文章

hdu 5438 Ponds 长春网赛1002

5438 好吉利的题号 不停的删掉度数小于等于1的点并更新各点度数,直至无法删点,然后dfs即可 #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define ll __int64 const int maxm=100008; const int maxn=10008; struct fuck{ int u,v,w,nex

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

hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余方程组x≡B[0](mod p[0])x≡B[1](mod p[1])x≡B[2](mod p[2])......解这个同余方程组 用中国剩余定理 Sample Input19 5 23 5 Sample Output6 1 # include <iostream> 2 # include <

hdu 5442 (ACM-ICPC2015长春网络赛F题)

题意: 分析: 明天再写…… #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define d(x) const int MAX_N = (int)(4e4) + 100; //call init_RMQ(f[], n) first. //then call query(a, b) to quest the RMQ of [a, b]. int power[3

HDU 4815 2013长春现场赛C题

C - Little Tiger vs. Deep Monkey Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. "Are y

hdu4271 Find Black Hand 2012长春网络赛E题 最短编辑距离

hdu4271 Find Black Hand  2012长春网络赛E题  最短编辑距离 Find Black Hand Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 1 Problem Description I like playing game with my friend

HDU 5024 (广州网络赛) Wang Xifeng&#39;s Little Plot 记忆化搜索+枚举

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

2015长春网络赛总结

早上七点多就(冻)醒来了,训练了一个暑假,acm生涯的第一场网络赛,很激动. 九点开打,我拔不出网线,用的机房电脑,装的cb有问题,不能编译,只好用dev.男神电脑插上网线没有网,习惯了linux可能不习惯吧.这提醒我们以后一定要早点去把环境调好. 第三分钟,G题有人A了.我跟560开始看题,男神还在弄电脑.题意是给你n个数(n<1000),然后q(q<1000)次询问,要求你输出[l,r]区间的最大值.数据很小,我说暴力,然后560说线段树,然后模板13分钟1Y.然后560开始搞J,一个貌似

15年-ICPC长春-网络赛

ID name status one word    POJ 5437 Alisha’s Party 赛后AC. 优先队列,模拟.对时间t排序 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> using namespace std; struct Node { char name[210]; int v