1601:war time limit: 1 Sec Memory Limit: MB
Submit: Solved: 38
[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
Title Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1601
Main topic: N Points M Edge (side 1, Side 2 ... Side m), q a side to be destroyed, in order to destroy the number of connected blocks in the graph after each edge
Topic Analysis: Offline and check set, reverse add edge, first the number of connected blocks after the Q road is destroyed, and then add edge can, each plus one side, if two points not in the same connected block, then merge and the number of connected blocks minus 1
#include <cstdio> #include <cstring>int const MAX = 1e5 + 5;int Fa[max], N, M;int X[max], Y[max], r[max];int an S, Res[max];bool vis[max];void Uf_set () {for (int i = 0; I <= N; i++) fa[i] = i;} int Find (int x) {return x = = Fa[x]? x:fa[x] = Find (Fa[x]);} void Union (int a, int b) {int r1 = find (a), int r2 = find (b), if (r1! = r2) {FA[R2] = R1;ans--;}} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {uf_set (); ans = N;memset (Vis, false, sizeof (VIS)); for (int i = 1; i <= m; i++) scanf ("%d%d", &x[i], &y[i]), int q;scanf ("%d", &q), for (int i = 1; I <= Q; i++) {scanf ("%d", &r[i]); v Is[r[i]] = true;} for (int i = 1; I <= m; i++) if (!vis[i]) Union (X[i], y[i]); for (int i = q; I >= 1; i--) {res[i] = ans; Union (X[r[i]], y[r[i]);} for (int i = 1; i < q; i++) printf ("%d", Res[i]);p rintf ("%d\n", Res[q]);}}
Csuoj 1601 War (offline and check to find the number of connected blocks)