ZOJ 3820:Building Fire Stations(树的直径 Grade C)
题意:
n个点的树,边长全为1,求找出两个点,使得树上离这两个点距离最远的那个点,到这两个点(中某个点就行)的距离最小。
思路:
求树直径,找中点,删除中间那条边(如果直径上点数为奇数,则删任何一侧都可),分成两个子树,再求中心,即为答案。
代码:
//14:12
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define N 200100 struct Graph{
struct Edge{
int to;
int next;
}e[N*];
int head[N];
int p;
void clear() {
p = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v) {
e[p].to = v;
e[p].next = head[u];
head[u] = p++;
}
}g; int n;
int dis[N];
int que[N*];
bool vis[N];
int fa[N]; void generateDis(const Graph &g, int s) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, , sizeof(vis));
memset(fa, -, sizeof(fa));
dis[s] = ;
int hd = ;
int tl = ;
que[tl++] = s;
vis[s] = ;
while (hd < tl) {
int now = que[hd++];
vis[now] = ;
for (int i = g.head[now]; i != -; i = g.e[i].next) {
int to = g.e[i].to;
if (dis[to] > dis[now]+) {
dis[to] = dis[now]+;
if (!vis[to]) {
vis[to] = ;
fa[to] = now;
que[tl++] = to;
}
}
}
}
} pair<int, int> getTreeMidPoint(const Graph &g, int s, int &OUT_dis) {
generateDis(g, s);
for (int i = ; i <= n; i++) {
if (dis[i] == 0x3f3f3f3f) dis[i] = -;
}
int u = max_element(&dis[], &dis[]+n) - dis; generateDis(g, u);
for (int i = ; i <= n; i++) {
if (dis[i] == 0x3f3f3f3f) dis[i] = -;
}
int v = max_element(&dis[], &dis[]+n) - dis; //printf("u = %d, v = %d, dis[v] = %d\n", u, v, dis[v]); int now = v;
while (dis[now] > (dis[v]+)/) {
now = fa[now];
} OUT_dis = dis[v]/ + dis[v]%;
return {fa[now], now};
} int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n); g.clear();
for (int i = ; i < n-; i++) {
int a, b;
scanf("%d%d", &a, &b);
g.addedge(a,b);
g.addedge(b,a);
} int tmpdis[];
pair<int ,int> point = getTreeMidPoint(g, , tmpdis[]); //printf("all tree get (%d %d, dis=%d)\n", point.first, point.second, tmpdis[0]); for (int i = g.head[point.first]; i != -; i=g.e[i].next) {
if (g.e[i].to == point.second) g.e[i].to = point.first;
} for (int i = g.head[point.second]; i != -; i=g.e[i].next) {
if (g.e[i].to == point.first) g.e[i].to = point.second;
} pair<int ,int> pa = getTreeMidPoint(g, point.first, tmpdis[]);
pair<int ,int> pb = getTreeMidPoint(g, point.second, tmpdis[]); printf("%d %d %d\n", max(tmpdis[], tmpdis[]), pa.second, pb.second);
}
return ;
}
ZOJ 3820:Building Fire Stations(树的直径 Grade C)的更多相关文章
- zoj 3820 Building Fire Stations 树的中心
Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...
- zoj 3820 Building Fire Stations(二分法+bfs)
题目链接:zoj 3820 Building Fire Stations 题目大意:给定一棵树.选取两个建立加油站,问说全部点距离加油站距离的最大值的最小值是多少,而且随意输出一种建立加油站的方式. ...
- zoj 3820 Building Fire Stations (二分+树的直径)
Building Fire Stations Time Limit: 5 Seconds Memory Limit: 131072 KB Special Judge Marjar ...
- zoj 3820 Building Fire Stations(树上乱搞)
做同步赛的时候想偏了,状态总是时好时坏.这状态去区域赛果断得GG了. 题目大意:给一棵树.让求出树上两个点,使得别的点到两个点较近的点的距离最大值最小. 赛后用O(n)的算法搞了搞,事实上这道题不算难 ...
- ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS
题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...
- ZOJ 3820 Building Fire Stations
题意: 树上找两个点 使得其它点到这两点随意一点的距离的最大值最小 思路: 最大值最小 想到二分 在二分的基础上判定这个最大值是否可能 怎样判定这个问题就是怎样选那两个点的问题 非常明显 我 ...
- ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】
题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题 ...
- zoj3820 Building Fire Stations 树的中心
题意:n个点的树,给出n-1条边,每条边长都是1,两个点建立防火站,使得其他点到防火站的最远距离最短. 思路:比赛的时候和队友一开始想是把这两个点拎起来,使得层数最少,有点像是树的中心,于是就猜测是将 ...
- Building Fire Stations ZOJ - 3820 (二分,树的直径)
大意: 给定树, 求两个点, 使得所有其他的点到两点的最短距离的最大值尽量小. 二分答案转为判定选两个点, 向外遍历$x$的距离是否能遍历完整棵树. 取直径两段距离$x$的位置bfs即可. #incl ...
随机推荐
- <s:submit> 指定的method方法不执行
很多文章在讲一个表单多个提交方法的时候都是在<s:submit>中通过method来指定,但是我在试验中怎么也不对,jsp页面代码如下 <%@page import="or ...
- struct2 命名空间
转自http://blog.csdn.net/carefree31441/article/details/4857546 使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s: ...
- 十二、mysql之视图,触发器,事务等
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- laravel5.5事件广播系统
目录 1. 定义广播事件 1.1 广播名称 1.2 广播数据 1.3 广播队列 1.4 广播条件 2. 频道授权 2.1 定义授权路由 2.2 定义授权回调 3. 对事件进行广播 3.1 可以使用ev ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- webdriver--设置元素等待
sleep():脚本执行到某一位置时“睡一会”,再继续执行:参数的单位是s:sleep方法由python的time模块提供,有两种引入和使用方式 import time time.sleep(5) f ...
- 自动using和Layout
一.自动using 1. Model 文件夹添加 Person类,在view文件夹下web.config文件,将namespace加入,cshtml文件就不需要添加@model引用: ...
- express常用代码片段
请求模块: var express = require('express'); var router = express.Router(); // 拿到express框架的路由 var mongoos ...
- 转:npm install 时总是报phantomjs-prebuilt@2.1.14安装失败
该文章转自:http://www.cnblogs.com/alice626/p/6206722.html 在npm install时总是报如下错误, 尝试单独安装:npm install phanto ...
- jQuery选择器之类选择器
类选择器,顾名思义,通过class样式类名来获取节点. 描述: $('.class') 类选择器,相对于id选择器来说,效率相对会低一些,但是优势就是可以多选. 同样的jQuery在实现上,对于类选择 ...