QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#47401 | #2228. Bread Pit | tovarisch | WA | 11ms | 17724kb | C++ | 1.8kb | 2022-09-09 03:39:21 | 2022-09-09 03:39:24 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;
/* *
*
* Too many mind, no mind.
*
* */
const int maxn = 3e5 + 10;
int par[maxn], children[maxn], idx[maxn];
vector <int> tree[maxn], leaf[maxn], nodes;
void dfs(int u, int prev = 0) {
for (int& v: tree[u]) {
if (v == prev) continue;
dfs(v, u);
}
if (u && tree[u].size() <= 1) {
nodes.push_back(prev ? prev : u);
leaf[prev ? prev : u].push_back(u);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, q; cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> par[i];
children[par[i]]++;
}
int dummy = n + 5;
for (int i = 0; i < n; i++) {
if (children[par[i]] > 1) continue;
while (children[par[i]] == 1 && par[i] != 0) {
int u = par[i];
par[i] = par[par[i]];
par[u] = dummy;
}
}
for (int i = 0; i < n; i++) {
tree[i].push_back(par[i]);
tree[par[i]].push_back(i);
}
dfs(0);
nodes.resize(unique(nodes.begin(), nodes.end()) - nodes.begin());
bool surface = false;
for (int i = 0; i < q; i++) {
if (nodes.empty()) {
cout << 0 << '\n';
continue;
}
int u = nodes[i % nodes.size()];
cout << (surface ? 0 : leaf[u][idx[u]]) << '\n';
//if (leaf[u][idx[u]] == 0) surface = true;
idx[u] = (idx[u] + 1) % leaf[u].size();
//if (children[u] == 1) leaf[u][0] = par[leaf[u][0]];
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 17604kb
input:
1 1
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 6ms
memory: 17724kb
input:
2 2 0
output:
1 1
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 2ms
memory: 17612kb
input:
3 3 0 1
output:
2 2 2
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 5ms
memory: 17668kb
input:
4 4 0 1 1
output:
2 3 2 3
result:
ok 4 lines
Test #5:
score: -100
Wrong Answer
time: 11ms
memory: 17652kb
input:
5 5 0 0 0 1
output:
2 3 4 2 3
result:
wrong answer 1st lines differ - expected: '4', found: '2'