比赛 NOI2015Day1 评测结果 AWWWWEWEEE
题目名称 程序自动分析 最终得分 10
用户昵称 FoolMike 运行时间 0.169 s
代码语言 Pascal 内存使用 5.51 MiB
提交时间 2015-08-01 12:58:33
显示代码纯文本
var
n,t,g,b,a,c:longint;
f2,i,j,e:array[1..100000]of longint;
f1:array[1..1000,0..1000]of longint;
ans:string;
begin
assign(input,'prog.in');
reset(input);
assign(output,'prog.out');
rewrite(output);

read(t);
for t:=1 to t do
  begin
  read(n);
  ans:='YES';g:=0;a:=0;
  for n:=1 to n do
    begin
    read(i[n],j[n],e[n]);
    f2[i[n]]:=0;
    f2[j[n]]:=0;
    if e[n]=1 then
      begin
      inc(a);
      b:=i[n];i[n]:=i[a];i[a]:=b;
      b:=j[n];j[n]:=i[a];j[a]:=b;
      b:=e[n];e[n]:=i[a];e[a]:=b;
      end;
    end;
  for a:=1 to a do
    begin
    if (f2[i[a]]=0)and(f2[j[a]]=0) then
      begin
      inc(g);
      f2[i[a]]:=g;
      f1[g,1]:=i[a];
      f2[j[a]]:=g;
      f1[g,1]:=j[a];
      f1[g,0]:=2;
      end
    else
    if (f2[i[a]]<>0)and(f2[j[a]]=0) then
      begin
      f2[j[a]]:=f2[i[a]];
      inc(f1[f2[i[a]],0]);
      f1[f2[i[a]],f1[f2[i[a]],0]]:=j[a];
      end
    else
    if (f2[i[a]]=0)and(f2[j[a]]<>0) then
      begin
      f2[i[a]]:=f2[j[a]];
      inc(f1[f2[j[a]],0]);
      f1[f2[j[a]],f1[f2[j[a]],0]]:=i[a];
      end
    else
    if (f2[i[a]]<>0)and(f2[j[a]]<>0) then
      begin
      c:=f2[j[a]];
      for b:=1 to f1[c,0] do
        begin
        inc(f1[f2[i[a]],0]);
        f1[f2[i[a]],f1[f2[i[a]],0]]:=f1[c,b];
        f2[f1[c,b]]:=f2[i[a]];
        end;
      f1[c,0]:=0;
      end;
    end;
  for b:=a+1 to n do
  if f2[i[b]]=f2[j[b]] then
    begin
    ans:='NO';
    break;
    end;
  writeln(ans);
  end;

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