Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

0ms并查集AC无压力~

Posted by This_poet at 2011-10-08 19:17:13 on Problem 1308
代码:
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator