ACM/ICPC Asia Regional Changchun Online Pro 1002 (topological sorting + and collection)

Source: Internet
Author: User

Ponds

Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 0 Accepted Submission (s): 0


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, numbers a and 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 First Save the side, the degree calculated, according to the topological sequence of the deletion point (a single point to note, here WA once), and then scan over the edge, there is a deletion of the point is ignored, using and check set maintenance connected components of the node and the total weight value.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e4+5, MAXM = 2e5+Ten;intn,m;intVAL[MAXN];intHEAD[MAXN],NXT[MAXM],TO[MAXM];intdeg[maxn],ecnt;BOOLRMVD[MAXN];voidAddedge (intUintv) {to[ecnt]=v; NXT[ECNT]=Head[u]; Head[u]= ecnt++; Deg[u]++;}voidtopo () {Queue<int>Q;  for(inti =1; I <= N; i++){        if(Deg[i] <=1) {Rmvd[i]=true;        Q.push (i); }    }     while(Q.size ()) {intU =Q.front (); Q.pop ();  for(inti = Head[u]; ~i; i =Nxt[i]) {            intv =To[i]; if(!rmvd[v] &&--deg[v] = =1) {Q.push (v); Rmvd[v]=true; }        }    }}Long LongSUM[MAXN];intPA[MAXN],CNT[MAXN];intFDST (intx) {returnx==pa[x]?x:pa[x]=FDST (pa[x]);}intMain () {//freopen ("In.txt", "R", stdin);    intT scanf"%d",&T);  while(t--) {scanf ("%d%d",&n,&m);  for(inti =1; I <= N; i++) scanf ("%d", val+i); memset (Head,-1,sizeof(head)); memset (deg,0,sizeof(deg)); memset (RMVD,0,sizeof(RMVD)); ECNT=0;  for(inti =0; I < m; i++){            intU,v; scanf"%d%d",&u,&v); Addedge (U,V);        Addedge (V,u);        } topo ();  for(inti =1; I <= N; i++) Pa[i] = i,sum[i] = Val[i],cnt[i] =1;  for(inti =0, M =2*m; i < M; i + =2){            intU = to[i], v = to[i^1]; if(!rmvd[u] &&!Rmvd[v]) {                intA = FDST (u), B =Fdst (v); if(A! =b) {Pa[a]=b; SUM[B]+=Sum[a]; CNT[B]+=Cnt[a]; }            }        }        Long LongAns =0;  for(inti =1; I <= N; i++){            intf =FDST (i); if(!Rmvd[f]) {                if(cnt[f]&1) {ans+=Sum[f]; } Rmvd[f]=true; }} printf ("%i64d\n", ans); }    return 0;}

ACM/ICPC Asia Regional Changchun Online Pro 1002 (topological sorting + and collection)

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.