标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 282 Accepted Submission(s): 86
/* *********************************************** Author :pk28 Created Time :2015/9/13 19:18:07 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10005 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } int n,m; struct node{ int v,next; }edge[maxn*20]; int l,pre[maxn]; int w[maxn]; int du[maxn]; int vis[maxn]; void add(int u,int v){ edge[l].v=v; edge[l].next=pre[u]; pre[u]=l++; } queue<int>q; void init(){ memset(pre,-1,sizeof pre); cle(du); l=0; cle(vis); cle(w); while(!q.empty())q.pop(); } ll sum,ans,cnt; void dfs(int u){ for(int i=pre[u];i+1;i=edge[i].next){ int v=edge[i].v; if(!vis[v]){ vis[v]=1; cnt++; sum+=w[v]; dfs(v); } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int t,x,y; cin>>t; while(t--){ init(); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&w[i]); for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); add(x,y); add(y,x); du[x]++; du[y]++; } for(int i=1;i<=n;i++){ if(du[i]<2){ q.push(i); } } while(!q.empty()){ int tmp=q.front(); vis[tmp]=1; q.pop(); for(int i=pre[tmp];i+1;i=edge[i].next){ int v=edge[i].v; du[v]--; if(du[v]<2&&!vis[v])q.push(v); } } sum=0;cnt=0; ans=0; for(int i=1;i<=n;i++){ if(!vis[i]){ dfs(i); if(cnt&1) ans+=sum; cnt=0; sum=0; } } printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/pk28/p/4805426.html