Brain Network (medium)

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 31 21 31 4

output

2

input

5 41 22 33 43 5

output

3分析:树中最长路,两次bfs;代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,vis[maxn],ma,now;
vi a[maxn];
int bfs(int x)
{
    memset(vis,0,sizeof(vis));ma=0;
    queue<int>p;p.push(x);vis[x]=1;
    while(!p.empty())
    {
        int u=p.front();p.pop();
        for(int y:a[u])
        {
            if(!vis[y])
            {
                vis[y]=vis[u]+1;
                p.push(y);
                if(ma<vis[y])ma=vis[y],now=y;
            }
        }
    }
    return now;
}
int main()
{
    int i,j,k,t;
    scanf("%d%d",&n,&m);
    while(m--)
    {
        scanf("%d%d",&j,&k);
        a[j].pb(k),a[k].pb(j);
    }
    j=bfs(bfs(1));
    printf("%d\n",ma-1);
    //system ("pause");
    return 0;
}
时间: 2024-12-28 01:09:06

Brain Network (medium)的相关文章

CodeForces 690C2 Brain Network (medium)(树上DP)

题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> G[maxn]; int f[maxn], g[maxn], l[maxn]; int dfs(int root, int fa){ if(f[root] !=

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

Codeforces 690 C3. Brain Network (hard) LCA

C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie consum

Brain Network (easy)

One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (

complex brain network

Organization, development and function of complex brain networks The Brain as a Complex System: Using Network Science as a Tool for Understanding the Brain Rubinov M, Sporns O. Complex network measures of brain connectivity: uses and interpretations.

codeforces 690C3 Brain Network

simple:并查集一下 #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; typedef long long LL; const

CodeForces 690C1 Brain Network (easy) (水题,判断树)

题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn =1000 + 5; int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){ int n, m, x, y; cin

JUnit in android

android 测试框架是基于JUnit(www.junit.org)的,感兴趣的可以看看JUnit的源码,相信会对做unit test in android 大有帮助的. 本文只是一篇介绍JUnit如何用于android的入门文字,更多是照着网上的敲出来的,很意外的是三星的官网上居然有介绍JUnit的文章, 哈哈,所以拿过来用了.以前也没做过unit test,JUnit也是最近才接触,记录一下,边学边用吧!! 英文还OK的可以直接看原文(click me)吧!! JUnit的基本思路: a

OpenStack若干概念

近期在部署OpenStack时涉及到各个服务之间的诸多概念,这里简要记录其中的一些作为备忘. 服务(service) 在OpenStack中,一个服务有若干端点,用户通过端点访问服务并使用服务提供的功能: 计算服务(Compute Service)—— Nova 网络服务(Networking Service)——Neutron 身份服务(Identity Service)——Keystone 镜像服务(Image Service)——Glance 界面服务(Dashboard)——Horizo