zoukankan      html  css  js  c++  java
  • poj1985&&第四次CCF软件认证第4题 求树的直径

    Cow Marathon
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 4216   Accepted: 2137
    Case Time Limit: 1000MS

    Description

    After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms.

    Input

    * Lines 1.....: Same input format as "Navigation Nightmare".

    Output

    * Line 1: An integer giving the distance between the farthest pair of farms.

    Sample Input

    7 6
    1 6 13 E
    6 3 9 E
    3 5 7 S
    4 1 3 N
    2 4 20 W
    4 7 2 S
    

    Sample Output

    52
    

    Hint

    The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52.

    Source

    本体的大概意思就是求一个树上任意两个点之间的最大距离,本树是一个无向图树,方法并不唯一,
    可以用dp解决,也可以用dfs解决,
    存储边的时候一定要切记应该是无向图,两边都应该存进去
     利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点。这样就可以首先任取一个点,bfs求出离其最远的点,
    在用同样的方法求出离这个叶子节点最远的点,此时两点间的距离就是树的直径。
     
        #include<iostream>  
        #include<cstring>  
        #include<cstdio>  
        #include<algorithm>        
        using namespace std;  
        #define MAXN  500000        
        struct edge{  
            int to,dis,next;  
        }e[9999999];        
        int head[MAXN],en;  
        int dis[MAXN];        
        void add(int u,int v ,int w){  
            e[en].to=v;  
            e[en].dis=w;  
            e[en].next=head[u];  
            head[u]=en++;  
        }  
        void dfs(int f,int u,int d){  
            dis[u]=d;  
            for(int i=head[u];i!=-1;i=e[i].next)  
                if(e[i].to!=f)  
                    dfs(u,e[i].to,d+e[i].dis);  
        }
        int u,v,c;  
        int n,m;  
        char s[20];  
        int main(){  
            while(~scanf("%d%d",&n,&m)){  
                memset(head,-1,sizeof(head));  
                en=0;  
                for(int i=0;i<m;i++){  
                    scanf("%d%d%d%s",&u,&v,&c,s);  
                    add(u,v,c);  
                    add(v,u,c);  
                }  
                dfs(0,1,0);  
                int maxs,tag;  
                maxs=-1;tag=0;  
                for(int i=1;i<=n;i++){  
                    if(maxs<dis[i])  
                    {  
                        maxs=dis[i];  
                        tag=i;  
                    }  
                }  
                dfs(0,tag,0);  
                maxs=-1;  
                for(int i=1;i<=n;i++)  
                    maxs=max(maxs,dis[i]);  
                printf("%d ",maxs);  
            }  
            return 0;  
        }  


    下面的这个题是CCF认证的第四题,
    该问题仍然求得是任意两点之间的最大距离,
    但是该题并没有给出边的长度,
    所以在套用模版的时候可以把边存储为1
    另外该题有n+m,边,在进行循环的时候一定要注意该题的界限是m+n
     
    姓名:
    IJ����  
    问题描述
    试题编号: 201503-4
    试题名称: 网络延时
    时间限制: 1.0s
    内存限制: 256.0MB
    问题描述:
    问题描述
       给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机、交换机与电脑之间使用网络连接。交换机按层级设置,编号为1的交换机为根交换 机,层级为1。其他的交换机都连接到一台比自己上一层的交换机上,其层级为对应交换机的层级加1。所有的终端电脑都直接连接到交换机上。
      当信息在电脑、交换机之间传递时,每一步只能通过自己传递到自己所连接的另一台电脑或交换机。请问,电脑与电脑之间传递消息、或者电脑与交换机之间传递消息、或者交换机与交换机之间传递消息最多需要多少步。
    输入格式
      输入的第一行包含两个整数n, m,分别表示交换机的台数和终端电脑的台数。
      第二行包含n - 1个整数,分别表示第2、3、……、n台交换机所连接的比自己上一层的交换机的编号。第i台交换机所连接的上一层的交换机编号一定比自己的编号小。
      第三行包含m个整数,分别表示第1、2、……、m台终端电脑所连接的交换机的编号。
    输出格式
      输出一个整数,表示消息传递最多需要的步数。
    样例输入
    4 2
    1 1 3
    2 1
    样例输出
    4
    样例说明
      样例的网络连接模式如下,其中圆圈表示交换机,方框表示电脑:

      其中电脑1与交换机4之间的消息传递花费的时间最长,为4个单位时间。
    样例输入
    4 4
    1 2 2
    3 4 4 4
    样例输出
    4
    样例说明
      样例的网络连接模式如下:

      其中电脑1与电脑4之间的消息传递花费的时间最长,为4个单位时间。
    评测用例规模与约定
      前30%的评测用例满足:n ≤ 5, m ≤ 5。
      前50%的评测用例满足:n ≤ 20, m ≤ 20。
      前70%的评测用例满足:n ≤ 100, m ≤ 100。
      所有评测用例都满足:1 ≤ n ≤ 10000,1 ≤ m ≤ 10000。
    答题栏
    试题编号: 201503-4
    试题名称: 网络延时
     
  • 相关阅读:
    [bbk5177]第62集第6章 用scheduler自动化 10(章节标题内容调整)
    [bbk4956]第65集 第7章 数据库的维护 02
    [bbk4940]第61集第6章 用scheduler自动化 09
    [bbk5179]第66集 第7章 数据库的维护 03
    ORA01031: insufficient privileges(有待于二次解决)
    Google Analytics功能篇 如何跟踪邮件打开率与点击率
    小资之豆浆篇 (IS2120@BG57IV3)
    wix custom action 之 vbscript 简明步骤(IS2120@BG57IV3)
    c++ faq (15)
    linux 下面字符串处理函数实现 抄来看一下 抄自[http://blog.csdn.net/BeWithLei/article/details/1719242]
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4780050.html
Copyright © 2011-2022 走看看