记录编号 39316 评测结果 WEEEEEEEEE
题目名称 硬币收集者 最终得分 0
用户昵称 GravatarSnowDancer 是否通过 未通过
代码语言 Pascal 运行时间 0.045 s
提交时间 2012-07-08 21:33:26 内存使用 0.52 MiB
显示代码纯文本
program coinmn;
type
  circlenode=array[0..300,1..2] of longint;
var
  n,i,j,k,l,ans,f1,f2,p,head,tail,top,fa:longint;
  e:array[1..300,1..2,1..2] of longint;
  f:array[1..300] of longint;
  h:array[1..10000] of longint;
  node:array[1..10000] of record code,id,x,next:longint; end;
  q:array[1..10000] of longint;
  pre:array[1..10000] of record code,id,x:longint;end;
  visit:array[1..10000] of boolean;
  pass:array[1..300,1..2] of boolean;
  findy:boolean;
function find(k:longint):longint;
  begin
    if f[k]=0 then exit(k);
    f[k]:=find(f[k]);exit(f[k]);
  end;
procedure insertE(code,id:longint);
  begin
    inc(top); node[top].code:=code;
    node[top].id:=id; node[top].x:=e[code][id][2];
    node[top].next:=h[e[code][id][1]];
    h[e[code][id][1]]:=top;
    inc(top); node[top].code:=code;
    node[top].id:=id; node[top].x:=e[code][id][1];
    node[top].next:=h[e[code][id][2]];
    h[e[code][id][2]]:=top;
  end;
procedure deleteE(code,id:longint);
  begin
    p:=h[e[code][id][1]];
    while p<>0 do begin
      if node[p].x=e[code][id][2] then begin
        node[fa].next:=node[p].next;
        break;
      end;
      fa:=p; p:=node[p].next;
    end;
    p:=h[e[code][id][2]];
    while p<>0 do begin
      if node[p].x=e[code][id][1] then begin
        node[fa].next:=node[p].next;
        break;
      end;
      fa:=p; p:=node[p].next;
    end;
  end;
procedure findcircle(x,y:longint;var c:circlenode);
  var
    i:longint;
  begin
    fillchar(visit,sizeof(visit),false);
    fillchar(c,sizeof(c),0);
    head:=0; tail:=1; q[1]:=x; visit[x]:=true; findy:=false;
    repeat
      inc(head);
      p:=h[q[head]];
      while p<>0 do begin
        if not visit[node[p].x] then begin
          inc(tail); i:=node[p].x;
          q[tail]:=i;
          visit[i]:=true;
          pre[i].code:=node[p].code;
          pre[i].id:=node[p].id;
          pre[i].x:=q[head];
          if i=y then findy:=true;
        end;
        if findy then break;
        p:=node[p].next;
      end;
      if findy then break;
    until false;
    p:=y;  c[0][1]:=0;
    while pre[p].x<>x do begin
      inc(c[0][1]);
      c[c[0][1]][1]:=pre[p].code;
      c[c[0][1]][2]:=pre[p].id;
      p:=pre[p].x;
    end;
  end;
function DFS(code,id:longint):boolean;
  var
    i:longint;
    c:circlenode;
  begin
    pass[code,id]:=true; DFS:=false;
    f1:=find(e[code][id][1]);f2:=find(e[code][id][2]);
    if f1<>f2 then begin
      f[f1]:=f2;
      insertE(code,id);
      exit(true);
    end else begin
      findcircle(e[code][id][1],e[code][id][2],c);
      for i:=1 to c[0][1] do
        if not pass[c[i][1],3-c[i][2]] and (DFS(c[i][1],3-c[i][2])) then begin
          insertE(code,id);
          deleteE(c[i][1],c[i][2]);
          exit(true);
        end;
    end;
  end;
begin
assign(input,'coinmn.in');reset(input);
assign(output,'coinmn.out');rewrite(output);
repeat
  readln(n);
  if n=0 then exit;
  top:=0; ans:=0;
  fillchar(h,sizeof(h),0);
  fillchar(f,sizeof(f),0);
  for i:=1 to n do
    for j:=1 to 2 do
      for k:=1 to 2 do begin
        read(e[i,j,k]);
        inc(e[i,j,k]);
      end;
  for i:=1 to n do begin
    fillchar(pass,sizeof(pass),false);
    if DFS(i,1) then begin
      inc(ans);continue;
    end;
    fillchar(pass,sizeof(pass),false);
    if DFS(i,2) then begin
      inc(ans);continue;
    end;
  end;
  writeln(ans<<1);
until false;
close(input); close(output);
end.