codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

时间:2023-02-19 10:01:42

题目链接:

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<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+;
const int maxn=;
const double eps=1e-; int n,m,vis[N],dis[N];
vector<int>ve[N];
queue<int>qu; void bfs(int x)
{
mst(dis,);
mst(vis,);
qu.push(x);
vis[x]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
int len=ve[fr].size();
for(int i=;i<len;i++)
{
int y=ve[fr][i];
if(!vis[y])
{
dis[y]=dis[fr]+;
vis[y]=;
qu.push(y);
}
}
}
} int main()
{
int u,v;
read(n);read(m);
For(i,,m)
{
read(u);read(v);
ve[u].push_back(v);
ve[v].push_back(u);
}
bfs();
int s=,ans=;
For(i,,n)
if(dis[i]>dis[s])s=i;
bfs(s);
For(i,,n)ans=max(ans,dis[i]);
cout<<ans<<"\n"; return ;
}

codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)的更多相关文章

  1. Brain Network &lpar;medium&rpar;&lpar;DFS&rpar;

    H - Brain Network (medium) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  2. Brain Network &lpar;medium&rpar;

    Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...

  3. codeforces 690C1 C1&period; Brain Network &lpar;easy&rpar;&lpar;水题&rpar;

    题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  4. codeforces 690C3 C3&period; Brain Network &lpar;hard&rpar;&lpar;lca&rpar;

    题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  5. Codeforces 690 C3&period; Brain Network &lpar;hard&rpar; LCA

    C3. Brain Network (hard)   Breaking news from zombie neurology! It turns out that – contrary to prev ...

  6. poj 1383 Labyrinth【迷宫bfs&plus;树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  7. codeforce 337D Book of Evil ----树形DP&amp&semi;bfs&amp&semi;树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  8. Codeforces 734E Anton and Tree&lpar;缩点&plus;树的直径&rpar;

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  9. E - We Need More Bosses CodeForces - 1000E (tarjan缩点,树的直径)

    E - We Need More Bosses CodeForces - 1000E Your friend is developing a computer game. He has already ...

随机推荐

  1. 从零开始学习jQuery &lpar;二&rpar; 万能的选择器

    本系列文章导航 从零开始学习jQuery (二) 万能的选择器 一.摘要 本章讲解jQuery最重要的选择器部分的知识. 有了jQuery的选择器我们几乎可以获取页面上任意的一个或一组对象, 可以明显 ...

  2. Android SDK生成时,自定义文件名称,而非系统第一分配的app-release&period;apk

    buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.tx ...

  3. log4j文件

    log4j文件是一种开源日志记录工具,其作用是记录程序运异常行过程中的重要的操作信息和记录可能出现的异常情况便于调试. 根据日志记录的信息内容可分为3类: Sql日志:记录系统执行的SQL语句 异常日 ...

  4. &period;net Mvc文件下载的功能,大文件下载完成之后修改数据库功能

    原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ...

  5. Mac下安装php5&period;6&sol;7&period;1

    安装环境 OS X EI Capitan 10.11.4 Homebrew安装 homebrew是一个类似于ubuntu中apt-get的一个软件管理器,安装比较简单,在命令行中输入如下代码: rub ...

  6. csv impor export with mysql

    server-side:SELECT id,tutorialId,tutorialName,ucreatelink,structureVersion FROM base_courseINTO OUTF ...

  7. Scratch 数字游戏

    本想用Scratch给女儿做一个类似舒尔特方格的程序来认识数字和提升专注,想想这对刚刚3岁的孩子来说还是比较困难的,于是只做了3*3的方格,来认识数字1-9. 游戏地址:Random 9 v0.21 ...

  8. Perl分片技术

    分片(slice) 在perl中,如果想要取得一部分变量.一部分列表内容.一部分hash内容,可以采用分片(切片)的方式. 注意,perl并未提供字符串的切片方式,但可以使用内置函数substr()来 ...

  9. Debian &amp&semi; CentOS建立本地iso源

    在宿舍搞开发的时候经常遇到有些工具需要安装,没有网络,这时候只能靠mount本地的iso镜像来搞,结果像Debian有3张安装光盘,CentOS有2张光盘,有时候安装包不在第一张光盘里,而在第二张光盘 ...

  10. PyQT5-QCalendarWidget 日历显示

    """ QCalendarWidget:提供了日历插件 Author:dengyexun DateTime:2018.11.22 """ f ...