ZOJ 3820 Building Fire Stations (二分+贪心) 2014 牡丹江现场赛B
2014-10-24 18:09
471 查看
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.
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).
two buildings selected to build the fire stations.
If there are multiple solutions, any one will be acceptable.
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 thetwo 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(); } }
相关文章推荐
- zoj 3820 Building Fire Stations(贪心 + 二分)
- ZOJ 3820 Building Fire Stations / 牡丹江现场赛B题
- zoj 3820 Building Fire Stations The 2014 ACM-ICPC Asia Mudanjiang Regional Contest B题 树的直径
- 2014-2015icpc 牡丹江 B.Building Fire Stations (二分+树的直径)
- ZOJ 3820 Building Fire Stations 二分+BFS
- ZOJ 3820 Building Fire Stations(二分+BFS)
- ZOJ 3820 Building Fire Stations (二分)
- ZOJ3820 Building Fire Stations(贪心)
- zoj 3820 Building Fire Stations(二分+bfs)
- ZOJ - 3820 Building Fire Stations
- ZOJ 3820 Building Fire Stations 树的最长链性质(拓展)
- zoj 3820 Building Fire Stations (求树的直径)
- ZOJ 3820 Building Fire Stations
- ZOJ-3820 Building Fire Stations 题解
- zoj 3820 Building Fire Stations(二分法+bfs)
- 【ZOJ】 3820 Building Fire Stations
- ZOJ 3820 Building Fire Stations(树上的问题)
- zoj 3820 Building Fire Stations(树上乱搞)
- ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS
- zoj 3820 Building Fire Stations (树的直径)