Description
很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系。某一天,凭着一个偶然的
机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球。这些星球通过特殊的以太隧道互相直
接或间接地连接。 但好景不长,很快帝国又重新造出了他的超级武器。凭借这超级武器的力量,帝国开始有计划
地摧毁反抗军占领的星球。由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来。现在,反抗军首
领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽量快的速度求出每
一次打击之后反抗军占据的星球的连通快的个数。(如果两个星球可以通过现存的以太通道直接或间接地连通,则
这两个星球在同一个连通块中)。
Input
输入文件第一行包含两个整数,N (1 < = N < = 2M) 和M (1 < = M < = 200,000),分别表示星球的
数目和以太隧道的数目。星球用 0 ~ N-1的整数编号。接下来的M行,每行包括两个整数X, Y,其中(0 < = X <>
Y 表示星球x和星球y之间有“以太”隧道,可以直接通讯。接下来的一行为一个整数k,表示将遭受攻击的星球的
数目。接下来的k行,每行有一个整数,按照顺序列出了帝国军的攻击目标。这k个数互不相同,且都在0到n-1的范
围内。
Output
输出文件的第一行是开始时星球的连通块个数。接下来的N行,每行一个整数,表示经过该次打击后现存星球
的连通块个数。
Sample Input
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7
Sample Output
1
1
2
3
3
HINT
Source
这是当年的一道考试题,真的是too young too simple orz;
正着想去拆边并没有什么有效的方法和算法,也木有什么规律
于是,学长教我们做人:
”一定要记住一种思想:正难则反,一定要记住!非常的重要!“
然后,我们倒着想,就变成了每次加一个点和加边来维护联通性,显然并查集就可以完成这个任务;
注意题目要求是没有被攻击的点所构成的连通块 (论读题的重要性)
每次还原一个星球,连通块+1
var
n,m,t,l,x,y :longint;
i :longint;
last,f :array[0..400010] of longint;
pre,other :array[0..400010] of longint;
ans :array[0..400010] of longint;
att :array[0..400010] of boolean;
ord :array[0..400010] of longint;
function get_father(x:longint):longint;
begin
if x=f[x] then exit(x);
f[x]:=get_father(f[x]);
exit(f[x]);
end;
procedure connect1(x,y:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
end;
procedure connect2(a,b,tt:longint);
begin
a:=get_father(a);
b:=get_Father(b);
if (a<>b) then
begin
f[b]:=a;
dec(ans[tt]);
end;
end;
procedure merge(x,tt:longint);
var
p,q:longint;
begin
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if not att[p] then connect2(x,p,tt);
q:=pre[q];
end;
end;
begin
read(n,m);
for i:=1 to m do
begin
read(x,y);
inc(x);inc(y);
connect1(x,y);
connect1(y,x);
end;
read(t);
for i:=1 to t do
begin
read(ord[i]);
inc(ord[i]);
att[ord[i]]:=true;
end;
//
for i:=1 to n do f[i]:=i;
ans[t+1]:=n-t;
for i:=1 to n do if not att[i] then merge(i,t+1);
//
for i:=t downto 1 do
begin
att[ord[i]]:=false;
ans[i]:=ans[i+1]+1;
merge(ord[i],i);
end;
for i:=1 to t+1 do writeln(ans[i]);
end.
——by Eirlys