#include <iostream>
#include <vector>
using namespace std;
int n, k;
vector<vector<int>> adj;
vector<vector<int>> newadj; // ignore nodes deeper than k
vector<int> depth;
vector<vector<int>> parents;
int timer = 0;
vector<int> sttime;
vector<int> fntime;
vector<int> loc;
vector<int> dp (1 << 21);
bool depthCheck(int v, int p) {
if(depth[v] == k) {
return true;
}
bool ret = false;
for(auto next : adj[v]) {
if(next == p) {
continue;
}
depth[next] = depth[v] + 1;
bool option = depthCheck(next, v);
ret |= option;
if(option) {
newadj[v].push_back(next);
}
}
return ret;
}
void dfs(int v, int p) {
parents[v][0] = v;
parents[v][1] = p;
if(p != -1) {
for(int i = 2; i <= depth[v] + 1; ++i) {
// parent of parents[v][i - 1] = parents[v][i]
parents[v][i] = parents[parents[v][i - 1]][1];
}
}
++timer;
// cout << "v, p, timer: " << v << ", " << p << ", " << timer << endl;
sttime[v] = timer;
loc[timer] = v;
for(int next : newadj[v]) {
if(next != p) {
dfs(next, v);
}
}
fntime[v] = timer;
}
int main() {
cin >> n >> k;
if(k > 20) {
cout << "DA" << endl;
return 0;
}
adj = vector<vector<int>>(n + 1);
newadj = vector<vector<int>>(n + 1);
depth = vector<int>(n + 1, 0);
parents = vector<vector<int>>(n + 1, vector<int>(n + 1, -1));
sttime = vector<int>(n + 1);
fntime = vector<int>(n + 1);
loc = vector<int>(n + 1);
for(int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if(!depthCheck(1, -1)) { // graph is too shallow
cout << "DA" << endl;
return 0;
}
dfs(1, -1);
// cout << endl;
// for (int i = 1; i <= n; i++) {
// cout << i << ": " << sttime[i] << " " << fntime[i] << endl;
// }
for(int mask = 0; mask < (1 << k); ++mask) {
int res = dp[mask];
while(res <= timer && depth[loc[res]] < k) {
++res;
}
if(res > timer) {
cout << "DA" << endl;
return 0;
}
int v = loc[res];
for(int i = 0; i < k; ++i) {
if(mask & (1 << i)) {
continue;
}
int p = parents[v][k - (i + 1)];
dp[mask | (1 << i)] = max(dp[mask | (1 << i)], fntime[p] + 1);
}
}
cout << "NE" << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
15 ms |
9164 KB |
Output is correct |
3 |
Correct |
4 ms |
8524 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
5 ms |
9036 KB |
Output is correct |
6 |
Correct |
4 ms |
8516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
9140 KB |
Output is correct |
2 |
Correct |
15 ms |
9148 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
5 ms |
8396 KB |
Output is correct |
5 |
Correct |
15 ms |
9172 KB |
Output is correct |
6 |
Correct |
4 ms |
9164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
9152 KB |
Output is correct |
2 |
Correct |
15 ms |
9076 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
4 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9036 KB |
Output is correct |
2 |
Correct |
16 ms |
9200 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
4 ms |
8780 KB |
Output is correct |
6 |
Correct |
4 ms |
8516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
9036 KB |
Output is correct |
2 |
Correct |
15 ms |
9028 KB |
Output is correct |
3 |
Correct |
4 ms |
8524 KB |
Output is correct |
4 |
Correct |
4 ms |
8524 KB |
Output is correct |
5 |
Correct |
5 ms |
9036 KB |
Output is correct |
6 |
Correct |
5 ms |
8848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
9036 KB |
Output is correct |
2 |
Correct |
15 ms |
9116 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
5 ms |
9036 KB |
Output is correct |
6 |
Correct |
5 ms |
8780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
9172 KB |
Output is correct |
2 |
Correct |
15 ms |
9036 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
15 ms |
9028 KB |
Output is correct |
6 |
Correct |
4 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8920 KB |
Output is correct |
2 |
Correct |
15 ms |
9184 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
5 ms |
8652 KB |
Output is correct |
6 |
Correct |
5 ms |
9164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8852 KB |
Output is correct |
2 |
Correct |
17 ms |
9196 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8524 KB |
Output is correct |
5 |
Correct |
4 ms |
8780 KB |
Output is correct |
6 |
Correct |
6 ms |
9024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8908 KB |
Output is correct |
2 |
Correct |
15 ms |
9164 KB |
Output is correct |
3 |
Correct |
4 ms |
8396 KB |
Output is correct |
4 |
Correct |
4 ms |
8396 KB |
Output is correct |
5 |
Correct |
10 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8652 KB |
Output is correct |