ac1208.pas

来自「某牛人写的acm.tongji.edu.cn上大部分ac的代码,仅供学习研究,请」· PAS 代码 · 共 43 行

PAS
43
字号
program tju1208;
const
  maxn=50000;
  kinds=3;
var
  root:array[1..maxn]of word;
  rel:array[1..maxn]of byte;
  n,k,i,lies,d,x,y:longint;
procedure pathcomp(x:word);
  var
    r,t,d:word;
  begin
    r:=x;d:=0;while root[r]<>r do begin
      inc(d,rel[r]);r:=root[r];
    end;
    d:=d mod kinds;
    while root[x]<>r do begin
      t:=rel[x];rel[x]:=d;d:=(d-t+kinds) mod kinds;
      t:=root[x];root[x]:=r;x:=t;
    end;
  end;
begin
  repeat
    read(n,k);lies:=0;
    for i:=1 to n do begin
      root[i]:=i;rel[i]:=0;
    end;
    for i:=1 to k do begin
      read(d,x,y);
      if (x>n) or (y>n) or (d=2) and (x=y) then begin inc(lies);continue;end;
      if (d=1) and (x=y) then continue;
      pathcomp(x);pathcomp(y);
      if root[x]<>root[y] then begin
        rel[root[y]]:=(ord(d=2)+rel[x]-rel[y]+kinds) mod kinds;
        root[root[y]]:=root[x];
      end
      else if (d=1) and (rel[x]<>rel[y]) or
              (d=2) and ((rel[y]-rel[x]+kinds) mod kinds<>1) then inc(lies);
    end;
    writeln(lies);
  until seekeof;
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?