比赛 20120712 评测结果 AAAAAAAAAA
题目名称 爆炸化合物 最终得分 100
用户昵称 IMSL77 运行时间 0.191 s
代码语言 Pascal 内存使用 0.58 MiB
提交时间 2012-07-12 11:56:45
显示代码纯文本
program explosion;
type
  integer=longint;
const
  maxn=110000;
var
  father:array[0..maxn] of integer;

  procedure Fopen;
  begin
    assign(input,'explosion.in'); reset(input);
    assign(output,'explosion.out'); rewrite(output);
  end;

  procedure Fclose;
  begin
    close(input); close(output);
  end;

  function getfather(x:integer):integer;
  begin
    if father[x]=x then exit(x);
    father[x]:=getfather(father[x]);
    exit(father[x]);
  end;

  procedure Solve;
  var
    i,p,q:integer;
    fx,fy:integer;
    ans:integer;
  begin
    for i:=0 to maxn do father[i]:=i;
    ans:=0;
    repeat
      read(p); if p=-1 then break;
      read(q);
      fx:=getfather(p); fy:=getfather(q);
      if fx=fy then inc(ans) else father[fx]:=fy;
    until false;
    writeln(ans);
  end;

begin
  Fopen;
  Solve;
  Fclose;
end.