这题是个遗憾 !!!!!当时一直不敢相信两个站一定在直径上,赛后想想自己真的是脑袋抽风, 如果其中一个站不在直径上就反向的说明了这条不是直径。可以很明白我们可以肯定的是有一个点一定在直径上假如另外一个点不在直径上,那么他在分支上,那么可以知道直径上的某点一定大于这个分支的最远点,显然放在这个分支上是不合适的。好现在我们知道了者两个点一定在直径上,二分可能最小的距离,

求直径 先从一个点 bfs到离他最远的点a ,然后从a bfs 到离他 最远的点b ,然后 记录路径,就得到了 ab为直径上的树,那么现在将每个直径上的点进行bfs得到了算以他为根的分支最远点(不算直径),然后每次二分后,从直径的两段朝中间走,知道走到二分的答案,然后判断可不可行, 我i好渣,当时脑袋真的被抽风了!

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ;
vector<int> F[maxn],rdio;
int n;
int per[maxn],dist[maxn];
bool use[maxn];
int bfs(int s,int &ma){
ma=-;
queue<int> Q;
use[s]=true;
int loc;
dist[s]=;
per[s]=-;
Q.push(s);
while(!Q.empty()){
int t=Q.front(); Q.pop();
int siz=F[t].size();
if(dist[t]>ma){
ma=dist[t]; loc=t;
}
for(int i=; i<siz ; ++i){
int to = F[t][i];
if(use[to]==true)continue;
dist[to]=dist[t]+;
use[to]=true;
per[to]=t;
Q.push(to);
}
}
return loc;
}
int Len;
int madist[maxn];
bool jud(int dist, int &x, int &y){
x=; y=Len-;
int d1=madist[];
if(d1>dist) return false;
while(true){
if(x+==y) break;
int d = max( d1+,madist[x+]);
if( d > dist ) break ;
d1=d;
x++;
}
d1=madist[y];
if(d1>dist) return false;
while(true){
if(y-==x)break;
int d= max(d1+,madist[y-]);
if(d>dist) break;
d1=d;
y--;
}
for(int i=x+; i<y; ++i){
int d = min(madist[i]+abs(i-x),madist[i]+abs(i-y));
if(d>dist) return false;
}
return true;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
rdio.clear();
for(int i=; i<=n; ++i ) F[i].clear();
for(int i=; i<n; ++i){
int a,b;
scanf("%d%d",&a,&b);
F[a].push_back(b);
F[b].push_back(a);
}
memset(use,false,sizeof(use));
int ttt;
int st=bfs(,ttt);
memset(use,false,sizeof(use));
int ed=bfs(st,ttt);
memset(use,false,sizeof(use));
for(int i = ed; i!=-; i=per[i]){
rdio.push_back(i);
use[i]=true;
}
for(int i=; i<int(rdio.size()) ; ++i)
bfs(rdio[i],madist[i]);
Len=rdio.size();
int L=,R=n;
int x,y,anx,any;
while( L<R ){
int mid=(L+R)/;
if(jud(mid,x,y)==true){
R=mid;
anx=x; any=y;
}
else L=mid+;
}
printf("%d %d %d\n",R,rdio[anx],rdio[any]);
}
return ;
}

zoj3820 树的直径+二分的更多相关文章

  1. Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)

    题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...

  2. bzoj 2282 [Sdoi2011]消防(树的直径,二分)

    Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家 ...

  3. Building Fire Stations ZOJ - 3820 (二分,树的直径)

    大意: 给定树, 求两个点, 使得所有其他的点到两点的最短距离的最大值尽量小. 二分答案转为判定选两个点, 向外遍历$x$的距离是否能遍历完整棵树. 取直径两段距离$x$的位置bfs即可. #incl ...

  4. [10.12模拟赛] 老大 (二分/树的直径/树形dp)

    [10.12模拟赛] 老大 题目描述 因为 OB 今年拿下 4 块金牌,学校赞助扩建劳模办公室为劳模办公室群,为了体现 OI 的特色,办公室群被设计成了树形(n 个点 n − 1 条边的无向连通图), ...

  5. [Bzoj2282]消防(二分答案+树的直径)

    Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家 ...

  6. zoj 3820 Building Fire Stations (二分+树的直径)

    Building Fire Stations Time Limit: 5 Seconds      Memory Limit: 131072 KB      Special Judge Marjar ...

  7. Forethought Future Cup - Elimination Round C 二分 + 交互(求树的直径)

    https://codeforces.com/contest/1146/problem/C 题意 一颗大小为n的树,每次可以询问两个集合,返回两个集合中的点的最大距离,9次询问之内得出树的直径 题解 ...

  8. 牡丹江.2014B(图论,树的直径)

    B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & ...

  9. BZOJ 2282 & 树的直径

    SDOI2011的Dayx第2题 题意: 在树中找到一条权值和不超过S的链(为什么是链呢,因为题目中提到“使得路径的两端都是城市”,如果不是链那不就不止两端了吗——怎么这么机智的感觉...),使得不在 ...

随机推荐

  1. python2.0_day18_django_form

    Django formDjango admin 为什么要讲form,Django里的form能做什么. 前面day16节 简单学习了Django admin,我们知道当我们的models在admin. ...

  2. laravel框架容器管理的一些要点

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

  3. 学习JQuery - 10

    第四章 Styling and Animating 1. 使用内联属性修改CSS 我们知道HTML在onload时会读取css的各项值. 那么,我们能不能在之后的操作中改变css值呢? 答案是肯定的! ...

  4. 存储总量达20T的MySQL实例,如何完成迁移?

    版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/122 来源:腾云阁 https://www.qclou ...

  5. sencha touch 问题汇总

    做sencha touch有一段时间了,目前而言,sencha touch在android上问题比较严重,在此对android中sencha touch的问题做一些汇总: 1.内存问题: 打包成安装程 ...

  6. c# 对数据库的操作

    1.首先需要引用 using System.Data.SqlClient; 2.创建连接 SqlConnection connection = new SqlConnection(); connect ...

  7. 第k最短路A*启发式搜索

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 21549   Accepted: 5862 ...

  8. html<input>输入框中各种正则表达式设置

    <%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding ...

  9. 【node】------websocket------【巷子】

    001.学习地址 https://github.com/websockets/ws 002.server.js //引入ws第三方模块 const WebSocket = require('ws'); ...

  10. 160227、javascript特效

    1.给网页设定快捷键 js: function getkey(){     event = event || window.event;     url = "www.baidu.com&q ...