#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define long unsigned long
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 1500007;
vector <int> graph[N];
int well[N], depth[N], n, k;
bool flag;
void dfs(int v = 1, int p = 1) {
for (auto to : graph[v]) {
if (to == p) continue;
depth[to] = depth[v] + 1;
dfs(to, v);
}
}
int find_wells(int v = 1, int p = 1) {
vector <int> depths;
for (auto to : graph[v]) {
if (to == p) continue;
auto res = find_wells(to, v);
if (res) {
depths.pb(res);
}
}
sort(rall(depths));
if ((sz(depths) && depths[0] >= k-1) || (sz(depths) > 1 && depths[0]+depths[1] >= k-1)) {
well[v] = 1;
return 0;
}
if (sz(depths)) return depths[0]+1;
else return 1;
}
int find_dbw(int v = 1, int p = 1) {
vector <int> depths;
for (auto to : graph[v]) {
if (to == p) continue;
auto res = find_dbw(to, v);
if (res) {
depths.pb(res);
}
}
sort(all(depths));
if ((well[v] && sz(depths) && depths[0] < k) || (sz(depths) > 1 && depths[0]+depths[1] < k)) {
cout << "NO\n0";
exit(0);
}
if (well[v]) return 1;
else if (sz(depths)) return depths[0]+1;
else return 0;
}
void solve() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
dfs();
find_wells();
find_dbw(); // distance between wells
if (flag) cout << "NO\0";
else cout << "YES\n1";
}
int main() {
do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
18 ms |
35540 KB |
Output is partially correct |
2 |
Incorrect |
17 ms |
35540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
18 ms |
35540 KB |
Output is partially correct |
2 |
Incorrect |
17 ms |
35540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
18 ms |
35540 KB |
Output is partially correct |
2 |
Incorrect |
17 ms |
35540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
18 ms |
35540 KB |
Output is partially correct |
2 |
Incorrect |
17 ms |
35540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |