1015: [JSOI2008] Star Wars Starwar time limit:3 Sec Memory limit:162 MB
submit:7336 solved:3448
[Submit] [Status] [Discuss] Description
Long ago, in a distant galaxy, a dark empire was leaning against its super-weapons rulers throughout the galaxy. One day, with an accidental
Opportunity, a rebel army destroyed the empire's super weapons and attacked almost all the planets in the galaxy. These planets are directly connected to each other through special etheric tunnels.
Connected to or indirectly. But it was not long, and soon the Empire re-created his super weapon. With the power of this super weapon, the Empire began to have plans
To destroy the rebel-occupied planet. As the planet continues to be destroyed, communication channels between the two planets are beginning to be unreliable. Now, the rebel army's first
I'll give you a mission: provide an Ethernet tunnel connection between the original two planets and the Order of the empire-hit planets to find out every
The number of connected fast planets that the rebels occupy after a strike. (If two planets can be connected directly or indirectly through the existing etheric channels,
The two planets are in the same connected block).
Input
The first line of the input file contains two integers, n (1 < = n < = 2M) and m (1 < = m < = 200,000), respectively, representing the planet's
Number and number of Ethernet tunnels. The planet is numbered with an integer of 0 ~ N-1. The next M-line, each line consists of two integers x, Y, where (0 < = X <>
Y means that there is an "ether" tunnel between Planet X and Planet Y that can communicate directly. The next behavior is an integer k, which indicates that the planet that will be attacked
Number. The next K-line, each line has an integer, in order to list the attack target of the imperial army. The number of k is different, and all the 0 to n-1
In the surrounding.
Output
The first line is the number of connected blocks at the beginning of the planet. The next K-line, an integer for each line, indicates the existing planet after the strike
Number of connected blocks.
Sample Input8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
9 |
7 6
3 6
5
1
6
3
5
7Sample Output1
1
1
2
3
the first thing to see is to seek the Unicom block, you will think and check set, and then found to do completely unable to maintain, so consider the insertion of the point of destruction,when you insert point now[i], it means that now[i + 1].....now[k] has been inserted (not yet destroyed), and Now[1]........now[i-1]has been inserted (destroyed), so you can use and check set, to maintain the unicom block.
#include <bits/stdc++.h>using namespacestd;Const intMAXN =400005;intN, M, Fa[maxn], K, VIS[MAXN], ANS[MAXN];intHEAD[MAXN], cnt =1, tot =0, NOW[MAXN];structnode{intV, NXT;} G[MAXN];voidInsertintUintv) {g[cnt]= (Node) {V, head[u]}; Head[u] = cnt++;}intFindintx) {returnx = = Fa[x]? x:fa[x] =find (Fa[x]);}voidMergeintR1,intR2) { intK1 = Find (r1), K2 =find (R2); if(K1! = K2) Fa[k1] = K2, tot--;}intMain () {scanf ("%d%d", &n, &m); for(inti =1; I <= m; ++i) {intx, y; scanf"%d%d", &x, &y); X+ +, y++; Insert (x, y); Insert (y, x); } for(inti =1; I <= N; ++i) Fa[i] =i; scanf ("%d", &k); for(inti =1; I <= K; ++i) {scanf ("%d", &now[i]); now[i]++; Vis[now[i]] =1; } tot= N-K; for(inti =1; I <= N; ++i) {if(!Vis[i]) { for(intj = Head[i]; J j =g[j].nxt) { intv =g[j].v; if(!Vis[v]) merge (I, v); }}} Ans[k]=tot; for(inti = k; I >=1; --i) {Vis[now[i]]=0; tot++; for(intj = Head[now[i]]; J j =g[j].nxt) { intv =g[j].v; if(!Vis[v]) merge (Now[i], V); } ans[i-1] =tot; } for(inti =0; I <= K; ++i) printf ("%d\n", Ans[i]); return 0;}
Summary and Reflection: Encounter connected block, should consider and check set, it is difficult to reflect on thinking
Bzoj 1015: [JSOI2008] Star Wars Starwar