Codeforces 337E Book of Evil (DFS)

Source: Internet
Author: User

Topic Link Http://codeforces.com/contest/337/problem/D


Test instructions: to a tree of n nodes, the right side is 1 . . There is a "demon book" on a node in the tree , which

Book let the nodes within the distance of D be affected. m nodes are now known to have received an impact, asking the most

There are a few more nodes available. can you keep the book of demons?

Idea: First we need to find out the affected points, 22 distance from the furthest point, after that, only need

Calculation has The distance between the points and the two points is less than D. So you can find the answer.

find out how to do the farthest point pair: we first take the 1 node as the root node, calculate the depth of each point at this time, and then

after finding the subject The impact of the point, the depth of the largest one node, and then re-calculate the root node with this node

the depth of each point, with To find out at this time The point at which the depth of the impact is the largest of the point v. So this time u,v this

Two points must be the farthest point pair.


#include <iostream> #include <cstdio> #include <cstring> #include <vector>using namespace std; const int Maxn=100010;vector <int> g[maxn];int n,m,d,a[maxn],dp[maxn],b[maxn],c[maxn];void dfs (int u,int fa) {for    (int i=0;i<g[u].size (); i++)        {int v=g[u][i];        if (V==FA) continue;        dp[v]=dp[u]+1;    DFS (V,U);    }}int Main () {int u,v;    scanf ("%d%d%d", &n,&m,&d);    for (int i=0;i<m;i++) scanf ("%d", &a[i]);        for (int i=1;i<n;i++) {scanf ("%d%d", &u,&v);        G[u].push_back (v);    G[v].push_back (U);    } dp[1]=0;    DFS (1,-1);    int s=a[0],t=a[0];    for (int i=1;i<m;i++) if (Dp[s]<dp[a[i]]) s=a[i];    dp[s]=0;    DFS (S,-1);    memcpy (B,dp,sizeof (DP));    for (int i=1;i<m;i++) if (Dp[t]<dp[a[i]]) t=a[i];    dp[t]=0;    DFS (T,-1);    memcpy (C,dp,sizeof (DP));    int ans=0;    for (int i=1;i<=n;i++) if (B[i]<=d && c[i]<=d) ans++; cout<<ans<<enDL;} 



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

Codeforces 337E Book of Evil (DFS)

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.