标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3178 Accepted Submission(s): 988
#include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> #include<queue> using namespace std; typedef long long LL; const int N = 10005; const int M = 100005; struct Edge{ int v,next; }edge[2*M]; int head[N]; LL value[N]; int indegree[N]; int tot; int vis[N]; queue<int> q; void init(){ while(!q.empty()) q.pop(); memset(indegree,0,sizeof(indegree)); memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); tot=0; } void addEdge(int u,int v,int &k){ edge[k].v = v,edge[k].next = head[u],head[u]=k++; } LL weight; int cnt; void dfs(int u){ vis[u]=1; cnt++; weight+=value[u]; for(int k=head[u];k!=-1;k=edge[k].next){ int v=edge[k].v; if(!vis[v]){ dfs(v); } } } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { init(); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%lld",&value[i]); } for(int i=1;i<=m;i++){ int a,b; scanf("%d%d",&a,&b); indegree[a]++,indegree[b]++; addEdge(a,b,tot); addEdge(b,a,tot); } for(int i=1;i<=n;i++){ if(indegree[i]==1) q.push(i); } while(!q.empty()){ int u = q.front(); vis[u]=1; q.pop(); for(int k = head[u];k!=-1;k=edge[k].next){ int v = edge[k].v; if(!vis[v]){ indegree[v]--; indegree[u]--; if(indegree[v]==1) q.push(v); } } } LL res = 0; for(int i=1;i<=n;i++){ weight=0,cnt=0; if(!vis[i]) dfs(i); if(cnt&1&&cnt>1){ res+=weight; } } printf("%lld\n",res); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5568291.html