记录编号 113930 评测结果 AAAAAAAAAA
题目名称 [焦作一中2012] 玻璃球游戏 最终得分 100
用户昵称 Gravatar传奇 是否通过 通过
代码语言 Pascal 运行时间 1.893 s
提交时间 2014-07-26 17:08:31 内存使用 16.92 MiB
显示代码纯文本
program cojs1070;
const
  maxn=1000000;
type
  node=record
    w,x:longint;
  end;
var
  f,ft:array[1..maxn] of longint;
  a:array[1..maxn] of node;
  ans:array[1..maxn] of longint;
  d,huan:array[1..maxn] of boolean;
  i,j,k,m,n,q,t:longint;
  x,y,p:longint;
procedure find(x:longint);
var
  p,t,count,k:longint;
begin
    if huan[x] then exit;
  if f[x]=0 then exit;
  if f[f[x]]=0 then exit;
  p:=x;
  while (f[p]<>0) and d[p] do
    begin
	  d[p]:=false;
	  p:=f[p];
	  if huan[p] then break;
	end;
  count:=0;
  if not d[p] and not huan[p] then
    while f[p]<>0 do
      begin
	    p:=f[p]; inc(count);
		if count>1000 then break;
      end;
  if f[p]=0 then
    begin
	  t:=x;
	  if f[t]<>0 then
	    begin
		 { k:=t;
		  t:=f[t];
		  f[k]:=p;}
		  f[t]:=p; t:=f[t];
		end;
	  exit;
    end;
  t:=x;
  while not huan[t] do
    begin
	  huan[t]:=true;
	  t:=f[t];
    end;
end;
function find2(x:longint):longint;
var
  p,t,k:longint;
begin
  if huan[x] then exit(0);
  if f[x]=0 then exit(x);
  if f[f[x]]=0 then exit(f[x]);
  p:=x;
  while f[p]<>0 do
    begin
	  p:=f[p];
	  if huan[p] then break;
	end;
  if f[p]=0 then
  begin
    t:=x;
	if f[t]<>0 then
      begin
	   { k:=t;
		t:=f[t];
		f[k]:=p;}
		f[t]:=p; t:=f[t];
      end;
    exit(p);
  end;
  t:=x;
  while not huan[t] do
    huan[t]:=true;
  exit(0);
end;
begin
  assign(input,'marbles.in');
  assign(output,'marbles.out');
  reset(input);
  rewrite(output);

  readln(n);
  for i:=1 to n do
    begin
	  read(k);
	  f[i]:=k;
	  ft[i]:=f[i];
	end;
  readln(q);
  for i:=1 to q do
    begin
	  readln(a[i].w,a[i].x);
	  if a[i].w=2 then
	    f[a[i].x]:=0;
	end;
  fillchar(d,sizeof(d),true);
  fillchar(huan,sizeof(huan),false);
  for i:=1 to n do
    if d[i] then
	  find(i);
  t:=0;
  for i:=q downto  1 do
  begin
    case a[i].w of
	  1:begin
	      inc(t);
		  ans[t]:=find2(a[i].x);
	    end;
	  2:begin
	      x:=find2(a[i].x);
		  y:=find2(ft[a[i].x]);
		  if x=y then
		    begin
			  huan[a[i].x]:=true;
			  p:=ft[a[i].x];
			  while not huan[f[p]] do
			    begin
				  huan[p]:=true;
				  p:=f[p];
				end;
			end
		  else
            f[a[i].x]:=ft[a[i].x];
        end;	
	end;
  end;
  for i:=t downto 1 do
    if ans[i]=0 then
	  writeln('CIKLUS')
	else
	  writeln(ans[i]);

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