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

pascal

Posted by zhouzixuan at 2014-06-20 21:51:48 on Problem 1182
var
  father,dis:array [0..50000] of longint;
  n,m,ans:longint;
procedure init;
var
  i:longint;
begin
  readln(n,m); ans:=0;
  for i:=1 to n do father[i]:=i;
  for i:=1 to n do dis[i]:=0;
end;
function getfather(x:longint):longint;
var
  pre:longint;
begin
  if father[x]=x then exit(x);
  pre:=getfather(father[x]);
  inc(dis[x],dis[father[x]]);
  dis[x]:=dis[x] mod 3;
  father[x]:=pre; exit(pre);
end;
function union(x,y,z:longint):longint;
var
  xx,yy:longint;
begin
  xx:=getfather(x); yy:=getfather(y);
  if xx=yy then
  begin
    if (z=1) and (dis[x]=dis[y]) then exit(0);
    if (z=2) and (dis[x]=2) and (dis[y]=0) then exit(0);
    if (z=2) and (dis[x]=0) and (dis[y]=1) then exit(0);
    if (z=2) and (dis[x]=1) and (dis[y]=2) then exit(0);
    exit(1);
  end;
  if dis[x]=1 then dis[xx]:=2 else if dis[x]=2 then dis[xx]:=1;
  if dis[y]=1 then dis[yy]:=2 else if dis[y]=2 then dis[yy]:=1;
  father[xx]:=x; father[yy]:=y; father[x]:=y; father[y]:=y; dis[y]:=0;
  if z=1 then dis[x]:=0 else dis[x]:=2; exit(0);
end;
procedure main;
var
  i,x,y,z:longint;
begin
  for i:=1 to m do
  begin
    readln(z,x,y);
    if (z=2) and (x=y) then begin inc(ans); continue; end;
    if (x>n) or (y>n) then begin inc(ans); continue; end;
    ans:=ans+union(x,y,z);
  end;
  writeln(ans);
end;
procedure openfile;
begin
  assign(input,'1.in');
  reset(input);
  assign(output,'1.out');
  rewrite(output);
end;
begin
  //openfile;
  init;
  main;
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