# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
993842 | danikoynov | Wells (CEOI21_wells) | C++14 | 137 ms | 17244 KiB |
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 endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e4 + 10;
int n, k;
vector < int > adj[maxn];
int dist[maxn];
int from[maxn];
void bfs(int v)
{
for (int i = 1; i <= n; i ++)
dist[i] = -1;
queue < int > q;
q.push(v);
dist[v] = 0;
while(!q.empty())
{
v = q.front();
q.pop();
for (int u : adj[v])
{
if (dist[u] == -1)
{
from[u] = v;
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
}
int forb[maxn], path[maxn][maxn];
void bfs_forb(int v)
{
for (int i = 1; i <= n; i ++)
dist[i] = -1;
queue < int > q;
q.push(v);
dist[v] = 0;
while(!q.empty())
{
v = q.front();
q.pop();
for (int u : adj[v])
{
if (forb[u] == 0 && dist[u] == -1)
{
from[u] = v;
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
}
int bruh[maxn][maxn];
void solve()
{
cin >> n >> k;
for (int i = 1; i < n; i ++)
{
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= n; i ++)
{
bfs(i);
for (int j = 1; j <= n; j ++)
path[i][j] = dist[j];
}
for (int v = 1; v <= n; v ++)
{
for (int u = v; u <= n; u ++)
{
// cout << v << " : " << u << endl;
if (path[v][u] >= k - 1)
{
/// cout << v << " :" << u << endl;
bfs(v);
///cout << "shiit" << endl;
//for (int i = 1; i <= n; i ++)
{
// cout << "vertex " << i << " " << from[i] << endl;
}
int cur = u;
vector < int > st;
while(cur != v)
{
///cout << cur << endl;
st.push_back(cur);
cur = from[cur];
}
st.push_back(cur);
for (int d : st)
for (int p : st)
bruh[d][p] = bruh[p][d] = 1;
}
}
}
/**for (int i = 1; i <= n; i ++, cout << endl)
for (int j = 1; j <= n; j ++)
cout << bruh[i][j] << " ";*/
bfs(1);
int mx = 1;
for (int i = 1; i <= n; i ++)
if (dist[mx] < dist[i])
mx = i;
bfs(mx);
int len = 0;
for (int i = 1; i <= n; i ++)
len = max(len, dist[i]);
if (len < k - 1)
{
cout << "YES" << endl;
cout << 0 << endl;
return;
}
for (int s = 1; s <= n; s ++)
{
bfs(s);
vector < int > st;
for (int i = 1; i <= n; i ++)
forb[i] = 0;
for (int i = 1; i <= n; i ++)
{
if (dist[i] % k == 0)
{
forb[i] = 1;
st.push_back(i);
}
}
bool fine = true;
for (int i = 0; i < st.size(); i ++)
for (int j = i + 1; j < st.size(); j ++)
{
if (path[st[i]][st[j]] < k && bruh[st[i]][st[j]] == 1)
{
fine = false;
}
}
if (!fine)
continue;
for (int i = 1; i <= n; i ++)
{
if (forb[i])
continue;
bfs_forb(i);
///cout << "start " << i << endl;
for (int j = 1; j <= n; j ++)
if (dist[j] + 1 >= k)
{
fine = false;
}
}
if (fine)
{
cout << "YES" << endl;
cout << 0 << endl;
return;
}
}
cout << "NO" << endl;
cout << 0 << endl;
}
int main()
{
///speed();
solve();
return 0;
}
Compilation message (stderr)
# | 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... |