比较笨啊,一直在想,到底问几次绝对能知道所有的关系呢?
后来看了题解才知道,问一次最少确定一对关系…………
这就好办le,n头牛有C(2,n)个关系
现在给出m条边,以确定的关系有多少呢?直接dfs啊……
……O(nm)
type link=^node;
node=record
po:longint;
next:link;
end;
var w:array[..] of link;
sum:array[..] of longint;
v:array[..] of boolean;
n,i,m,ans,x,y:longint; procedure add(x,y:longint);
var p:link;
begin
new(p);
p^.next:=w[x];
p^.po:=y;
w[x]:=p;
end; procedure dfs(i:longint);
var p:link;
y:longint;
begin
p:=w[i];
v[i]:=true;
sum[i]:=;
while p<>nil do
begin
y:=p^.po;
if not v[y] then
begin
dfs(y);
sum[i]:=sum[i]+sum[y];
end;
p:=p^.next;
end;
end; begin
readln(n,m);
for i:= to m do
begin
readln(x,y);
add(x,y);
end;
for i:= to n do
begin
fillchar(v,sizeof(v),false);
dfs(i);
ans:=ans+sum[i]-;
end;
writeln(n*(n-) div -ans);
end.