比赛 20130729 评测结果 AAAAAAAATA
题目名称 奶牛的糖果 最终得分 90
用户昵称 呵呵 运行时间 1.362 s
代码语言 Pascal 内存使用 1.31 MiB
提交时间 2014-07-17 10:52:02
显示代码纯文本
    var
    a,b,d:array[1..100000]of longint;
    c,m,n,i,j,k:longint;
    procedure zou(x:longint);
    var
    y,y1:longint;
    begin
    y:=x;
    while b[a[y]]<>i do
    begin
    b[a[y]]:=i;
    inc(d[i]);
    y:=a[y];
    end;
    y1:=i;
    while y1<>a[y] do
    begin
    d[a[y1]]:=d[y1]-1;
    y1:=a[y1];
    end;
    y1:=a[y1];
    while y1<>a[y] do
    begin
    d[y1]:=d[a[y]];
    y1:=a[y1];
    end;
    end;
    begin
    assign(input,'treat.in');
    assign(output,'treat.out');
    reset(input);
    rewrite(output);
    readln(n);
    for i:=1 to n do
    readln(a[i]);
    for i:=1 to n do
    if d[i]=0 then
    begin
    b[i]:=i;
    inc(d[i]);
    zou(i);
    end;
    for i:=1 to n do
    writeln(d[i]);
    close(input);
    close(output);
    end.