hdu 5438 Ponds(長春網絡賽 拓撲+bfs)

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

 

Ponds

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


ios

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(1T30) 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(1p104) which represents the number of ponds she owns, and the other is the number m(1m105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1vi108) 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
 
題目大意:有一些池塘,每個池塘都有一個價值,如今想刪除一些池塘。
有以下刪除條件:一、一個池塘有兩個管道鏈接的不能夠刪除。
二、求最後剩下的爲奇數環的池塘的價值。
 
解題思路:用拓撲將全部入度爲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 }
相關文章
相關標籤/搜索