标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3851 Accepted Submission(s): 582
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 }
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4820255.html