#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
#define temp(T) template<class T>
temp(T) using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vvi = V<vi>;
using vpi = V<pi>;
using vb = V<bool>;
#define sz(x) x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
using str = string;
const int MOD = 1e9 + 7;
const ll BIG = 1e18;
const int INF = 1e9 + 2;
temp(T) bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
temp(T) bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; } // set a = max(a,b)
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
unsyncIO();
if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}
#define read(x) int x; cin >> x;
temp(T) void pr(T x) {
cout << to_string(x);
}
temp(T) void prln(T x) {
cout << (x) << "\n";
}
vi readV(int n) {
vi input(n);
F0R(i,n)
cin >> input[i];
return input;
}
int rand(int a, int b) {
return a + rand() % (b - a + 1);
}
#define gen(x,a,b) int x = rand(a,b)
vi genArr(int n, int a, int b) {
vi generated(n);
F0R(i,n) {
generated[i] = rand(a,b);
}
return generated;
}
vvi readTree(int n) {
vvi g(n + 1);
F0R(i,n - 1) {
read(a); read(b);
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvi genTree(int n) {
vvi g(n + 1);
F0R(i,n) {
gen(p,0,i);
g[p].pb(i + 1);
g[i + 1].pb(p);
}
return g;
}
vb imp;
vvi g;
vi cnt;
int k;
int isImp(int x, int p) {
int f = 0;
each(child,g[x]) {
if (child == p) continue;
ckmax(f, isImp(child,x) + 1);
}
if (f < k / 2) imp[x] = false;
return f;
}
bool pos(int x, int p, int h) {
if (h % k >= k / 2 && cnt[x] > 2) return false;
each(child,g[x]) {
if (child == p) continue;
if (!imp[child]) continue;
if (!pos(child,x,h + 1)) return false;
}
return true;
}
int main() {
read(n);
cin >> k;
g = readTree(n);
imp.rsz(n + 1,true);
FOR(i,1,n) {
isImp(i,-1);
}
cnt.rsz(n + 1);
FOR(i,1,n) {
each(child, g[i])
if (imp[child]) cnt[i]++;
}
bool ans = false;
int alive = 0;
FOR(i,1,n) {
if (imp[i]) continue;
alive++;
if (pos(i,-1,0)) {
ans = true;
}
}
if (alive == 0) ans = true;
prln(ans ? "YES" : "NO");
pr(0);
return 0;
}
Compilation message
tortoise.cpp: In function 'void setIn(str)':
tortoise.cpp:49:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
tortoise.cpp: In function 'void setOut(str)':
tortoise.cpp:50:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |