/*
COCI 16-burza
* If k^2 >= n, the answer is DA.
* Otherwise, let's define a bitmask dp, dp[i][mask] = 1 if we can cover the first i leaves using nodes forming that mask.
(leave = node at height k). In order to compute the transitions easier, we are going to do a dfs to find the dfs order of the tree
(left_i = leftmost leaf in subtree of i, right_i = rightmost leaf in subtree of i)
* From this point on, the transition is pretty simple, for every starting left point, we're going to update the dp matrix according to
the previous states.
* Let's say we want to go to cover the next leaves and we are at position i and mask j. From dp[i][j] we can go to all dp[right_x][j + 2^level_x],
where level_x is the level of node x.
* If we found that we can cover all the leaves using some bitmask, the answer is DA. Otherwise, it's NE.
*/
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
using namespace std;
typedef long long ll;
int add(int a, int b)
{
ll x = a+b;
if(x >= mod)
x -= mod;
if(x < 0)
x += mod;
return x;
}
ll mul(ll a, ll b)
{
return (a*b) % mod;
}
ll pw(ll a, ll b)
{
ll ans = 1;
while(b)
{
if(b & 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
vector<int>v[402], q[402];
int n, k, level[402], st[402], dr[402], pz;
void dfs(int dad, int nod)
{
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 < v[nod].size(); ++i)
{
int vecin = v[nod][i];
if(vecin == dad)
continue;
dfs(nod, vecin);
}
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(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()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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;
v[a].pb(b);
v[b].pb(a);
}
level[0] = -1;
dfs(-1, 0);
cout << (solve() ? "DA" : "NE");
return 0;
}
Compilation message
burza.cpp: In function 'void dfs(int, int)':
burza.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < v[nod].size(); ++i)
| ~~^~~~~~~~~~~~~~~
burza.cpp: In function 'bool solve()':
burza.cpp:94:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int jj = 0; jj < q[i].size(); ++jj)
| ~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
980 KB |
Output is correct |
2 |
Correct |
40 ms |
2548 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
2708 KB |
Output is correct |
2 |
Correct |
40 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
65 ms |
3004 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
2784 KB |
Output is correct |
2 |
Correct |
45 ms |
2540 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1256 KB |
Output is correct |
2 |
Correct |
60 ms |
3292 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
2232 KB |
Output is correct |
2 |
Correct |
52 ms |
2892 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
2696 KB |
Output is correct |
2 |
Correct |
50 ms |
2764 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
3032 KB |
Output is correct |
2 |
Correct |
51 ms |
2792 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
57 ms |
3144 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
1200 KB |
Output is correct |
2 |
Correct |
50 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
724 KB |
Output is correct |
2 |
Correct |
58 ms |
2936 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
1628 KB |
Output is correct |
2 |
Correct |
60 ms |
2984 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
23 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |