QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#117987 | #2411. Balanced Cut | sanskar_123 | WA | 11ms | 75180kb | C++20 | 2.5kb | 2023-07-02 19:36:26 | 2023-07-02 19:36:27 |
Judging History
answer
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long LL;
const int maxn=5e5+5, MX=30;
const int inf=522133279;
int n,k,root, son [maxn][2], p[maxn];
int f[maxn] [MX+2], nmax[maxn], deep[maxn];
void dfs_dp(int k) {
nmax[k]=k;
int l=son[k][0], r=son[k][1];
if (l)
{
deep[l] = deep[k]+1;
dfs_dp (l);
}
if (r)
{
deep[r]=deep[k]+1;
dfs_dp(r);
nmax[k]=max(nmax[k], nmax[r]);
}
f[k][1]=1;
fo(j,2,MX) f[k][j]=min(f[k][j],min(f[l][j-1]+f[r][j-2],f[l][j-2]+f[r][j-1])+1);
}
int tag[maxn], maxdeep [maxn], num[maxn], h[MX+2];
void Make_tag(int st){
for (int x=st; x!=-1; x=p[x])
{
maxdeep [x]=max(maxdeep[x], deep[st]);
num [x]++;
int y=p[x];
if (y!=-1 && son[y][0]==x) tag[son[y][1]]=max(tag[son[y][1]], maxdeep[x]-1-deep[y]);
}
}
bool check(int x)
{
if (num[x]) return 1;
int nowdeep=0, nownum=0;
memset(h, 31, sizeof(h));
h[0]=f[son[x][1]][0], h[1]=f[son[x][1]][1];
for(; x!=-1; x=p[x])
{
int y=p[x];
nownum++;
if (nowdeep+1<tag[x])
{
nownum+=h[nowdeep-tag[x]-1];
if (nownum>=inf) return 0;
fd(i,MX, nowdeep) if (h[i]<inf) h[i]-=h[nowdeep];
}
nowdeep++;
fd(i,MX, nowdeep) h[i]=h[i-1];
if (y!=-1 && son[y][0]==x && son[y][1])
{
int r=son[y][1], t=max(tag[r], nowdeep-1);
if (f[r][t]>=inf) return 0;
nownum+=f[r][t];
fd(i,MX, nowdeep+1)
{
h[i]=min(h[i-1]+f[r][i]-f[r][t],h[i]+f[r][i-1]-f[r][t]);
if (h[i]>n) h[i]=inf;
}
h[nowdeep]=0;
} else if (y!=-1 && son [y][1]==x && son[y][0])
{
int l=son[y][0];
if (nowdeep-(maxdeep[l]-deep[y])>1) return 0;
nowdeep=max(nowdeep, maxdeep[l]-deep[y]);
h[nowdeep]=0;
nownum+=num[son[y][0]];
}
}
return nownum<=k;
}
bool ans[maxn];
int main()
{
scanf("%d %d",&n,&k);
fo(i,1,n)
{
scanf("%d",&p[i]);
if (p[i]==-1) root=i; else son[p[i]][(i>p[i])]=i;
}
memset(f, 31, sizeof(f));
fo(i,0,n) f[i][0]=0;
deep[root]=1;
dfs_dp (root);
fo(i,1,n) if (check(i))
{
ans[i]=1;
Make_tag(i);
}
fo(i,1,n) putchar(ans[i] ?'1' : '0');
puts("");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 75128kb
input:
2 1 -1 1
output:
10
result:
ok correct
Test #2:
score: 0
Accepted
time: 11ms
memory: 75180kb
input:
2 1 2 -1
output:
01
result:
ok correct
Test #3:
score: 0
Accepted
time: 3ms
memory: 73132kb
input:
24 16 2 4 2 8 6 4 6 16 10 12 10 8 14 12 14 -1 18 19 21 19 16 23 21 23
output:
111111110101010101101010
result:
ok correct
Test #4:
score: 0
Accepted
time: 3ms
memory: 73144kb
input:
12 6 2 3 5 3 8 7 5 -1 10 11 8 11
output:
001010110110
result:
ok correct
Test #5:
score: 0
Accepted
time: 9ms
memory: 73328kb
input:
24 12 2 4 2 8 6 4 6 16 10 12 10 8 14 12 14 -1 18 19 21 19 16 23 21 23
output:
110101010101000101101010
result:
ok correct
Test #6:
score: 0
Accepted
time: 4ms
memory: 73328kb
input:
54 38 2 3 5 3 8 7 5 13 10 11 8 11 21 15 16 18 16 13 20 18 34 23 24 26 24 29 28 26 21 31 32 29 32 -1 36 37 39 37 42 41 39 47 44 45 42 45 34 49 50 52 50 47 54 52
output:
011111111111101111011011010110110101101011011010110101
result:
ok correct
Test #7:
score: 0
Accepted
time: 1ms
memory: 73104kb
input:
25 21 2 3 5 3 9 7 5 7 -1 11 12 14 12 18 16 14 16 9 20 22 20 18 24 22 24
output:
1111111111111111011101010
result:
ok correct
Test #8:
score: 0
Accepted
time: 0ms
memory: 73108kb
input:
40 20 2 4 2 8 6 4 6 19 10 13 10 11 8 16 14 13 16 17 -1 21 23 21 28 25 23 25 26 19 30 33 30 31 28 36 34 33 38 36 38 39
output:
1111111111101101101010100001000010000000
result:
ok correct
Test #9:
score: 0
Accepted
time: 4ms
memory: 73340kb
input:
609 399 2 3 5 3 8 7 5 13 10 11 8 11 21 15 16 18 16 13 20 18 34 23 24 26 24 29 28 26 21 31 32 29 32 55 36 37 39 37 42 41 39 47 44 45 42 45 34 49 50 52 50 47 54 52 89 57 58 60 58 63 62 60 68 65 66 63 66 76 70 71 73 71 68 75 73 55 78 79 81 79 84 83 81 76 86 87 84 87 144 91 92 94 92 97 96 94 102 99 100 ...
output:
011111111111111111111111111111111111111111111111111111101111111011110110101101101011011010110101101101011010110110101101101011010110110101101011011010110110101101011011010110110101101011011010110101101101011011010110101101101011011010110101101101011010110110101101101011010110110101101011011010110110...
result:
ok correct
Test #10:
score: 0
Accepted
time: 0ms
memory: 73224kb
input:
609 326 2 5 2 3 13 8 6 5 10 8 10 11 34 16 14 21 18 16 18 19 13 23 26 23 24 21 29 27 26 31 29 31 32 89 37 35 42 39 37 39 40 55 44 47 44 45 42 50 48 47 52 50 52 53 34 57 60 57 58 68 63 61 60 65 63 65 66 55 71 69 76 73 71 73 74 68 78 81 78 79 76 84 82 81 86 84 86 87 233 92 90 97 94 92 94 95 110 99 102 ...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111101101010010100100000001111111010000...
result:
ok correct
Test #11:
score: 0
Accepted
time: 3ms
memory: 73292kb
input:
16383 4134 2 4 2 8 6 4 6 16 10 12 10 8 14 12 14 32 18 20 18 24 22 20 22 16 26 28 26 24 30 28 30 64 34 36 34 40 38 36 38 48 42 44 42 40 46 44 46 32 50 52 50 56 54 52 54 48 58 60 58 56 62 60 62 128 66 68 66 72 70 68 70 80 74 76 74 72 78 76 78 96 82 84 82 88 86 84 86 80 90 92 90 88 94 92 94 64 98 100 9...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
result:
ok correct
Test #12:
score: 0
Accepted
time: 0ms
memory: 73320kb
input:
7 5 2 3 5 3 -1 7 5
output:
0111101
result:
ok correct
Test #13:
score: 0
Accepted
time: 1ms
memory: 73320kb
input:
13 3 2 3 5 3 9 7 5 7 -1 11 9 13 11
output:
0000100010100
result:
ok correct
Test #14:
score: 0
Accepted
time: 0ms
memory: 73156kb
input:
13 8 2 3 5 3 9 7 5 7 -1 11 9 13 11
output:
0111101011100
result:
ok correct
Test #15:
score: 0
Accepted
time: 1ms
memory: 75132kb
input:
13 11 2 3 5 3 9 7 5 7 -1 11 9 13 11
output:
0111111111101
result:
ok correct
Test #16:
score: 0
Accepted
time: 0ms
memory: 73136kb
input:
13 12 2 3 5 3 9 7 5 7 -1 11 9 13 11
output:
1111111011111
result:
ok correct
Test #17:
score: 0
Accepted
time: 1ms
memory: 75172kb
input:
20 18 3 1 8 5 3 5 6 -1 10 13 10 11 8 16 14 13 18 16 18 19
output:
11111111111111111100
result:
ok correct
Test #18:
score: -100
Wrong Answer
time: 1ms
memory: 75156kb
input:
21 20 2 3 5 3 8 5 6 13 11 9 8 11 -1 15 17 15 13 19 17 19 20
output:
111111111111111111110
result:
wrong answer No AVL tree as answer.