比赛 20120708 评测结果 AAAATTTTTT
题目名称 硬币收集者 最终得分 40
用户昵称 zhangchi 运行时间 12.139 s
代码语言 Pascal 内存使用 0.21 MiB
提交时间 2012-07-08 09:46:12
显示代码纯文本
var
  n,i,j,ans,time:longint;
  p:boolean;
  x,y:array[1..300,1..2] of longint;
  fa:array[0..10000] of longint;
  function find(x:longint):longint;
  begin
    if fa[x]=x then exit(x)
      else exit(find(fa[x]));
  end;
  procedure dfs(k,num:longint);
  var
    i,j,temp:longint;
  begin
    if k=n+1 then
      begin
        if num>ans then ans:=num;
        exit;
      end;
    inc(time);
    if time>10000000 then
      begin
        p:=false;
        exit;
      end;
    dfs(k+1,num);
    if p=false then exit;
    i:=find(x[k,1]);
    j:=find(y[k,1]);
    if i<>j then
      begin
        temp:=fa[i];
        fa[i]:=j;
        dfs(k+1,num+1);
        if p=false then exit;
        fa[i]:=temp;
      end;
    i:=find(x[k,2]);
    j:=find(y[k,2]);
    if i<>j then
      begin
        temp:=fa[i];
        fa[i]:=j;
        dfs(k+1,num+1);
        if p=false then exit;
        fa[i]:=temp;
      end;
  end;
begin
  assign(input,'coinmn.in'); reset(input);
  assign(output,'coinmn.out'); rewrite(output);
  readln(n);
  while n<>0 do
    begin
      time:=0; p:=true; ans:=0;
      for i:=1 to n do
        readln(x[i,1],y[i,1],x[i,2],y[i,2]);
      for i:=0 to 10000 do
        fa[i]:=i;
      dfs(1,0);
      writeln(ans*2);
      readln(n);
    end;
  close(input); close(output);
end.