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,加我一起讨论问题,一起进步^-^

  • 相关阅读:
    修改CentOS 6.4 root用户的系统默认语言设置
    Xpages学习
    Mysql 执行sql脚本文件
    Errors occurred during the build. Errors running builder 'JavaScript Validator' on project 'XXX'.
    【鸟哥学习笔记】之一:目录的权限问题
    学习C++的一些问题总结
    C# 一些知识点总结(一)_继承,多态,集合,关键字...
    .NET Framework 框架的一些简单介绍
    Winform窗体关闭时判断是否关闭
    SQL Server 数据库的安全管理(登录、角色、权限)
  • 原文地址:https://www.cnblogs.com/victorslave/p/4843196.html
Copyright © 2011-2022 走看看