标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1288 Accepted Submission(s): 429
#include<stdio.h> #include<string.h> #include<queue> #include<vector> #include<iostream> #include<algorithm> using namespace std; const int maxn=10005; const int maxm=100005; int val[maxn]; vector<int> g[maxm]; int ind[maxn]; bool vis[maxn]; void dfs(int u,long long &cnt,long long &temp){ vis[u]=true; cnt++; temp+=val[u]; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(vis[v]) continue; dfs(v,cnt,temp); } } int main(){ int t; scanf("%d",&t); while(t--){ int n,m; memset(val,0,sizeof(val)); memset(ind,0,sizeof(ind)); memset(vis,false,sizeof(vis)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ g[i].clear(); scanf("%d",&val[i]); } int u,v; for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); g[v].push_back(u); g[u].push_back(v); ind[u]++; ind[v]++; } queue<int>q; for(int i=1;i<=n;i++ ){ if(ind[i]<2){ q.push(i); } } while(!q.empty()){ int u=q.front(); q.pop(); vis[u]=true; ind[u]=0; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; ind[v]--; if(ind[v]<2&&!vis[v]){ q.push(v); } } } long long ans=0,cnt=0,temp=0;//注意,这里必须用long long,long int会wa for(int i=1;i<=n;i++){ if(vis[i]==true) continue; temp=0; cnt=0; dfs(i,cnt,temp); if(cnt%2==1) ans+=temp; } printf("%lld\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/13224ACMer/p/4809833.html