-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathColoring trees.cpp
69 lines (57 loc) · 1.14 KB
/
Coloring trees.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* nuttela - Soham Chakrabarti */
#include <bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n';
typedef long long ll;
typedef long double ld;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
vector <int> G[N], deg;
vector <bool> is;
int main() {
IO;
int n, k;
cin >> n >> k;
deg.assign(n + 1, 0);
is.assign(n + 1, false);
for (int i = 1; i < n; ++i)
{
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
++deg[u]; ++deg[v];
}
for (int i = 0; i < k; ++i)
{
int x; cin >> x;
is[x] = true;
}
queue <int> nodes;
for (int i = 1; i < (n + 1); ++i)
{
if(!is[i] && deg[i] == 1) { //leaf nodes and not terminus
nodes.push(i);
}
}
int nvis = 0;
while(!nodes.empty()) {
int data = nodes.front();
// cout << data << endl;
nodes.pop();
for (auto u : G[data]) {
remove(G[u].begin(), G[u].end(), data);
deg[u]--;
}
for (auto u : G[data]) {
if(!is[u] && deg[u] == 1) {
nodes.push(u);
}
}
nvis++;
}
cout << (n - nvis);
cout << endl;
return 0;
}