记录编号 232630 评测结果 AAAAAAAAAA
题目名称 [NOI 2015]程序自动分析 最终得分 100
用户昵称 GravatarMarvolo 是否通过 通过
代码语言 Pascal 运行时间 1.984 s
提交时间 2016-03-02 13:02:35 内存使用 15.70 MiB
显示代码纯文本
program zht;
var
i,n,t,s,bh,q,w:longint;
x,y,p:array[0..110000] of longint;
h,f:array[0..2100000] of longint;

procedure hash(x:longint);
begin
s:=x mod 2000007;
while (h[s]<>0) and (h[s]<>x) do
inc(s);
h[s]:=x;
end;

procedure find(x:longint);
begin
if f[x]=x then begin s:=x; exit; end
else begin find(f[x]); f[x]:=s; end;
end;

begin
assign(input,'prog.in');
assign(output,'prog.out');
reset(input);
rewrite(output);

readln(t);

for t:=1 to t do
begin
fillchar(h,sizeof(h),0);
readln(n);

for i:=1 to n do
begin
readln(x[i],y[i],p[i]);
hash(x[i]);
x[i]:=s;
hash(y[i]);
y[i]:=s;
end;

for i:=1 to 2000010 do
f[i]:=i;

for i:=1 to n do
begin
if p[i]=0 then continue;
find(x[i]);
q:=s;
find(y[i]);
w:=s;
f[q]:=w;
end;
bh:=0;

for i:=1 to n do
begin
if p[i]=1 then continue;
find(x[i]);
q:=s;
find(y[i]);
w:=s;
if q=w then begin bh:=1; writeln('NO'); break; end;
end;

if bh=0 then writeln('YES');

end;
close(input);
close(output);
end.