IOIBIN – spoj

Đề bài:

Thuật toán:

Code:

const   fi = '';
        fo = '';
        maxn = trunc(1e4)+3;
var     p : array[1..maxn] of longint;
        i,j,n,m : longint;
        x,u,v : longint;
procedure chuanbi;
begin
        for i:=1 to maxn do p[i]:=i;
end;
function find(i:longint):longint;
begin
        while p[i]<>i do i:=p[i];
        exit(i);
end;
procedure lam1;
var     i,j : longint;
begin
        i:=find(u);
        j:=find(v);
        if i<>j then
        if i<j then p[j]:=p[i] else p[i]:=p[j];
end;
procedure main;
var     i:longint;
begin
        assign(input,fi);reset(input);
        assign(output,fo);rewrite(output);
        readln(m);
        chuanbi;
 
        for i:=1 to m do
                begin
                        readln(u,v,x);
                        if x=1 then lam1 else
                                if find(u)=find(v) then writeln(1) else writeln(0);
                end;
        close(input);
        close(output);
end;
begin
        main;
end.
Khuyên dùng

 

Speak Your Mind

*