QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#50131 | #1668. Guide | yllcm | AC ✓ | 11ms | 27196kb | C++17 | 2.1kb | 2022-09-24 17:05:48 | 2022-09-24 17:05:51 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define FR first
#define SE second
using namespace std;
inline int read() {
int x = 0; bool op = 0;
char c = getchar();
while(!isdigit(c))op |= (c == '-'), c = getchar();
while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return op ? -x : x;
}
const int N = 1e6 + 10;
int n, k;
int dep[N], sz[N], son[N], d[N], F[N];
vector<int> G[N];
void dfs1(int u, int fa) {
dep[u] = dep[fa] + 1; sz[u] = 1; F[u] = fa;
for(int v : G[u]) {
if(v == fa)continue;
dfs1(v, u); sz[u] += sz[v];
if(son[u] == 0 || d[v] + 1 > d[u]) {
d[u] = d[v] + 1; son[u] = v;
}
}
return ;
}
vector<int> ans;
void clr() {
ans.clear();
for(int i = 1; i <= n; i++)G[i].clear();
for(int i = 1; i <= n; i++)d[i] = sz[i] = 0;
return ;
}
void dfs2(int u, int fa, int s) {
if(s == 0)return ;
s--; ans.pb(u);
for(int v : G[u]) {
if(v == fa || s == 0)continue;
dfs2(v, u, min(s, sz[v]));
s -= min(s, sz[v]); ans.pb(u);
}
return ;
}
void solve() {
n = read(); k = read();
// for(int i = 1; i < n; i++) {
// int u = read(), v = read();
// G[u].pb(v); G[v].pb(u);
// }
for(int i = 2; i <= n; i++) {
int u = read();
G[u].pb(i); G[i].pb(u);
}
dfs1(1, 0);
int mx = 0;
for(int i = 1; i <= n; i++) {
if(dep[i] <= k)mx = max(mx, dep[i]);
}
int u = 1, c = 1, s = k - mx, lst = 0;
for(; c <= mx; lst = u, u = son[u], c++) {
ans.pb(u);
for(int v : G[u]) {
if(v == lst || v == son[u] || s == 0)continue;
dfs2(v, u, min(s, sz[v])); s -= min(s, sz[v]); ans.pb(u);
}
}
printf("%d\n", (int)ans.size() - 1);
for(int u : ans)printf("%d ", u); putchar('\n');
return clr(), void();
}
int main() {
int test = read();
while(test--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 27076kb
input:
3 6 2 1 1 2 2 3 6 6 1 1 2 2 3 6 4 1 2 3 4 5
output:
1 1 2 8 1 3 6 3 1 2 5 2 4 3 1 2 3 4
result:
ok All testcases are passed!
Test #2:
score: 0
Accepted
time: 4ms
memory: 27192kb
input:
3 1 1 2 1 1 2 2 1
output:
0 1 0 1 1 1 2
result:
ok All testcases are passed!
Test #3:
score: 0
Accepted
time: 10ms
memory: 27112kb
input:
10 10 1 1 2 1 4 4 1 6 5 2 10 2 1 2 1 4 4 1 6 5 2 10 3 1 2 1 4 4 1 6 5 2 10 4 1 2 1 4 4 1 6 5 2 10 5 1 2 1 4 4 1 6 5 2 10 6 1 2 1 4 4 1 6 5 2 10 7 1 2 1 4 4 1 6 5 2 10 8 1 2 1 4 4 1 6 5 2 10 9 1 2 1 4 4 1 6 5 2 10 10 1 2 1 4 4 1 6 5 2
output:
0 1 1 1 4 2 1 4 5 3 1 4 5 9 5 1 2 1 4 5 9 7 1 2 3 2 1 4 5 9 9 1 2 3 2 10 2 1 4 5 9 11 1 2 3 2 10 2 1 7 1 4 5 9 13 1 2 3 2 10 2 1 7 1 4 6 4 5 9 15 1 2 3 2 10 2 1 7 1 4 6 8 6 4 5 9
result:
ok All testcases are passed!
Test #4:
score: 0
Accepted
time: 11ms
memory: 27080kb
input:
100 100 1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 100 2 1 1 2...
output:
0 1 1 1 2 2 1 2 4 3 1 2 4 8 4 1 2 4 8 16 5 1 2 4 8 16 32 6 1 2 4 8 16 32 64 8 1 3 1 2 4 8 16 32 64 10 1 3 6 3 1 2 4 8 16 32 64 12 1 3 6 12 6 3 1 2 4 8 16 32 64 14 1 3 6 12 24 12 6 3 1 2 4 8 16 32 64 16 1 3 6 12 24 48 24 12 6 3 1 2 4 8 16 32 64 18 1 3 6 12 24 48 96 48 24 12 6 3 1 2 4 8 16...
result:
ok All testcases are passed!
Test #5:
score: 0
Accepted
time: 6ms
memory: 27152kb
input:
100 100 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
0 1 1 1 2 2 1 2 3 3 1 2 3 4 4 1 2 3 4 5 5 1 2 3 4 5 6 6 1 2 3 4 5 6 7 7 1 2 3 4 5 6 7 8 8 1 2 3 4 5 6 7 8 9 9 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 11 11 1 2 3 4 5 6 7 8 9 10 11 12 12 1 2 3 4 5 6 7 8 9 10 11 12 13 13 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 1 2 3 4 5 6 7 8 9 10 11 12...
result:
ok All testcases are passed!
Test #6:
score: 0
Accepted
time: 2ms
memory: 27024kb
input:
100 100 1 1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 17 17 19 19 21 21 23 23 25 25 27 27 29 29 31 31 33 33 35 35 37 37 39 39 41 41 43 43 45 45 47 47 49 49 51 51 53 53 55 55 57 57 59 59 61 61 63 63 65 65 67 67 69 69 71 71 73 73 75 75 77 77 79 79 81 81 83 83 85 85 87 87 89 89 91 91 93 93 95 95 97 97 99 100...
output:
0 1 1 1 3 2 1 3 5 3 1 3 5 7 4 1 3 5 7 9 5 1 3 5 7 9 11 6 1 3 5 7 9 11 13 7 1 3 5 7 9 11 13 15 8 1 3 5 7 9 11 13 15 17 9 1 3 5 7 9 11 13 15 17 19 10 1 3 5 7 9 11 13 15 17 19 21 11 1 3 5 7 9 11 13 15 17 19 21 23 12 1 3 5 7 9 11 13 15 17 19 21 23 25 13 1 3 5 7 9 11 13 15 17 19 21 23 25 27 ...
result:
ok All testcases are passed!
Test #7:
score: 0
Accepted
time: 5ms
memory: 27104kb
input:
100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 100 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
0 1 1 1 2 3 1 3 1 2 5 1 3 1 4 1 2 7 1 3 1 4 1 5 1 2 9 1 3 1 4 1 5 1 6 1 2 11 1 3 1 4 1 5 1 6 1 7 1 2 13 1 3 1 4 1 5 1 6 1 7 1 8 1 2 15 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 2 17 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 2 19 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 2 21 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1...
result:
ok All testcases are passed!
Test #8:
score: 0
Accepted
time: 4ms
memory: 27156kb
input:
100 100 1 1 2 2 1 3 3 5 4 3 1 5 3 4 2 14 9 10 8 11 16 7 6 6 5 5 24 23 6 28 30 11 24 8 2 19 26 29 37 23 33 19 37 19 44 30 19 44 44 40 37 22 41 44 26 31 53 8 51 15 3 17 47 63 21 8 62 43 9 49 38 48 49 34 47 44 29 66 1 39 26 16 31 72 41 48 77 8 74 8 67 12 2 24 75 91 36 8 23 69 100 2 1 2 2 1 3 3 5 4 3 1 ...
output:
0 1 1 1 2 2 1 2 3 3 1 2 3 6 4 1 2 3 6 23 5 1 2 3 6 23 28 6 1 2 3 6 23 28 30 7 1 2 3 6 23 28 30 31 8 1 2 3 6 23 28 30 31 56 10 1 5 1 2 3 6 23 28 30 31 56 12 1 5 8 5 1 2 3 6 23 28 30 31 56 14 1 5 8 19 8 5 1 2 3 6 23 28 30 31 56 16 1 5 8 19 36 19 8 5 1 2 3 6 23 28 30 31 56 18 1 5 8 19 36 9...
result:
ok All testcases are passed!
Test #9:
score: 0
Accepted
time: 5ms
memory: 27196kb
input:
100 100 1 1 1 2 4 5 4 2 8 8 4 9 11 9 5 6 2 2 3 6 5 8 1 23 5 2 15 13 13 17 8 15 24 13 25 6 3 9 22 34 32 23 39 7 39 30 44 28 36 16 34 50 45 48 2 14 23 21 37 57 39 9 14 20 42 10 22 12 49 34 40 17 69 48 74 15 8 47 14 39 43 1 60 50 3 5 32 87 79 68 7 2 65 27 67 72 26 29 86 59 100 2 1 1 2 4 5 4 2 8 8 4 9 1...
output:
0 1 1 1 2 2 1 2 4 3 1 2 4 5 4 1 2 4 5 6 5 1 2 4 5 6 36 6 1 2 4 5 6 36 49 7 1 2 4 5 6 36 49 69 8 1 2 4 5 6 36 49 69 73 10 1 3 1 2 4 5 6 36 49 69 73 12 1 3 19 3 1 2 4 5 6 36 49 69 73 14 1 3 19 3 37 3 1 2 4 5 6 36 49 69 73 16 1 3 19 3 37 59 37 3 1 2 4 5 6 36 49 69 73 18 1 3 19 3 37 59 100 ...
result:
ok All testcases are passed!
Test #10:
score: 0
Accepted
time: 4ms
memory: 27100kb
input:
100 100 1 1 1 1 2 2 5 7 3 4 5 7 12 4 9 1 7 11 2 4 19 6 22 5 21 11 12 6 3 9 21 2 22 28 25 23 20 31 7 28 34 13 32 8 5 44 8 24 43 39 44 42 34 36 6 37 42 54 13 46 49 27 51 24 42 2 13 37 66 66 59 7 65 4 14 27 28 26 52 68 25 61 51 40 31 49 64 42 40 44 8 33 22 13 38 32 88 62 93 88 100 2 1 1 1 2 2 5 7 3 4 5...
output:
0 1 1 1 2 2 1 2 5 3 1 2 5 7 4 1 2 5 7 12 5 1 2 5 7 12 13 6 1 2 5 7 12 13 42 7 1 2 5 7 12 13 42 52 8 1 2 5 7 12 13 42 52 79 10 1 3 1 2 5 7 12 13 42 52 79 12 1 3 9 3 1 2 5 7 12 13 42 52 79 14 1 3 9 15 9 3 1 2 5 7 12 13 42 52 79 16 1 3 9 15 9 30 9 3 1 2 5 7 12 13 42 52 79 18 1 3 9 15 9 30 ...
result:
ok All testcases are passed!
Test #11:
score: 0
Accepted
time: 6ms
memory: 27192kb
input:
100 100 1 1 2 1 1 4 4 5 8 7 8 10 7 11 12 8 16 2 14 17 7 6 14 5 5 8 5 23 11 19 30 7 23 3 14 10 33 11 30 39 33 15 34 39 24 37 33 34 35 15 39 19 37 40 51 7 12 10 57 29 26 22 18 44 63 4 37 6 38 23 48 47 13 19 41 73 67 8 65 14 42 47 1 15 78 48 27 25 45 15 37 23 32 18 57 14 14 83 58 78 100 2 1 2 1 1 4 4 5...
output:
0 1 1 1 5 2 1 5 8 3 1 5 8 11 4 1 5 8 11 14 5 1 5 8 11 14 19 6 1 5 8 11 14 19 30 7 1 5 8 11 14 19 30 39 8 1 5 8 11 14 19 30 39 40 9 1 5 8 11 14 19 30 39 40 54 11 1 2 1 5 8 11 14 19 30 39 40 54 13 1 2 3 2 1 5 8 11 14 19 30 39 40 54 15 1 2 3 34 3 2 1 5 8 11 14 19 30 39 40 54 17 1 2 3 34 43...
result:
ok All testcases are passed!
Test #12:
score: 0
Accepted
time: 5ms
memory: 27116kb
input:
100 100 1 1 1 2 1 5 2 5 5 9 2 10 9 1 14 11 14 10 6 8 4 11 1 2 14 17 17 3 7 24 18 29 26 32 17 25 21 23 37 12 39 33 4 7 8 8 28 11 45 38 25 7 29 29 52 46 3 29 10 6 22 53 40 14 41 19 32 55 33 42 8 58 16 35 59 4 64 66 3 9 29 7 69 60 60 4 1 37 5 3 6 7 67 44 2 17 60 28 79 31 100 2 1 1 2 1 5 2 5 5 9 2 10 9 ...
output:
0 1 1 1 2 2 1 2 4 3 1 2 4 21 4 1 2 4 21 37 5 1 2 4 21 37 39 6 1 2 4 21 37 39 41 7 1 2 4 21 37 39 41 65 9 1 3 1 2 4 21 37 39 41 65 11 1 3 28 3 1 2 4 21 37 39 41 65 13 1 3 28 47 28 3 1 2 4 21 37 39 41 65 15 1 3 28 47 28 98 28 3 1 2 4 21 37 39 41 65 17 1 3 28 47 28 98 28 3 57 3 1 2 4 21 37 ...
result:
ok All testcases are passed!
Test #13:
score: 0
Accepted
time: 4ms
memory: 27100kb
input:
100 100 34 1 1 3 1 4 3 3 2 5 5 3 1 6 4 3 7 1 18 2 5 12 15 2 14 16 10 20 15 12 26 3 27 5 6 35 14 26 6 35 39 37 6 38 27 8 7 21 38 14 22 35 33 33 43 16 29 26 4 47 59 47 7 34 62 21 56 24 13 28 10 28 36 50 64 50 26 48 17 34 46 57 17 34 22 46 50 20 10 85 35 88 38 49 88 94 82 50 44 21 100 74 1 1 2 3 3 6 7 ...
output:
59 1 2 9 2 20 28 70 28 72 28 20 88 92 88 95 88 20 2 24 68 24 2 1 5 10 27 33 53 33 54 33 27 45 27 10 71 10 89 10 5 11 5 21 48 78 48 21 66 21 100 21 5 1 3 4 15 29 57 82 97 136 1 2 4 98 4 2 9 15 9 16 35 16 9 64 85 64 9 65 86 65 9 2 27 50 27 2 1 41 53 91 53 41 1 3 5 42 52 95 52 42 96 42 5 45 5 3 46 3 6...
result:
ok All testcases are passed!
Test #14:
score: 0
Accepted
time: 6ms
memory: 27096kb
input:
100 100 50 1 2 3 3 4 3 7 5 2 8 1 6 3 7 13 12 10 17 19 6 9 15 1 5 24 7 13 21 26 9 19 25 20 14 16 32 26 7 17 32 27 41 39 14 23 23 38 45 41 30 27 49 52 47 39 48 18 31 40 8 4 43 38 63 12 47 49 21 60 29 17 32 63 5 62 46 29 27 64 28 52 8 25 50 5 82 70 83 39 36 28 35 33 85 54 48 51 51 50 100 10 1 1 1 1 3 5...
output:
90 1 12 17 19 20 34 20 19 32 37 32 41 43 63 65 63 74 63 43 41 50 85 95 85 50 100 50 41 32 73 32 19 17 40 60 70 88 70 60 40 17 72 17 12 66 12 1 24 26 30 51 98 51 99 51 30 26 38 48 57 48 97 48 38 64 80 64 38 26 24 1 2 10 18 58 18 10 2 3 5 9 22 9 5 3 4 6 13 16 36 91 9 1 3 6 11 14 19 21 34 49 63 126 1...
result:
ok All testcases are passed!
Test #15:
score: 0
Accepted
time: 6ms
memory: 27112kb
input:
100 100 29 1 2 2 2 1 3 5 1 6 1 7 2 7 11 4 5 1 11 13 15 10 9 1 6 20 26 3 28 15 17 25 26 25 29 3 8 6 14 1 40 29 1 27 33 16 3 1 37 17 27 29 25 4 23 22 34 32 16 23 45 60 9 58 20 15 7 18 61 16 19 58 52 21 32 33 25 10 68 9 44 37 39 82 13 4 45 53 88 32 66 18 5 38 78 36 70 7 16 40 100 98 1 2 1 3 4 5 7 4 5 8...
output:
48 1 6 10 22 56 22 10 78 95 78 10 6 25 32 58 64 58 72 58 32 75 32 90 32 25 34 57 34 25 53 88 89 88 53 25 77 25 6 38 6 1 2 13 20 26 33 45 61 69 182 1 4 6 14 31 37 62 91 62 37 31 96 31 14 50 14 70 73 70 89 70 14 77 14 6 16 6 32 46 32 52 69 52 100 52 32 6 4 9 40 9 4 24 25 26 28 26 25 30 25 85 25 24 4 ...
result:
ok All testcases are passed!
Test #16:
score: 0
Accepted
time: 5ms
memory: 27160kb
input:
100 100 78 1 2 1 4 4 5 1 4 5 6 1 9 7 8 12 14 16 16 12 20 14 22 1 9 22 3 25 17 12 20 25 8 26 6 24 29 23 26 12 26 18 26 22 8 26 30 27 2 30 11 10 32 31 2 49 24 27 39 4 34 36 29 40 25 4 51 21 50 59 17 25 42 15 8 15 24 54 61 39 42 9 48 75 10 58 70 36 23 59 89 81 6 82 85 52 52 14 25 99 100 69 1 1 3 3 3 1 ...
output:
144 1 2 3 27 48 83 48 27 58 86 58 27 3 2 49 56 49 2 55 2 1 8 15 74 15 76 15 8 33 8 45 8 75 84 75 8 1 12 16 18 42 73 42 81 92 81 42 18 16 19 16 12 20 21 68 21 20 31 54 78 54 31 20 12 30 47 30 50 69 50 30 12 40 64 40 12 1 24 36 62 36 88 36 24 57 24 77 24 1 4 6 11 51 67 51 11 6 35 6 93 6 4 9 13 9 25 28...
result:
ok All testcases are passed!
Test #17:
score: 0
Accepted
time: 6ms
memory: 27144kb
input:
100 100 56 1 2 2 3 3 4 1 8 8 9 5 4 12 10 4 7 7 10 6 8 15 14 2 9 19 20 25 24 21 21 31 9 33 28 34 21 3 33 24 26 22 28 10 28 18 16 11 43 6 8 13 36 35 32 19 50 24 33 46 11 30 57 60 46 9 12 58 21 44 19 66 62 30 13 61 63 35 25 63 27 75 78 52 57 58 34 19 28 30 29 16 68 87 77 33 42 35 88 89 100 57 1 1 1 1 4...
output:
102 1 8 9 11 48 11 61 76 61 11 9 25 28 35 54 35 78 83 78 35 98 35 28 43 49 43 28 45 28 89 100 89 28 25 79 25 9 33 34 36 53 36 34 87 94 87 34 33 39 33 59 33 96 33 9 66 72 66 9 8 10 15 22 42 97 42 22 15 10 19 26 41 26 19 56 19 71 19 88 99 88 19 10 44 70 44 10 8 21 30 62 30 21 8 1 2 3 6 50 57 63 77 95 ...
result:
ok All testcases are passed!