题意:n个人,朋友的朋友是我的朋友,敌人的敌人是我的朋友,互为朋友的组成一个团伙,问团伙的数量
并查集入门题
把一个人拆成两个,x表示朋友集合,n+x表示敌人集合
朋友的朋友是我的朋友:直接合并x和y
敌人的敌人是我的朋友:合并x和n+y ,合并 y 和n+1
var
n,m,x,y,ans :longint;
vis :array[0..2010] of boolean;
f :array[0..2010] of longint;
i :longint;
ch :char;
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 connect(x,y:longint);
begin
x:=get_father(x);
y:=get_father(y);
if (x<>y) then f[x]:=y;
end;
begin
read(n);
for i:=1 to 2*n do f[i]:=i;
readln(m);
for i:=1 to m do
begin
read(ch); readln(x,y);
if ch='F' then connect(x,y) else
begin
connect(x,n+y);
connect(y,n+x);
end;
end;
ans:=0;
for i:=1 to n do
begin
x:=get_father(i);
if not vis[x] then
begin
inc(ans);
vis[x]:=true;
end;
end;
writeln(ans);
end.
——by Eirlys