-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultihedgehog.cpp
More file actions
93 lines (74 loc) · 1.61 KB
/
multihedgehog.cpp
File metadata and controls
93 lines (74 loc) · 1.61 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// https://codeforces.com/contest/1068/problem/E
#include <iostream>
#include <vector>
using namespace std;
int n = 100002;
vector<vector<int>> adjList(n+1);
vector<int> degrees(n+1);
vector<int> sizes(n+1);
vector<int> pre(n+1);
vector<int> depths(n+1);
int mxdepth;
int dex;
void dfs (int node, int maxdep, int par) {
depths[node] = maxdep;
if (maxdep > mxdepth) {
mxdepth = maxdep;
dex = node;
}
for (auto i: adjList[node]) {
if (i == par) {
continue;
}
sizes[node]++;
pre[i] = node;
dfs(i, maxdep+1, node);
}
}
int main ()
{
int k;
cin >> n >> k;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
adjList[x].push_back(y);
adjList[y].push_back(x);
degrees[x]++;
degrees[y]++;
}
int currRoot = 0;
for (int i = 1; i <= n; i++) {
if (degrees[i] == 1) {
currRoot = i;
break;
}
}
dfs(currRoot, 1, 0);
int mid = (mxdepth+1)>>1;
while (depths[dex] != mid) {
dex = pre[dex];
}
currRoot = dex;
sizes.clear();
sizes.resize(n+1, 0);
depths.clear();
depths.resize(n+1, 0);
dfs(currRoot, 1, 0);
bool is = true;
for (int i = 1; i <= n; i++) {
if (sizes[i] == 0) {
if (depths[i] != k+1) {
is = false;
break;
}
} else {
if (sizes[i] < 3) {
is = false;
break;
}
}
}
if (is) cout << "YES" << "\n";
else cout << "NO" << "\n";
}