QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#474353 | #8134. LCA Counting | toan | WA | 4ms | 21004kb | C++17 | 1.9kb | 2024-07-12 17:35:31 | 2024-07-12 17:35:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m, u, v, a[200005], subtree[200005], ans[200005];
multiset<int> mp[200005];
vector<int> adj[200005];
void build(int u, int par) {
subtree[u] = 1;
for (int node: adj[u]) {
if(node == par) continue;
build(node, u);
subtree[u] += subtree[node];
}
}
void dfs(int u, int par) {
int mx=0, p=-1;
for (int node: adj[u]) {
if(node == par) continue;
dfs(node, u);
if(mx < subtree[node]) mx = subtree[node], p = node;
}
if(p==-1) {
mp[u].insert(0);
return;
}
int MAX = 0;
vector<int> v;
multiset<int> Q;
Q.insert(*prev(mp[p].end()));
for (int node: adj[u]) {
if(node == par || node == p) continue;
MAX = 0;
Q.insert(*prev(mp[node].end()));
if (Q.size() > 2) Q.erase(Q.begin());
for (auto it: mp[node]) {
mp[p].insert(it);
MAX = max(MAX, it);
}
mp[node].clear();
}
swap(mp[p], mp[u]);
if(Q.size() >= 2) {
mp[u].insert(*Q.begin() + *next(Q.begin()) + 1);
mp[u].erase(mp[u].lower_bound(*Q.begin()));
mp[u].erase(mp[u].lower_bound(*next(Q.begin())));
}
}
void solve() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> u; v = i;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout << '\n';
int cnt = 0;
for (int i = 1; i <= n; i++) cnt += (adj[i].size()==1 && i != 1 ? 1 : 0);
build(1, -1);
dfs(1,-1);
int sl = 0;
ans[1] = 1;
for (auto i = mp[1].rbegin(); i != mp[1].rend(); i++) {
int num = *i;
cout << num << ' ';
for (int j = 0; j <= num; j++) ans[sl+1+j] = ans[sl]+1+2*j;
sl += 1 + num;
}
cout << '\n';
for (int i = 1; i <= cnt; i++) cout << ans[i] << ' ';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen("input.inp","r")){
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}//
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 21004kb
input:
7 1 1 2 4 2 2
output:
2 0 1 3 5 6
result:
wrong answer 1st numbers differ - expected: '1', found: '2'