码迷,mamicode.com
首页 > 其他好文 > 详细

长春网络赛 1102

时间:2015-09-18 21:57:47      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

Ponds

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


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
技术分享
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int vul[10005],l[10005],s;
 8 vector <int> e[10005];
 9 long long sum;
10 
11 void check(int x)
12 {
13     int i,j,num=0;
14     for(i=0;i<e[x].size();i++)
15     {
16         int v=e[x][i];
17         if(l[v]==1)
18             num++;
19     }
20     if(e[x].size()-num<2)
21     {
22         l[x]=1;
23         for(i=0;i<e[x].size();i++)
24         {
25             int v=e[x][i];
26             if(l[v]==0)
27             {
28                 check(v);
29             }
30         }
31     }
32     return ;
33 }
34 
35 int add(int x)
36 {
37     int i,j;
38     l[x]=1;s++;sum=sum+vul[x];
39     for(i=0;i<e[x].size();i++)
40     {
41         int v=e[x][i];
42         if(l[v]==0)
43             add(v);
44     }
45     return 0;
46 }
47 int main()
48 {
49     int T;
50     int n,m;
51     int i,j;
52     scanf("%d",&T);
53     while(T--)
54     {
55         long long ans=0;
56         scanf("%d %d",&n,&m);
57         memset(l,0,sizeof(l));
58         for(i=1;i<=n;i++)
59             scanf("%d",&vul[i]);
60         for(i=1;i<=n;i++)
61         {
62             e[i].clear();
63         }
64         for(i=1;i<=m;i++)
65         {
66             int u,v;
67             scanf("%d %d",&u,&v);
68             e[u].push_back(v);
69             e[v].push_back(u);
70         }
71         for(i=1;i<=n;i++)
72         {
73             if(l[i]==0)
74             {
75                 check(i);
76             }
77         }
78 
79         for(i=1;i<=n;i++)
80         {
81             if(l[i]==0)
82             {
83                 s=0,sum=0;
84                 add(i);
85                 if(s%2==1)
86                     ans=ans+sum;
87             }
88         }
89         printf("%I64d\n",ans);
90     }
91     return 0;
92 }
View Code

 

长春网络赛 1102

标签:

原文地址:http://www.cnblogs.com/cyd308/p/4820255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!