一个男孩有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. spark中streamingContext的使用详解

    两种创建方式 val conf = new SparkConf().setAppName(appName).setMaster(master);val ssc = new StreamingConte ...

  2. ARM体系的7种工作模式

    一.ARM体系的CPU有以下7种工作模式:   用户模式(usr)    大多数程序运行于用户模式 特权模式   系统模式(sys)   运行具有特权的操作系统任务 异常模式 中断模式(irq)   ...

  3. 论文阅读之:Is Faster R-CNN Doing Well for Pedestrian Detection?

    Is Faster R-CNN Doing Well for Pedestrian Detection? ECCV 2016   Liliang Zhang & Kaiming He 原文链接 ...

  4. C#加载dll 创建类对象

    //加载dll 创建类对象string sqlightAssembly = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "syst ...

  5. Linux下PHP+MySQL+CoreSeek中文检索引擎配置

    说明: 操作系统:CentOS 5.X 服务器IP地址:192.168.21.127 Web环境:Nginx+PHP+MySQL 站点根目录:/usr/local/nginx/html 目的:安装co ...

  6. 使用seajs来引入js代码

    注意的是:引入jquery的代码最好放在html文件中,本文是为了说明seajs中require如何使用的,才将jquery放入seajs中的. html中对应的代码: <script type ...

  7. Python中strip()函数

    在python API中这样解释strip()函数:

  8. linux 通用IO

    open(),read(),write(),close()可以应用于管道,FIFO,socket,或者终端等所有文件类型执行IO操作. lseek()并不适用于所有类型的文件.不允许将lseek()应 ...

  9. qemu-kvm命令

    三种方式创建虚拟机 1.qemu-kvm来创建虚拟机 通过阅读man qemu-kvm手册而清楚的. 于20160430阅读 [root@kvm1 ~]# /usr/libexec/qemu-kvm ...

  10. [Asp.net]说说密码框和只读框

    作者:Wolfy出处:http://www.cnblogs.com/wolf-sun/ 引言 最近负责了一个公司的小项目,从前台到后代,都是自己搞的,为一个客户弄一个信息管理的小系统,虽然对界面什么的 ...