| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
0ms并查集AC无压力~代码:
Program poj1308;//By_Thispoet
Const
maxn=1000;
Var
i,j,k,m,n,p,q :Longint;
father :Array[1..maxn]of Longint;
v :Array[1..maxn]of Boolean;
flag :Boolean;
Function Max(i,j:Longint):Longint;
begin
if i>j then exit(i);exit(j);
end;
Function Root(i:Longint):Longint;
begin
if father[i]=i then exit(i);
father[i]:=Root(father[i]);
exit(father[i]);
end;
Procedure Union(i,j:Longint);
var x,y:Longint;
begin
x:=Root(i);y:=Root(j);
if x=y then flag:=false else
father[x]:=y;
end;
BEGIN
read(p,q);m:=0;
while not ((p=-1)and(q=-1)) do
begin
inc(m);n:=0;
fillchar(v,sizeof(v),0);
for i:=1 to maxn do father[i]:=i;
flag:=true;
while not ((p=0)and(q=0)) do
begin
n:=Max(n,p);n:=Max(n,q);
v[p]:=true;v[q]:=true;
Union(p,q);
read(p,q);
end;
for i:=1 to n do if v[i] then if Root(i)<>Root(n) then flag:=false;
if flag then writeln('Case ',m,' is a tree.') else
writeln('Case ',m,' is not a tree.');
read(p,q);
end;
END.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator