# 【换根dp】9.22小偷

换根都不会了

## 题目大意

$n,m\le 30000,LIM\le 30000$

## 题目分析

 1 #include<bits/stdc++.h>
2 const int maxn = 30035;
3 const int maxm = 60035;
4
5 int n,m,lim,ans,sum,p[maxn],mx[maxn],dx[maxn];
7 bool tag[maxn];
8
10 {
11     char ch = getchar();
12     int num = 0, fl = 1;
13     for (; !isdigit(ch); ch=getchar())
14         if (ch=='-') fl = -1;
15     for (; isdigit(ch); ch=getchar())
16         num = (num<<1)+(num<<3)+ch-48;
17     return num*fl;
18 }
19 void addedge(int u, int v)
20 {
23 }
24 void dfs1(int x, int fa)
25 {
26     mx[x] = dx[x] = -1;
27     if (tag[x]) mx[x] = 0;
28     for (int i=head[x]; i!=-1; i=nxt[i])
29     {
30         int v = edges[i];
31         if (v==fa) continue;
32         dfs1(v, x);
33         if (mx[v]!=-1&&mx[v]+1 >= mx[x]) dx[x] = mx[x], mx[x] = mx[v]+1;
34         else if (mx[v]!=-1&&mx[v]+1 > dx[x]) dx[x] = mx[v]+1;
35     }
36 }
37 void dfs2(int x, int fa)
38 {
39     for (int i=head[x]; i!=-1; i=nxt[i])
40     {
41         int v = edges[i], val = 0;
42         if (v==fa) continue;
43         if (mx[x]==mx[v]+1&&mx[v]!=-1) val = dx[x]+1;
44         else val = mx[x]+1;
45         if (val&&val >= mx[v]) dx[v] = mx[v], mx[v] = val;
46         else if (val&&val > dx[v]) dx[v] = val;
47         dfs2(v, x);
48     }
49     if (mx[x] <= lim) ++ans;
50 }
51 int main()
52 {
55     for (int i=1; i<=m; i++) tag[read()] = true;
57     dfs1(1, 0);
58     dfs2(1, 0);
59     printf("%d\n",ans);
60     return 0;
61 }

END

posted @ 2019-09-22 21:02  AntiQuality  阅读(186)  评论(0编辑  收藏  举报