Codeforces 337D D. Book of Evil (tree DP)

Source: Internet
Author: User

Topic Links:

Codeforces 337D

Main topic:

Give a tree, give the infection range, give some identified the infected point, ask the number of points the infection may be placed.

Topic Analysis:
    • Define state Dp[i] represents the distance at which a point reaches the identified point of infection farthest from it.
    • Then we first DFS again, the tree with 1 roots, each point to the maximum distance of the infection point in the subtree, and then DFS again, to obtain dp[i] the required value, using a dd[i] array to represent the father of point I, in addition to their own and the root of the sub-tree point of infection point to their farthest distance.
    • Then processing out each point son's prefix and suffix of the maximum value, and then use DD can be obtained dp[i], the specific transfer process see code, relatively simple, unclear local comment area ...
AC Code:
#include <iostream># include <cstring>#include <cstdio>#include <algorithm>#include <vector>#define MAX 100007#define INF (-(1<<29));using namespace STD;intN,m,d;intDp[max];intDd[max];intMark[max];intLef[max],rig[max]; vector<int>E[max];voidAdd (intU,intV) {e[u].push_back (v); E[v].push_back (u);}voidDFS (intU,intP) {if(Mark[u]) dp[u] =0;ElseDp[u] = INF; for(inti =0; I < e[u].size (); i++) {intv = e[u][i];if(v = = p)Continue;        DFS (v, u); Dp[u] = max (Dp[u], Dp[v] +1); }}voidDFS1 (intU,intP) {lef[0] = INF; Rig[e[u].size () +1] = INF; for(inti =0; I < e[u].size (); i++) {intx = i+1;intv = e[u][i];if(v = = p) {Lef[x] = lef[x-1];Continue; } Lef[x] = max (lef[x-1], dp[v]); } for(inti = e[u].size ()-1; I >=0; i--) {intx = i+1;intv = e[u][i];if(v = = p) {Rig[x] = rig[x+1];Continue; } Rig[x] = max (rig[x+1], dp[v]); } for(inti =0; I < e[u].size (); i++) {intv = e[u][i];if(v = = p)Continue; DD[V] = max (Dd[u], Max (lef[i), rig[i+2])+1)+1;    DP[V] = max (Dp[v], dd[v]); } for(inti =0; I < e[u].size (); i++) {intv = e[u][i];if(v = = p)Continue;    DFS1 (V, u); }   }voidClear () { for(inti =0; i < MAX; i++) E[i].clear ();}intMain () { while( ~scanf("%d%d%d", &n, &m, &d)) {intx, y; Clear ();memset(Mark,0,sizeof(Mark));memset(DD,-0x3f,sizeof(DD)); for(inti =0; I < m; i++) {scanf("%d", &x); MARK[X] =1; } for(inti =1; I < n; i++) {scanf("%d%d", &x, &y);        Add (x, y); } DFS (1, -1);if(mark[1]) dd[1] =0;Elsedd[1] = INF; DFS1 (1, -1);intAns =0; for(inti =1; I <= N; i + +)if(Dp[i] <= d && Dp[i] >=0) ans++;printf("%d\n", ans); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 337D D. Book of Evil (tree DP)

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.