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 ...
随机推荐
- hadoop删除节点
1.修改Master节点Hdfs-site.xml,增加dfs.hosts.exclude参数 eg: <property> <name>dfs.hosts.exclude&l ...
- 课堂所讲整理:super和转型(修改版)
创建父类: package org.hanqi.pn0120; public class Father { private String name; private int age; public S ...
- Ubuntu 12.04 禁用触摸板
昨天把系统换为Backbox了,版本为Ubuntu12.04,装完后发现其触摸板不能禁用,之前在其他版本都是直接快捷键就可关闭或者启用触摸板,解决方法如下: sudo add-apt-reposito ...
- 解决Ubuntu下Sublime Text 3无法输入中文
前言 sublime很好用,但是ubuntu下不能输入中文,这是一个很大的问题.不知道为什么开发着一直也不解决,好在还是有高手在,总能找到方法.网上方法很多,但是也很乱,现在我将自己的经验总结一下. ...
- Java theory and practice
This content is part of the series: Java theory and practice A brief history of garbage collection A ...
- UDP/TCP通信小记
TCP 和UDP的区别 TCP是面向连接的; 所谓连接 就是 打开的时候要握手,收发数据的时候要确认(传说中的窗口协议保持滑动过去的窗口都已成功发送,接收方已经成功接收). UDP是无连接的. 所 ...
- linux工具之dracut
这是一个工具类,不是一个后台服务类 centos7.2-minimal就下面三个包 [root@1st-kvm ~]# rpm -qa|grep dracutdracut-config-rescue- ...
- 前端优化:DNS预解析提升页面速度
在网页体验中我们常会遇到这种情况,即在调用百度联盟.谷歌联盟以及当前网页所在域名外的域名文件时会遇到请求延时非常严重的情况.那么有没有方法去解决这种请求严重延时的现象呢? 一般来说这种延时的原因不会是 ...
- Struts2中s:set标签和s:if标签小结
1. s:set标签 格式:<s:set name="" value="" scope=””/> 说明:把jsp页面中的一个值,以name存储起来 ...
- ExpandableListView方法详解
正文 一.结构public interface ExpandableListAdapter 间接子类:BaseExpandableListAdapter,CursorTreeAdapter,Resou ...