一个男孩有n只玩具蜘蛛,每只蜘蛛都是一个树的结构,现在男孩准备把这n只小蜘蛛通过粘贴节点接在一起,形成一只大的蜘蛛。大的蜘蛛也依然是树的结构。输出大的蜘蛛的直径。

知识:

树的直径是指树上的最长简单路

求树的直径有个结论:

假设s-t这条路径为树的直径,或者称为树上的最长路。

从任意一点u出发搜到的最远的点一定是s、t中的一点,然后再从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜或者深搜就可以找出树的最长路

证明:反证法。

那回到这道题,要使得大蜘蛛的直径最大,就要使连接的小蜘蛛都是用s,t2个点来和其他蜘蛛连接

所以大蜘蛛的直径就是小蜘蛛的直径的和

 #include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int maxn=;
const int inf=0x3f3f3f3f; struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot;
bool vis[maxn];
struct Point
{
int num,dis;
}; void init()
{
memset(head,-,sizeof head);
tot=;
} void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} //返回树的直径
int solve(int m);
//返回结构体,树的节点和节点到u的距离
Point bfs(int u,int m); int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
scanf("%d",&n);
int ans=;
for(int i=;i<=n;i++)
{
init();
int m;
scanf("%d",&m);
for(int i=;i<m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
ans+=solve(m);
}
printf("%d\n",ans); return ;
} int solve(int m)
{
Point cnt=bfs(,m);
Point ret=bfs(cnt.num,m);
return ret.dis;
} Point bfs(int s,int m)
{
memset(vis,false,sizeof vis);
Point start;
start.num=s;
start.dis=;
vis[s]=true;
queue<Point>que;
while(!que.empty())
que.pop();
que.push(start);
Point ret;
while(!que.empty())
{
Point u=que.front();
que.pop();
if(que.empty())
{
ret.num=u.num;
ret.dis=u.dis;
}
for(int i=head[u.num];~i;i=edge[i].next)
{
int v=edge[i].to;
if(vis[v])
continue;
Point uu;
uu.num=v;
uu.dis=u.dis+;
vis[v]=true;
que.push(uu);
}
}
return ret;
}

CF 120F Spider 树的直径 简单题的更多相关文章

  1. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  2. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  3. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  4. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  5. 树状数组 简单题 cf 961E

    题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...

  6. 【对询问分块】【主席树】bzoj2683 简单题

    对操作序列分块,每S次暴力重建主席树. 当S=sqrt(n*log(n))时,复杂度为O(m*sqrt(n*log(n))). 在线的. #include<cstdio> #include ...

  7. cf 605A Sorting Railway Cars 贪心 简单题

    其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...

  8. LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)

    Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...

  9. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

随机推荐

  1. Codeforces Round #339 Div.2 B - Gena's Code

    It's the year 4527 and the tanks game that we all know and love still exists. There also exists Grea ...

  2. ExtJS参考手册

    ExtJS是一个用javascript写的,主要用于创建前端用户界面,是一个与后台技术无关的前端ajax框架.因此,可以把ExtJS用在.Net.Java.Php等各种开发语言开发的应用中.ExtJs ...

  3. 嵌入式系统Linux内核开发工程师必须掌握的三十道题(转)

    嵌入式系统Linux内核开发工程师必须掌握的三十道题 如果你能正确回答以下问题并理解相关知识点原理,那么你就可以算得上是基本合格的Linux内核开发工程师,试试看! 1) Linux中主要有哪几种内核 ...

  4. javaweb在线预览

    需要工具: 1.openoffice 将word.excel.ppt.txt等文件转换为pdf文件 2.SWFTool 将pdf转换为swf文件 3.flexPaper是一个开源轻量级的在浏览器上显示 ...

  5. note: declarations in dependent base ‘std::basic_ios<char>’ are not found by unqualified lookup

    错误信息如题所示. 修改前: template< typename _CharT, typename _Traits = std::char_traits<_CharT> > ...

  6. 在AndroidStudio中引入SlidingMenu第三方库的步骤

    步骤一:        在GitHub上下载库文件     步骤二:         在需要引入库的项目中导入一个Moudle,如下图:     步骤三:         将下载后的Slidingme ...

  7. 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍

    下文所有内容转自开源中国:http://www.oschina.net/question/565065_86453#tags_nav ================================= ...

  8. Advancing The Realtime Web With RethinkDB

    RethinkDB is an open-source distributed database built to store JSON and scale with very little effo ...

  9. android中的EditView控件

    android中的EditView控件 EditText继承关系:View-->TextView-->EditText ,EditText是可编辑文本框 1.EditText默认情况下,光 ...

  10. Jenkins入门系列之

    Jenkins入门系列之——00答疑解惑 Jenkins进阶系列之——11修改Jenkins用户的密码 Jenkins进阶系列之——12详解Jenkins节点配置 Jenkins进阶系列之——13修改 ...