QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402591 | #6742. Leaves | mulberry | WA | 0ms | 3764kb | C++23 | 2.6kb | 2024-04-30 23:44:16 | 2024-04-30 23:44:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int rt;
int n, m;
int dep[2005];
int a[2005], fa[2005];
bool mark[2005];
int L[2005], R[2005];
void up(int u){
while(!mark[u]){
mark[u] = 1;
u = fa[u];
}
}
int find(int u){
if(!mark[u])return u;
int f = 0;
if(L[u])f = find(L[u]);
if(!f && R[u])f = find(R[u]);
return f;
}
struct node{
int id, num, sum;
}tmp[2005];
bool cmp(node a, node b){
if(a.num != b.num) return a.num < b.num;
return a.sum < b.sum;
}
int tot;
void collect(int u, int sum){
if(!L[u] && !R[u]){
++tot;
tmp[tot] = node{u, a[u], sum};
return;
}
if(L[u])collect(L[u], sum);
if(R[u])collect(R[u], sum + 1);
}
int num = 0;
int ans[20005];
void print(int u){
if(!L[u] && !R[u]){
ans[++num] = a[u];
// printf("%d ", a[u]);
return;
}
print(L[u]);
print(R[u]);
}
int main(){
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++){
int f, u, v;
scanf("%d", &f);
if(f == 1){
scanf("%d%d", &u, &v);
L[i] = u;
R[i] = v;
fa[u] = i, fa[v] = i;
}else{
scanf("%d", &u);
a[i] = u;
}
}
for (int i = 1; i <= n; i++)
if(fa[i] == 0)rt = i;
// printf("rt = %d\n", rt);
// dfs(rt);
while(m){
int s = find(rt), x = 0;
if(!s)break;
tot = 0;
collect(s, 0);
// printf("s = %d tot = %d\n", s, tot);
sort(tmp + 1, tmp + tot + 1, cmp);
for (int i = 1; i <= tot ; i++){
// printf(" id = %d a = %d sum = %d\n", tmp[i].id, tmp[i].num, tmp[i].sum);
// cout << tmp[i].s << endl;
if(m >= tmp[i].sum){
m -= tmp[i].sum;
x = tmp[i].id;
int t = x;
while(t != s){
if(t == R[fa[t]]){
// printf(" swap = %d %d\n", L[fa[t]], R[fa[t]]);
swap(L[fa[t]], R[fa[t]]);
}
t = fa[t];
}
break;
}
}
if(!x)break;
up(x);
mark[x] = mark[R[fa[x]]] = 1;
}
print(rt);
if(m % 2 == 1)swap(ans[tot - 1], ans[tot]);
for(int i = 1; i <= tot; i++)
printf("%d ", ans[i]);
}
/*
15 4
1 2 3
1 4 5
1 6 7
1 8 9
1 10 11
1 12 13
1 14 15
2 7
2 8
2 6
2 5
2 1
2 2
2 4
2 3
2 4
2 2
2 3
2 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3764kb
input:
3 0 1 2 3 2 1 2 2
output:
result:
wrong answer Answer contains longer sequence [length = 2], but output contains 0 elements