2015ACM/ICPC Asia Regional Changchun online/hdu 5438 map

Source: Internet
Author: User

Ponds

Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)

Problem Descriptionbetty owns a lot of ponds, some of them is connected with other ponds by pipes, and there would not be More than one pipe between and ponds. Each pond have a valuev.

Now Betty wants to remove some ponds because she does not having enough money. But each time if she removes a pond, she can only remove the ponds which is connected with less than a ponds, or the Pond would explode.

Note that Betty should keep removing ponds until no more ponds can be removed. After this, please help her calculate the sum of the value for each connected component consisting of a odd number of pond S

Inputthe first line of input would contain a numberT(1≤t≤) Which is the number of the test cases.

For each test case, the first line contains the number separated by a blank. One is the numberP(1≤p≤4) which represents the number of ponds she owns, and the other are the numberm(1≤m≤5) which represents the number of pipes.

The next line containsPNumbersv1,.. . ,vp , wherevi(1≤vi≤8) indicating the value of pondI.

Each of the lastmlines contain, numbersaand b, which indicates that Pond a and pond b is connected by a pipe.

Outputfor each test case, output the sum of the value of any connected components consisting of odd number of ponds after Removing all the ponds connected and less than the pipes.

Sample Input17 71 2 3 4 5 6 71 41 54 52 32 63 62 7

Sample Output21

Source2015 ACM/ICPC Asia Regional Changchun Online pose question test instructions: give you n point, M edge, each time delete its connected edge number less than 2 points, until can not delete, ask you the last point of the value of how much
//1085422276#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<cmath>#include<map>#include<bitset>#include<Set>#include<vector>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a));#defineMemfy (a) memset (A,-1,sizeof (a))#defineTS printf ("111111\n");#definefor (I,A,B) for (int i=a;i<=b;i++)#defineForj (I,A,B) for (int i=a;i>=b;i--)#defineREAD (a,b,c) scanf ("%d%d%d", &a,&b,&c)#defineMoD 1000000007#defineMAXN 1000006inline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//****************************************intn,m;ll sum;ints,vis[maxn*2],V[MAXN], out[Maxn];vector<int>G[MAXN];voidInit () {for (I,1, N)    G[i].clear ();    Mem (VIS); MEM ( out);}voidDfsintx) {Vis[x]=1; S++; Sum+=V[x];  for(intI=0; I<g[x].size (); i++)    {        if(!Vis[g[x][i]])        {DFS (g[x][i]); }    }}intMain () {intt=read ();  while(t--) {init (); scanf ("%d%d",&n,&m); For (I,1, N) {scanf ("%d",&V[i]); }        intb; 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 ()) {            intk=Q.front ();            Q.pop (); VIS[K]=1; Ans-=V[k];  for(intI=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;}
Code

2015ACM/ICPC Asia Regional Changchun online/hdu 5438 map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.