#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> adj[402], q[402];
int n, k, st[402], dr[402], level[402], pz;
// void dfs(int u, int p) {
// if (u==p) return;
// if (u) {
// level[u]=level[p]+1;
// }
// if (level[u]==k-1) {
// st[u]=pz++;
// dr[u]=pz;
// }
// st[u]=pz;
// for (auto i : adj[u]) {
// if (i!=p) dfs(i, u);
// }
// dr[u]=pz;
// }
void dfs(int nod, int dad)
{
if(nod)
level[nod] = level[dad] + 1;
if(level[nod] == k-1)
{
st[nod] = pz++;
dr[nod] = pz;
return;
}
st[nod] = pz;
for(int i = 0; i < adj[nod].size(); ++i)
{
int vecin = adj[nod][i];
if(vecin == dad)
continue;
dfs(vecin, dad);
}
dr[nod] = pz;
}
bool dp[402][(1<<20)];
// bool solve() {
// dp[0][0]=1;
// for (int i = 1; i < n; i++) {
// q[st[i]].push_back(i);
// }
// for (int i = 0; i < pz; i++) {
// for (int j = 0; j < (1<<k); j++) {
// if (!dp[i][j]) continue;
// for (auto it : q[i]) {
// if (!(j >> level[it] & 1)) {
// dp[dr[it]][j | (1<<level[it])]=1;
// }
// }
// }
// }
// for (int j = 0; j < (1<<k); j++) {
// if (dp[pz][j]) return 1;
// }
// return 0;
// }
bool solve()
{
dp[0][0] = 1;
for(int i = 1; i < n; ++i)
q[st[i]].push_back(i);
for(int i = 0; i < pz; ++i)
{
for(int j = 0; j < (1<<k); ++j)
{
if (!dp[i][j])
continue;
for(int jj = 0; jj < q[i].size(); ++jj)
{
int it = q[i][jj];
if (!(j >> level[it] & 1))
dp[dr[it]][j | (1<<level[it])] = 1;
}
}
}
for(int j = 0; j < (1<<k); ++j)
if (dp[pz][j])
return 1;
return 0;
}
int main() {
cin >> n >> k;
if (k*k >= n) {
cout << "DA\n";
return 0;
}
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
level[0]=-1;
dfs(0, -1);
cout << (solve() ? "DA\n" : "NE\n");
}
Compilation message
burza.cpp: In function 'void dfs(int, int)':
burza.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < adj[nod].size(); ++i)
| ~~^~~~~~~~~~~~~~~~~
burza.cpp: In function 'bool solve()':
burza.cpp:80:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int jj = 0; jj < q[i].size(); ++jj)
| ~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
218 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
236 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
227 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
255 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
230 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
221 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
247 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
221 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
225 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
233 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |