zoukankan      html  css  js  c++  java
  • 9-25考试第三题解题报告(还是写了指针翻译神马的都是浮云感觉好久不说话连话都不会说了233333333QVO)

    type
            link=^pnode;
            pnode=record
                    w:longint;
                    next:link;
            end;
    
            var
                    a,root:array[0..1122] of longint;
                    g:array[0..1100,0..1100] of longint;
                    hash:array[0..1122] of boolean;
                    list:array[0..1122] of link;
                    n,min:longint;
    
            procedure init;
            begin
                    assign(input,'badnews.in');
                    assign(output,'badnew.out');
                    reset(input);
                    rewrite(output);
            end;
    
            procedure terminate;
            begin
                    close(input);
                    close(output);
                    halt;
            end;
    
            procedure qsort(s,t:longint);
            var
                    i,j,x,m:longint;
            begin
                    m:=(s+t)>>1;
                    x:=a[m];
                    a[m]:=a[s];
                    a[s]:=x;
                    i:=s;
                    j:=t;
                    repeat
                            while (i<j) and (x>a[j]) do dec(j);
                            if i<j then begin a[i]:=a[j]; inc(i); end;
                            while (i<j) and (x<a[i]) do inc(i);
                            if i<j then begin a[j]:=a[i]; dec(j); end;
                    until i=j;
                    a[i]:=x;
                    inc(i);
                    dec(j);
                    if i<t then qsort(i,t);
                    if s<j then qsort(s,j);
            end;
    
            function dfs(i:longint):longint;
            var
                    j,tot:longint;
                    p,q:link;
                    pd:boolean;
            begin
                    pd:=true;
                    p:=nil;
                    for j:=1 to g[i][0] do
                    begin
                            if not hash[g[i][j]]  then
                            begin
                                    pd:=false;
                                    hash[g[i][j]]:=true;
                                    new(q);
                                    q^.w:=dfs(g[i][j])+1;
                                    q^.next:=p;
                                    p:=q;
                            end;
                    end;
                    if pd then exit(1);
                    tot:=0;
                    while p<>nil do
                    begin
                            inc(tot);
                            a[tot]:=p^.w;
                            p:=p^.next;
                    end;
                    qsort(1,tot);
    
                    for j:=2 to tot do inc(a[j],j-1);
                    dfs:=0;
                    for j:=1 to tot do if a[j]>dfs then dfs:=a[j];
            end;
    
            procedure main;
            var
                    i,x,min:longint;
            begin
                    fillchar(list,sizeof(list),0);
                    readln(n);
                    for i:=2 to n do
                    begin
                            readln(x);
                            inc(g[i][0]);
                            g[i][g[i][0]]:=x;
                            inc(g[x][0]);
                            g[x][g[x][0]]:=i;
                    end;
                    for i:=1 to n do
                    begin
                            fillchar(hash,sizeof(hash),0);
                            hash[i]:=true;
                            root[i]:=dfs(i);
                    end;
    
                    min:=10000;
                    for i:=1 to n do
                    begin
                            if root[i]<min then min:=root[i];
                    end;
                    writeln(min);
                    a[0]:=0;
                    for i:=1 to n do
                    begin
                            if root[i]=min then
                            begin
                                    inc(a[0]);
                                    a[a[0]]:=i;
                            end;
                    end;
    
                    for i:=1 to a[0]-1 do write(a[i],' ');
                    writeln(a[a[0]]);
            end;
    
    
            begin
                    init;
                    main;
                    terminate;
            end.

     喜欢就收藏一下,vic私人qq:1064864324,加我一起讨论问题,一起进步^-^

  • 相关阅读:
    SSRF
    【Floyd(并非水题orz)】BZOJ4093-[Usaco2013 Dec]Vacation Planning
    【SPFA+二分答案】BZOJ1614- [Usaco2007 Jan]Telephone Lines架设电话线
    【刷水-贪心】BZOJ1629-[Usaco2007 Demo]Cow Acrobats
    【刷水-二分答案】BZOJ1650 & BZOJ1639
    【UOJ244】[UER7]短路
    【尺取法好题】POJ2566-Bound Found
    【尺取法】POJ3061 & POJ3320
    【hash】BZOJ3751-[NOIP2014]解方程
    【最小点覆盖】POJ3041-Asteroids
  • 原文地址:https://www.cnblogs.com/victorslave/p/4843196.html
Copyright © 2011-2022 走看看