zoukankan      html  css  js  c++  java
  • URAL 1056. Computer Net

    1056. Computer Net

    Time limit: 2.0 second Memory limit: 64 MB

    Background

    Computer net is created by consecutive computer plug-up to one that has already been connected to the net. Each new computer gets an ordinal number, but the protocol contains the number of its parent computer in the net. Thus, protocol consists of several numbers; the first of them is always 1, because the second computer can only be connected to the first one, the second number is 1 or 2 and so forth. The total quantity of numbers in the protocol is N − 1 (N is a total number of computers). For instance, protocol 1, 1, 2, 2 corresponds to the following net:
    1 - 2 - 5
    |   |
    3   4
    
    The distance between the computers is the quantity of mutual connections (between each other) in chain. Thus, in example mentioned above the distance between computers #4 and #5 is 2, and between #3 and #5 is 3.
    Definition. Let the center of the net be the computer which has a minimal distance to the most remote computer. In the shown example computers #1 and #2 are the centers of the net.

    Problem

    Your task is to find all the centers using the set protocol.

    Input

    The first line of input contains an integer N, the quantity of computers (2 ≤ N ≤ 10000). Successive N − 1 lines contain protocol.

    Output

    Output should contain ordinal numbers of the determined net centers in ascending order.

    Sample

    inputoutput
    5
    1
    1
    2
    2
    
    1 2
    
    Problem Source: Rybinsk State Avia Academy
    ****************************************************************************************************
    一棵树,求最长路径的中点
    ****************************************************************************************************
     1 /*求树的最长路径,
     2   两次dfs(),
     3   第一次求最大深度
     4   第二次反向求路径,
     5   求这条最长路径的中点值;
     6   偶取2个,奇取1个;
     7   注意要用链式前向星存储(数据量大),
     8 */
     9 #include<iostream>
    10 #include<string>
    11 #include<cstring>
    12 #include<cstdio>
    13 #include<cmath>
    14 #include<algorithm>
    15 using namespace std;
    16 struct  node
    17 {
    18     int  n,next;
    19 }e[20010];
    20 int n,m,i,j,k,top,maxdep;
    21 int ind;
    22 int head[10010];
    23 int check[10010];
    24 int pre[10010];
    25 void init()
    26  {
    27      cin>>n;
    28      top=0;
    29      for(i=1;i<=n;i++)
    30       {
    31           head[i]=-1;
    32           check[i]=0;
    33       }
    34      for(i=2;i<=n;i++)//链式前向星
    35       {
    36           cin>>k;
    37           e[top].n=i;
    38           e[top].next=head[k];
    39           head[k]=top++;
    40           e[top].n=k;
    41           e[top].next=head[i];
    42           head[i]=top++;
    43       }
    44  }
    45  void dfs(int v,int t)//dfs();
    46    {
    47        check[v]=1;
    48        if(maxdep<t)
    49         {
    50             maxdep=t;
    51             ind=v;
    52         }
    53         int q=head[v];
    54         while(q!=-1)
    55          {
    56              if(!check[e[q].n])
    57               {
    58                   pre[e[q].n]=v;
    59                   dfs(e[q].n,t+1);
    60               }
    61             q=e[q].next;
    62          }
    63 
    64    }
    65    int main()
    66    {
    67        init();
    68        maxdep=-1;
    69        dfs(1,1);//求深度
    70        for(i=1;i<=n;i++)
    71         {
    72             check[i]=0;
    73             pre[i]=-1;
    74         }
    75         maxdep=-1;
    76         dfs(ind,1);//记录路径
    77         i=ind;
    78         top=0;
    79         while(i!=-1)
    80          {
    81              check[top++]=i;
    82              i=pre[i];//求路径
    83          }
    84          if((top&1)==0)//判断输出
    85           cout<<check[(top>>1)-1]<<' '<<check[(top>>1)]<<endl;
    86          else
    87           cout<<check[top>>1]<<endl;
    88          return 0;
    89 
    90    }
    View Code

    坚持!!!!!!!

  • 相关阅读:
    从壹开始前后端分离[.NetCore ] 38 ║自动初始化数据库(不定期更新)
    从壹开始前后端分离[.NetCore] 37 ║JWT实现权限与接口的动态分配——复杂策略授权
    从壹开始微服务 [ DDD ] 之十二 ║ 核心篇【下】:事件驱动EDA 详解
    从壹开始微服务 [ DDD ] 之八 ║剪不断理还乱的 值对象和Dto
    从壹开始微服务 [ DDD ] 之七 ║项目第一次实现 & CQRS初探
    CentOS7下的CDH 6.2.0 安装过程
    php获取客户端IP地址的方法
    IntelliJIdea 2016.2 使用 tomcat 8.5 调试spring的web项目时,bean被实例化两次导致timer和thread被启动了两遍的问题的解决
    Linux 系统 TCP优化
    Fedora 25-64位操作系统中安装配置Hyperledger Fabric过程
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3307736.html
Copyright © 2011-2022 走看看