QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#474245 | #8134. LCA Counting | toan | WA | 2ms | 12664kb | C++17 | 1.6kb | 2024-07-12 16:54:31 | 2024-07-12 16:54:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m, u, v, a[100005], subtree[100005], ans[100005];
multiset<int> mp[100005];
vector<int> adj[100005];
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;
}
for (int node: adj[u]) {
if(node == par || node == p) continue;
for (auto it: mp[node]) mp[p].insert(it);
mp[node].clear();
}
swap(mp[p], mp[u]);
if(mp[u].size() >= 2) {
int num = *(prev(mp[u].end())) + *(prev(prev(mp[u].end())));
mp[u].erase(prev(mp[u].end()));
mp[u].erase(prev(mp[u].end()));
mp[u].insert(num+1);
}
}
void solve() {
cin >> n >> m;
for (int i = 2; i <= n; i++) {
cin >> u; v = i;
adj[u].push_back(v);
adj[v].push_back(u);
}
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;
for (int j = 0; j <= num; j++) ans[sl+1+j] = ans[sl]+1+2*j;
sl += 1 + num;
}
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: 100
Accepted
time: 2ms
memory: 11684kb
input:
7 1 1 2 4 2 2
output:
1 3 5 6
result:
ok 4 number(s): "1 3 5 6"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 12664kb
input:
10 1 1 2 2 1 1 1 2 4
output:
1 3 5 7 8 9 10
result:
wrong answer 4th numbers differ - expected: '6', found: '7'