ZOJ 3820 Building Fire Stations (two points + greedy) 2014 Mudanjiang live race b

Source: Internet
Author: User
Tags bool time limit
Building Fire Stations Time Limit:5 Seconds Memory limit:131072 KB Special Judge

Marjar University is a beautiful and peaceful place. There is N buildings and N-1 bidirectional roads in the campus. These buildings is connected by roads in such a-path that there is exactly one paths between any and 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-fire stations in both differ ENT buildings So firefighters is 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 is as short as possible.

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

There is multiple test cases. The first line of input contains an integer T indicating the number of the 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 the integers Xi and Yi. That means there was a road connecting building Xi and building Yi (indexes is 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 integers is the indexes of the buildings selected to build the fire stations.

If There is multiple solutions, any one'll 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

Test instructions: Give a tree, to set up a fire station at two different points, each point will go to their nearest fire station, ask each point distance from their nearest fire station of the shortest distance is how much.

idea: Can be two minutes an answer K, and then judge K Line not. It can be judged that in a tree with a root, we consider the leaves, the leaves must be covered, then we consider the deepest depth of the leaves, to cover this point, we can choose to build the fire station in the leaf of the k ancestors, and this is optimal. We mark the covered points, and then find another depth of the largest not covered point, or find his first k ancestors to build a fire station, and then see whether each point is covered, if so, that K is okay, otherwise k is too small

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cassert> #include <queue&
Gt
#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) #p
Ragma 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 (); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.