"CF" 196 Div.2 Book of Evil

Source: Internet
Author: User

Obviously this picture is a lesson tree, looking at the topic first associative to the LCA (certainly can be solved). But looking at the size of the data, it should be tle.
Then, suddenly think of a topic that has been done before, presumably under certain conditions in a tree node rotation becomes the root of the query the longest path.
The result is inspiration, the main idea is that for each node, the first time DFS gets two variables to the P-node maximum and the second-largest value.
Then, the second DFS for the current node u,u to its sub-tree the maximum distance of the P-class node is known (nd[u].mx), then the other nodes except U, v to P-class node maximum distance plus v to u distance and the maximum value of the PMX, can be calculated by each deep search, As long as D is greater than or equal to the maximum value, it is a valid node. And the Pmx method is also very easy, for u, the PMX may be the pmx+1 of its father, or the MX value of the Brother node of v.
Just because we know the maximum value of each node and the sub-large value, the maximum value of all sibling nodes can be obtained. Direct use of int_min, it is easier to achieve.

1 /*337D*/2#include <iostream>3#include <string>4#include <map>5#include <queue>6#include <Set>7#include <stack>8#include <vector>9#include <deque>Ten#include <algorithm> One#include <cstdio> A#include <cmath> -#include <ctime> -#include <cstring> the#include <climits> -#include <cctype> -#include <cassert> -#include <functional> +#include <iterator> -#include <iomanip> + using namespacestd; A //#pragma COMMENT (linker, "/stack:102400000,1024000") at  - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> - #definePII pair<int,int> in #defineVpii vector<pair<int,int> > - #defineRep (I, A, n) for (int i=a;i<n;++i) to #definePer (i, a, n) for (int i=n-1;i>=a;--i) + #defineCLR Clear - #definePB Push_back the #defineMP Make_pair * #defineFIR First $ #defineSEC SecondPanax Notoginseng #defineAll (x) (x). Begin (), (x). End () - #defineSZ (x) ((int) (x). Size ()) the #defineLson L, Mid, rt<<1 + #defineRson mid+1, R, rt<<1|1 A  thetypedefstruct { +     intmx, MX2; - } node_t; $  $ Const intMAXN = 1e5+5; - BOOLMARK[MAXN]; - intN, M, D; the VI E[MAXN]; - node_t ND[MAXN];Wuyi intAns =0; the  - intDfsintUintFA) { Wu     intI, V; -     inttmp; About      $nd[u].mx = ND[U].MX2 =int_min; -     if(Mark[u]) -nd[u].mx =0; -      for(i=0; I<sz (E[u]); ++i) { Av =E[u][i]; +         if(V! =FA) { theTMP = DFS (V, u) +1; -             if(TMP >=nd[u].mx) { $                 //find the fathest distance from p[*] to u theND[U].MX2 =nd[u].mx; thend[u].mx =tmp; the}Else if(tmp >nd[u].mx2) { theND[U].MX2 =tmp; -             } in         } the     } the      About     //-1 means no P in the path the     returnnd[u].mx; the } the  + voidDFS2 (intUintFaintpmx) { -     intI, V; the     intTMP =Max (PMX, nd[u].mx);Bayi      the     if(TMP <=d) { the++ans; -     } -      for(i=0; I<sz (E[u]); ++i) { thev =E[u][i]; the         if(V! =FA) { the             if(nd[v].mx+1==nd[u].mx) theTMP =nd[u].mx2; -             Else theTMP =nd[u].mx; theTMP = MAX (tmp, PMX) +1; the DFS2 (V, U, tmp);94         } the     } the } the 98 intMain () { AboutIos::sync_with_stdio (false); - #ifndef Online_judge101Freopen ("data.in","R", stdin);102Freopen ("Data.out","W", stdout);103     #endif104      the     intu, v;106     107scanf" %d%d%d", &n, &m, &d);108      while(m--) {109scanf"%d", &u); theMark[u] =true;111     } the     113Rep (I,1, N) { thescanf"%d%d", &u, &v); the E[U].PB (v); the E[V].PB (u);117     }118     119     //get the item in node -Dfs1, -1);121     //Calculate the number of valid position122DFS2 (1, -1, int_min);123     124printf"%d\n", ans); the     126 #ifndef Online_judge127printf"Time =%d.\n", (int) clock ()); -     #endif129      the     return 0;131}

"CF" 196 Div.2 Book of Evil

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.