CF 120F Spider 树的直径 简单题
一个男孩有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 树的直径 简单题的更多相关文章
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- 树状数组 简单题 cf 961E
题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...
- 【对询问分块】【主席树】bzoj2683 简单题
对操作序列分块,每S次暴力重建主席树. 当S=sqrt(n*log(n))时,复杂度为O(m*sqrt(n*log(n))). 在线的. #include<cstdio> #include ...
- cf 605A Sorting Railway Cars 贪心 简单题
其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...
- LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)
Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
随机推荐
- mac下使用Solarized配色方案
Solarized配色方案不用多介绍了.具体点击这里:http://ethanschoonover.com/solarized 我们首先搞定macvim 你需要下载solarized.vim配色文件, ...
- Mac上因磁盘格式导致gulp无限刷新问题
今天遇到个超奇葩的问题,使用gulp.watch监控文件变化,但是并没有修改文件,却一直执行change,导致浏览器无限刷新 调试了10小时,代码各种删改,一直不得其解.切换到Windows运行,又正 ...
- 为mysql在表的某一位置增加一列
如果想在一个已经建好的表中添加一列,可以用诸如: alter table t1 add column addr varchar(20) not null; 这条语句会向已有的表t1中加入一列addr, ...
- 配置duilib
前两天项目要用duilib重构.苦于网上几乎没有duilib的文档和教程,郁闷之极.那份简单文档上的代码都基本看懂了,就是不知道怎么配置,代码跑不起来! 网络上也几乎没有告诉第一次配置duilib的文 ...
- fs event_socket
mod_event_socket Skip to end of metadata Created by John Boteler, last modified by Niek Vlesse ...
- ceph
http://docs.ceph.com/docs/master/radosgw/swift/java/
- 让图片完全显示出来,应对overflow,以及在背景中完全显示出来
1.应对overflow <script type="text/javascript"> $('.foo img').css('width','100%');//修正, ...
- Tensorflow ——神经网络
Training Data Eval: Num examples: 55000 Num correct: 52015 Precision @ 1: 0.9457Validation Data Eval ...
- 在AndroidStudio中引入SlidingMenu第三方库的步骤
步骤一: 在GitHub上下载库文件 步骤二: 在需要引入库的项目中导入一个Moudle,如下图: 步骤三: 将下载后的Slidingme ...
- 【转】弹出可拖动的DIV层提示窗口
来源:www.divcss5.com <html> <head> <meta http-equiv="Content-Type" content=&q ...