CSU 1601 War

Source: Internet
Author: User

1601:war time limit:1 Sec Memory limit:128 MB
submit:127 solved:36
[Submit] [Status] [Web Board] Description

AME decided to destroy CH ' s country. In CH ' country, there is N villages, which is numbered from 1 to N. We say village A and B are connected, if and only if there is a road between A and B, or there exists a village C such That there was a road between A and C, and C and B are connected. To defend the country from the attack of AME, CH have decided to build some roads between some villages. Let us say, that, villages belong to the same garrison area if they is connected.
Now AME have already worked out the overall plan including which road and in which order would be attacked and destroyed. CH wants to know the number of garrison areas in he country after each of AME ' s attack.

Input

The first line contains the integers N and m-the number of villages and roads, (2≤n≤100000; 1≤m≤100000). Each of the next M lines contains-different integers u, V (1<=u, v<=n)-which means there is a road between U and V. The next line contains a integer Q which denotes the quantity of roads AME wants to destroy (1≤Q≤M). The last line contains a series of numbers all of which denoting a road as its order of appearance-different integers s Eparated by spaces.

Output

Output Q integers-the number of garrison areas in CH ' s country after each of AME ' s attack. Each pair of numbers is separated by a single space.

Sample Input
3 11 2114 41 22 31 33 432 4 3
Sample Output
31 2 3
HINT Source

Test instructions: Give n points, M edge, given Q times query, ask after each query there are several independent connected blocks. Analysis: The first algorithm is to use and check the set to find out there are several connected blocks, and then each time you delete the edge to see if the end of the degree is 0, according to this to count, but so write a few hair is WA, also did not find out what the bug, passing the great God is interested can and I discuss the next AH ~ later on is to use and check the set to find out the last number of connected blocks, and then forward from the back side.
#include <cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<cmath>#include<queue>#include<algorithm>using namespacestd;Const intmaxn=100000+5;intP[MAXN],U[MAXN],V[MAXN],VIS[MAXN],W[MAXN],CNT[MAXN];intN,m,q,ans;intFINDFA (intx) {    returnp[x]==x?x:p[x]=Findfa (p[x]);}voidBinintXxintyy) {    intx=Findfa (XX); inty=Findfa (yy); if(x!=y) {p[x]=y; Ans--; }}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {ans=N; memset (Vis,0,sizeof(VIS));  for(intI=1; i<=m;i++) scanf ("%d%d",&u[i],&V[i]); scanf ("%d",&Q);  for(intI=1; i<=q;i++) {scanf ("%d",&W[i]); Vis[w[i]]=1; }         for(intI=1; i<=n;i++) p[i]=i;  for(intI=1; i<=m;i++)            if(vis[i]==0) Bin (u[i],v[i]);  for(intI=q;i>0; i--) {Cnt[i]=ans;        Bin (U[w[i]],v[w[i]]); }         for(intI=1; i<=q;i++)        {            if(i==q) printf ("%d\n", Cnt[i]); Elseprintf"%d", Cnt[i]); }    }    return 0;}
View Code

CSU 1601 War

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.