标签:
Time Limit: 1500/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 5 using namespace std; 6 7 #define ll long long 8 #define push_back pb 9 10 const int maxp=1e4+5; 11 const int maxm=1e5+5; 12 13 struct Edge 14 { 15 int to,next; 16 }; 17 Edge edge[maxm<<1]; 18 19 int head[maxp]; 20 int tot; 21 bool vis[maxp]; 22 int in[maxp]; 23 ll v[maxp]; 24 25 void addedge(int u,int v) 26 { 27 edge[tot].to=v; 28 edge[tot].next=head[u]; 29 head[u]=tot++; 30 } 31 32 ll solve(int ,int ); 33 void dfs(int ); 34 void dfs1(int ,int &,ll &); 35 36 int main() 37 { 38 int test; 39 scanf("%d",&test); 40 while(test--){ 41 tot=0; 42 memset(head,-1,sizeof head); 43 memset(in,0,sizeof in); 44 45 int p,m; 46 scanf("%d %d",&p,&m); 47 for(int i=1;i<=p;i++) 48 scanf("%I64d",&v[i]); 49 int u,v; 50 for(int i=0;i<m;i++){ 51 scanf("%d %d",&u,&v); 52 addedge(u,v); 53 addedge(v,u); 54 in[u]++; 55 in[v]++; 56 } 57 58 printf("%I64d\n",solve(p,m)); 59 } 60 return 0; 61 } 62 63 ll solve(int p,int m) 64 { 65 memset(vis,false,sizeof vis); 66 67 for(int i=1;i<=p;i++){ 68 if(!vis[i]&&in[i]<2){ 69 in[i]--; 70 dfs(i); 71 } 72 } 73 74 ll ret=0; 75 memset(vis,false,sizeof vis); 76 int num; 77 ll sum; 78 for(int i=1;i<=p;i++){ 79 if(!vis[i]&&in[i]>1){ 80 num=0; 81 sum=0; 82 dfs1(i,num,sum); 83 if(num%2) 84 ret+=sum; 85 } 86 } 87 return ret; 88 } 89 90 void dfs(int u) 91 { 92 vis[u]=true; 93 for(int i=head[u];~i;i=edge[i].next){ 94 int v=edge[i].to; 95 if(vis[v]) 96 continue; 97 in[v]--; 98 if(in[v]<2){ 99 in[v]--; 100 dfs(v); 101 } 102 } 103 } 104 105 void dfs1(int u,int &num,ll &sum) 106 { 107 vis[u]=true; 108 num++; 109 sum+=v[u]; 110 for(int i=head[u];~i;i=edge[i].next){ 111 int v=edge[i].to; 112 if(vis[v]||in[v]<2) 113 continue; 114 dfs1(v,num,sum); 115 } 116 }
标签:
原文地址:http://www.cnblogs.com/-maybe/p/4805600.html