1601:war time limit: 1 Sec Memory Limit: MB
Submit: Solved: 24
[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
#include <stdio.h>const int N = 100005; int Fath[n],a[n],b[n],k[n],vist[n],ans; int findroot (int x) {if (x!=fath[x]) fath[x]=findroot (fath[x]); return fath[x];} void Setroot (int x,int y) {x=findroot (x); Y=findroot (y); if (x!=y) {fath[x]=y; ans--; }}int Main () {int q,n,m; while (scanf ("%d%d", &n,&m) >0) {for (int i=1;i<=n;i++) fath[i]=i; Ans=n; for (int i=1;i<=m;i++) scanf ("%d%d", &a[i],&b[i]), vist[i]=0; scanf ("%d", &q); for (int i=1;i<=q;i++) {scanf ("%d", &k[i]); vist[k[i]]=1; } for (int i=1;i<=m;i++) if (vist[i]==0) Setroot (A[i],b[i]); for (int i=q;i>0;i--) {int tt=k[i]; K[i]=ans; Setroot (A[tt],b[tt]); } for (int i=1;i<=q;i++) {printf ("%d", k[i]); if (i!=q) printf (""); } printf ("\ n"); }}/************************************************************** problem:1601 user:aking2015 language:c++ result:accepted time:352 Ms memory:2916 kb****************************************************************/
Csu1601:war (and collection)