-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc_2016_s3.cpp
52 lines (48 loc) · 991 Bytes
/
ccc_2016_s3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
James Cai
CCC '16 S3 - Phonomenal Reviews
C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N,M,root,far,len,cnt;
bool phoRest[100001];
vector<int> adj[100001];
void dfs1(int u, int par) {
for(int v: adj[u]) {
if(v==par) continue;
dfs1(v,u);
phoRest[u] |= phoRest[v];
}
}
void dfs2(int u, int par, int dis) {
if(dis > len) {
len = dis;
far = u;
}
for(int v: adj[u]) {
if(v == par || !phoRest[v]) continue;
dfs2(v,u,dis+1);
}
}
int main() {
cin >> N >> M;
for(int i = 0,x; i < M; i++) {
cin >> x;
phoRest[x] = true;
root = x;
}
for(int i = 1, u, v; i < N; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(root, -1);
dfs2(root, -1, 0);
dfs2(far, -1, 0);
for(int i = 0; i < N; i++) {
cnt += phoRest[i];
}
cout << 2*(cnt-1) - len << endl;
}