ZOJ 3820 Building Fire Stations (二分+贪心) 2014 牡丹江现场赛B

Building Fire Stations

Time Limit: 5 Seconds       Memory Limit: 131072 KB       Special Judge

Marjar University is a beautiful and peaceful place. There are N buildings and N - 1 bidirectional roads in the campus. These buildings are connected by roads in such a way that there is exactly one path between any two buildings. By coincidence, the length of each road is 1 unit.

To ensure the campus security, Edward, the headmaster of Marjar University, plans to setup two fire stations in two different buildings so that firefighters are able to arrive at the scene of the fire as soon as possible whenever fires occur. That means the longest distance between a building and its nearest fire station should be as short as possible.

As a clever and diligent student in Marjar University, you are asked to write a program to complete the plan. Please find out two proper buildings to setup the fire stations.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (2 <= N <= 200000).

For the next N - 1 lines, each line contains two integers Xi and Yi. That means there is a road connecting building Xi and building Yi (indexes are 1-based).

Output

For each test case, output three integers. The first one is the minimal longest distance between a building and its nearest fire station. The next two integers are the indexes of the two buildings selected to build the fire stations.

If there are multiple solutions, any one will be acceptable.

Sample Input
2
4
1 2
1 3
1 4
5
1 2
2 3
3 4
4 5
Sample Output
1 1 2

1 2 4

题意:给出一颗树,要在两个不同的点建立消防站,每个点会去离自己最近的消防站,问每个点距离自己最近的消防站的最短距离的最长距离是多少。

思路:可以二分一个答案K,然后判断K行不行。可以这样判断,在一棵有根树里面,我们考虑一下叶子,叶子是必须要被覆盖的,那么我们考虑深度最深的叶子,要覆盖这个点,我们可以选择把消防站建在这个叶子的第K个祖先处,而且这是最优的。我们把被覆盖的点都标记一下,然后找另外一个深度最大的没被覆盖的点,还是找他的第K个祖先建立消防站,然后看是不是每个点都被覆盖,如果是,表示这个K是可以的,否则K太小了

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
#define LL long long
#define zero(x) (-eps < (x) && (x) < eps)
#pragma comment(linker,"/STACK:102400000,102400000")
const int maxn = 200000 + 5;
int n;

struct Node
{
	int v;
	Node * next;
}*first[maxn], edges[maxn * 2];
int ptr;

void add(int u, int v)
{
	edges[++ptr].v = v;
	edges[ptr].next = first[u];
	first[u] = &edges[ptr];
}

void input()
{
	scanf("%d", &n);
	clr(first, 0);
	ptr = 0;
	rep(i, 1, n) {
		int u, v; scanf("%d%d", &u, &v);
		add(u, v); add(v, u);
	}
}

int q[maxn], fa[maxn];
void bfs()
{
	int front = 0, rear = 0;
	q[rear++] = 1; clr(fa, -1);
	while (front < rear) {
		int u = q[front++];
		for (Node * p = first[u]; p; p = p->next) {
			int v = p->v;
			if (v == fa[u]) continue;
			fa[v] = u; q[rear++] = v;
		}
	}
}

bool vis[maxn];
int d[maxn];
int Q[maxn];
void Cover(int u, int limit)
{
	clr(d, 0x3f); d[u] = 0;
	int front = 0, rear = 0; Q[rear++] = u;
	while (front < rear) {
		u = Q[front++]; vis[u] = true;
		if (d[u] == limit) continue;
		for (Node * p = first[u]; p; p = p->next) {
			int v = p->v;
			if (d[v] <= d[u] + 1) continue;
			d[v] = d[u] + 1;
			Q[rear++] = v;
		}
	}
}

bool Ok(int limit, int & a, int & b)
{
	int cnt = 0;
	clr(vis, 0);
	rrep(i, n - 1, 0) {
		int u = q[i];
		if (vis[u]) continue;
		++cnt;
		if (cnt > 2) return false;
		rep(j, 0, limit) {
			if (fa[u] == -1) break;
			u = fa[u];
		}
		Cover(u, limit);
		if (cnt == 1) a = u;
		else b = u;
	}
	if (cnt == 1 || a == b) {
		if (a == 1) b = 2;
		else b = 1;
	}
	return true;
}

void solve()
{
	bfs();
	int l = 0, r = n - 1, m;
	int sd = r + 11;
	int a, b;
	while (l <= r) {
		m = l + r >> 1;
		int u, v;
		if (Ok(m, u, v)) {
			if (m < sd)  {
				a = u; b = v;
				sd = m;
			}
			r = m - 1;
		}
		else
			l = m + 1;
	}
	printf("%d %d %d\n", sd, a, b);
}

int main()
{
#ifdef ACM
	freopen("in.txt", "r", stdin);
#endif // ACM
	int T; cin >> T;
	while (T--) {
		input();
		solve();
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值