标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
//1085422276 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<cmath> #include<map> #include<bitset> #include<set> #include<vector> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)); #define memfy(a) memset(a,-1,sizeof(a)) #define TS printf("111111\n"); #define FOR(i,a,b) for( int i=a;i<=b;i++) #define FORJ(i,a,b) for(int i=a;i>=b;i--) #define READ(a,b,c) scanf("%d%d%d",&a,&b,&c) #define mod 1000000007 #define maxn 1000006 inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); } return x*f; } //**************************************** int n,m; ll sum; int s,vis[maxn*2],v[maxn],out[maxn]; vector<int >G[maxn]; void init() { FOR(i,1,n)G[i].clear(); mem(vis); mem(out); } void dfs(int x) { vis[x]=1; s++; sum+=v[x]; for(int i=0;i<G[x].size();i++) { if(!vis[G[x][i]]) { dfs(G[x][i]); } } } int main() { int T=read(); while(T--) { init(); scanf("%d%d",&n,&m); FOR(i,1,n) { scanf("%d",&v[i]); } int a,b; FOR(i,1,m) { scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); out[a]++; out[b]++; } ll ans=0; queue<int >q; FOR(i,1,n) { if(out[i]<=1)q.push(i); ans+=v[i]; } while(!q.empty()) { int k=q.front(); q.pop(); vis[k]=1; ans-=v[k]; for(int i=0;i<G[k].size();i++) { out[G[k][i]]--; if(out[G[k][i]]<=1&&!vis[G[k][i]])q.push(G[k][i]); } } FOR(i,1,n) { sum=0; s=0; if(!vis[i])dfs(i); if(s%2==0)ans-=sum; } cout<<ans<<endl; } return 0; }
2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图
标签:
原文地址:http://www.cnblogs.com/zxhl/p/4805292.html