题意:一个树上建两个加油站。使得全部点到达其近期加油站的最大距离最小。

解法:二分答案。关键时二分时候,要最合理话布局两个点的位置,做法是处理出来树的直径,然后在直径两端分别向中间移动二分的x步的两个点布下加油站。

贪心能够证明正确性;

代码:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
#define zero(_) (_<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=200010;
const LL INF=0x3FFFFFFF;
vector<int> vec[Max];
int n;
vector<int> diameter;
int rem[Max];
int rem1[Max];
int rem2[Max];
int dis[Max];
int st,en;
int dui[Max*2];
int now=0;
int count1=10;
void dfs(int u)
{
memset(rem,0,sizeof rem);
rem[u]=-1;
int left=0;
int right=1;
dui[0]=u;
dis[u]=0;
while(left<right)
{
for(int i=0; i<vec[dui[left]].size(); i++)
{
if(rem[vec[dui[left]][i]]==0)
{
rem[vec[dui[left]][i]]=dui[left];
dis[vec[dui[left]][i]]=dis[dui[left]]+1;
dui[right++]=vec[dui[left]][i];
}
}
left++;
}
} void ran1(int u,int* re)
{
int left=0;
int right=1;
dui[0]=u;
dis[u]=0;
re[u]=count1;
while(left<right)
{
if(dis[dui[left]]<now)
for(int i=0; i<vec[dui[left]].size(); i++)
{
if(re[vec[dui[left]][i]]!=count1)
{
re[vec[dui[left]][i]]=count1;
dis[vec[dui[left]][i]]=dis[dui[left]]+1;
if(dis[vec[dui[left]][i]]<now)
dui[right++]=vec[dui[left]][i];
}
}
left++;
}
} bool OK(int middle)
{
count1++;
now=middle;
ran1(diameter[middle],rem1);
ran1(diameter[diameter.size()-1-middle],rem2);
for(int i=1; i<=n; i++)
{
if(rem1[i]!=count1&&rem2[i]!=count1)
return false;
}
return true;
}
int main()
{
int t;
cin>>t;
while(t--)
{
for(int i=1;i<Max;i++)
vec[i].clear();
diameter.clear();
scanf("%d",&n);
for(int i=0; i<n-1; i++)
{
int a,b;
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
dfs(1);
int d=0;
for(int i=1; i<=n; i++)
{
if(dis[i]>d)
{
d=dis[i];
st=i;
}
}
dfs(st);
d=0;
for(int i=1; i<=n; i++)
{
if(dis[i]>d)
{
d=dis[i];
en=i;
}
}
while(en!=-1)
{
diameter.push_back(en);
en=rem[en];
}
int left=0,right=diameter.size();
while(left<=right)
{
int middle=(left+right)/2;
if(!OK(middle))
{
left=middle+1;
}
else
{
right=middle-1;
}
}
int a=diameter[left];
int b=diameter[diameter.size()-1-left];
if(a==b)
{
a=b==n?n-1:b+1;
}
//cout<<diameter.size()<<" "<<OK(0)<<OK(1)<<endl;
cout<<left<<" "<<a<<" "<<b<<'\n';
}
return 0;
}
/*
34
4
1 2
1 3
1 4
5
1 2
2 3
3 4
4 5
3
1 2
2 3
*/

hdu5044(二分)的更多相关文章

  1. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  2. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  3. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  4. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  8. BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流

    1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...

  9. BZOJ 3110 [Zjoi2013]K大数查询 ——整体二分

    [题目分析] 整体二分显而易见. 自己YY了一下用树状数组区间修改,区间查询的操作. 又因为一个字母调了一下午. 貌似树状数组并不需要清空,可以用一个指针来维护,可以少一个log 懒得写了. [代码] ...

随机推荐

  1. C#学习-程序集和反射

    准备项目 1.新建一个空的解决方案MyProj.sln 2.在该解决方案下,建一个控制台项目P01.csproj 3.在该项目下,自己新建一个类MyFirstClass.cs 查看解决方案MyProj ...

  2. P1257 平面上的最接近点对

    题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...

  3. python框架之Flask基础篇(三)-------- 模版的操作

    1.flask特有的变量和函数: 变量:g.session.request.config 函数:url_for().get_flashed_messages()这个函数注意了啊,记住这是个函数,别忘了 ...

  4. 如何快速获取yun2win app key?

    注册yun2win开发者账号 1.在注册页面输入您的邮箱,点击下方发送,yun2win将会发送一封验证邮件到您的邮箱: 2.如果没有收到邮件请查看垃圾箱或者点击重新发送: 3.打开邮箱查看验证邮件,点 ...

  5. 安装nodejs6.9x以后,原来在nodejs4.2.x中运行正常的ionic项目出现问题的解决

    安装nodejs6.9x以后,原来在nodejs4.2.x中运行正常的程序出现的问题.看错误信息,由于NodeJs版本升级导致的. 到提示的目录下运行:npm rebuild node-sass -g ...

  6. 循环语句和方法(day4)

  7. Python 之web动态服务器

    webServer.py代码如下: import socket import sys from multiprocessing import Process class WSGIServer(obje ...

  8. Python---HTML表单

    一. http:80 https:443 -------------------------- 二.

  9. 哈夫曼树(Huffman Tree)

    Date:2019-06-21 14:42:04 做题时更多的是用到哈夫曼树的构造思想,即按照问题规模从小到大,依次解决问题,可以得到最优解 Description: 在一个果园里,多多已经将所有的果 ...

  10. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...