Zoj 3820 building fire stations greedy + Tree Diameter

Source: Internet
Author: User

I don't know why it is right, but I can't give a counterexample. During the competition, my teammates proposed to find the diameter of the tree, but I didn't dare to write it. It's a pity.

The specific method is to first find the diameter of the original tree, and then delete an edge in the middle to change it into two Subtrees. The diameter midpoint of the two Subtrees is the answer.

# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> # include <stack> # include <map> # include <set> # include <climits> # include <iostream> # include <string> using namespace STD; # define MP make_pair # define Pb push_backtypedef long ll; typedef unsigned long ull; typedef vector <int> VI; typedef pair <int, int> PII; typedef pair <double, double> PDD; const int INF = int_max/3; const do Uble EPS = 1e-8; const ll linf = 1e17; const double dinf = 1e60; const int maxn = 2e5 + 10; int head [maxn], NXT [maxn <1], U [maxn <1], V [maxn <1], ecnt; int N, DIST [maxn], pre [maxn], path [maxn], id [maxn]; bool vis [maxn], del [maxn <1]; void Adde (INT Uu, int vv) {u [ecnt] = Uu; V [ecnt] = vv; NXT [ecnt] = head [UU]; head [UU] = ecnt ++;} // The BFS parameter is the starting point, returns the end point (the farthest point from the start point) int BFS (int str, int col) {memset (VIS, 0, sizeof (VIS); vis [STR] = true; Dist [STR] = 0; Pre [STR] =-1; queue <int> q; q. push (STR); int now = 0; while (! Q. Empty () {now = Q. Front (); q. Pop (); Id [now] = Col; For (INT I = head [now]; ~ I; I = NXT [I]) if (! Del [I]) {If (! Vis [V [I]) {pre [V [I] = now; Dist [V [I] = DIST [now] + 1; q. push (V [I]); vis [V [I] = true; }}return now;} void solve () {// The First Time, the diameter of the tree is int S, T, Len = 0, Du, DV; S = BFS (1,-1); t = BFS (S,-1 ); for (INT I = T ;~ I; I = pre [I]) {path [Len ++] = I;} // Delete the center edge for (INT I = T, J = 0 ;~ I; I = pre [I], J ++) {If (J + 1) = Len/2) {du = I; DV = pre [I]; break ;}for (INT I = 0; I <ecnt; I ++) {If (U [I] = du & V [I] = DV) | (U [I] = DV & V [I] = DU) {del [I] = true ;}} // calculate the diameter of int maxlen = 0, P [2], PCNT = 0, for (INT I = 1; I <= N; I ++) for the subtree on both sides) if (ID [I] =-1) {S = BFS (I, PCNT); t = BFS (S, PCNT); Len = 0; for (Int J = T ;~ J; j = pre [J]) path [Len ++] = J; For (Int J = T, K = 0 ;~ J; j = pre [J], K ++) if (k + 1) = (LEN + 1)/2) {P [PCNT ++] = J; break;} maxlen = max (maxlen, Len/2);} printf ("% d \ n", maxlen, P [0], P [1]) ;}int main () {int t; scanf ("% d", & T); While (t --) {ecnt = 0; scanf ("% d", & N); memset (Head,-1, sizeof (head); memset (Del, 0, sizeof (DEL )); for (INT I = 0; I <n-1; I ++) {int A, B; scanf ("% d", & A, & B ); ADDE (a, B); Adde (B, A);} solve ();} return 0 ;}

 

Zoj 3820 building fire stations greedy + Tree Diameter

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.