zoukankan      html  css  js  c++  java
  • codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接:

    C2. Brain Network (medium)

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains uand v (1 ≤ u, v ≤ n) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.

    In this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version.

    Input

    The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains ab it connects (1 ≤ a, b ≤ n and a ≠ b).

    Output

    Print one number – the brain latency.

    Examples
    input
    4 3
    1 2
    1 3
    1 4
    output
    2
    input
    5 4
    1 2
    2 3
    3 4
    3 5
    output
    3

    题意:

    给一棵树,求树的直径;

    思路:

    两次bfs找树的直径,水题;

    AC代码;

    #include <bits/stdc++.h>
    /*
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    */
    using namespace std;
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef  long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=1e5+10;
    const int maxn=1005;
    const double eps=1e-10;
    
    
    
    int n,m,vis[N],dis[N];
    vector<int>ve[N];
    queue<int>qu;
    
    void bfs(int x)
    {
        mst(dis,0);
        mst(vis,0);
        qu.push(x);
        vis[x]=1;
        while(!qu.empty())
        {
            int fr=qu.front();
            qu.pop();
            int len=ve[fr].size();
            for(int i=0;i<len;i++)
            {
                int y=ve[fr][i];
                if(!vis[y])
                {
                    dis[y]=dis[fr]+1;
                    vis[y]=1;
                    qu.push(y);
                }
            }
        }
    }
    
    int main()
    {
            int u,v;
            read(n);read(m);
            For(i,1,m)
            {
                read(u);read(v);
                ve[u].push_back(v);
                ve[v].push_back(u);
            }
            bfs(1);
            int s=0,ans=0;
            For(i,1,n)
            if(dis[i]>dis[s])s=i;
            bfs(s);
            For(i,1,n)ans=max(ans,dis[i]);
            cout<<ans<<"
    ";
    
            return 0;
    }
  • 相关阅读:
    轮播图
    原生js实现分页效果(带实例)
    mint-ui Toast icon 图标
    阮小二买彩票
    js事件冒泡和事件捕捉
    html,css,js加载顺序
    单调栈-哈希表-768. 最多能完成排序的块 II
    同余问题-三整除系列
    动态规划-区间dp-单调栈-1130. 叶值的最小代价生成树
    动态规划-1Ddp-983. 最低票价
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5661800.html
Copyright © 2011-2022 走看看