This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fe(x, y) for(auto& x : y)
#define fi first
#define se second
#define pb push_back
#define pw(x) (1LL << (x))
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
template <typename T>
using ve = vector <T>;
template <typename T>
bool umx(T& a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T>
bool umn(T& a, T b) { return a > b ? a = b, 1 : 0; }
using ll = long long;
using pll = pair <ll, ll>;
using pii = pair <int, int>;
const int oo = 2e9;
const ll OO = 2e18;
const int N = 2e5 + 10;
ve <int> G[N];
int up[N][22];
int timer;
int tin[N];
int tout[N];
int n, k;
int clrd[N];
int dep[N];
bool bd = 0;
void dfs1(int u, int p) {
if (u) dep[u] = dep[p] + 1;
up[u][0] = p;
tin[u] = timer++;
fr(i, 1, 20) up[u][i] = up[up[u][i - 1]][i - 1];
fe(to, G[u]) if (to != p) dfs1(to, u);
tout[u] = timer;
}
bool isupper(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; }
int lca(int u, int v) {
if (isupper(u, v)) return u;
if (isupper(v, u)) return v;
rf(i, 20, 0) if (!isupper(up[u][i], v)) u = up[u][i];
return up[u][0];
}
int mxw[N];
void dfs(int u, int p, int dst) {
mxw[u] = 1;
if (dst % k == 0) {
clrd[u] = 1;
mxw[u] = 0;
}
ve <int> v;
fe(to, G[u]) if (to != p) {
dfs(to, u, dst + 1);
if (clrd[u] == 0) {
umx(mxw[u], mxw[to] + 1);
v.pb(mxw[to]);
}
}
sort(all(v));
reverse(all(v));
if (sz(v) > 1 && v[0] + v[1] + 1 >= k) bd = 1;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
fr(i,0, n - 2) {
int u, v; cin >> u >> v; u--; v--;
G[u].pb(v);
G[v].pb(u);
}
dfs1(0, 0);
// cout << "NO\n"; return 0;
fr(i, 0, n - 1) {
dfs(i, i, 0);
ve <int> v;
fr(j, 0, n - 1) if(clrd[j]) v.pb(j);
fr(i, 0, n - 1) clrd[i] = 0;
bool bad = 0;
if (!bd) {
fr(x, 0, sz(v) - 1) {
fr(y, x + 1, sz(v) - 1) {
int C = lca(v[x], v[y]);
if (dep[v[x]] + dep[v[y]] - 2 * dep[C] < k) {
bad = 1;
break;
}
}
if (bad) break;
}
}
// cout << i << "\n";
// fe(x, v) cout << x << " ";
if(!(bad || bd)) {
cout << "YES\n1\n";
exit(0);
}
// cout << "\n";
bd = 0;
}
cout << "NO\n0\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |