QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#310150 | #8134. LCA Counting | ucup-team992# | TL | 1ms | 3804kb | C++17 | 2.0kb | 2024-01-21 04:10:59 | 2024-01-21 04:10:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define F first
#define S second
typedef int uci;
#define int long long
void solve(){
int n;
cin >> n;
vector<set<int>> adj(n);
vector<int> par(n);
for(int i{1}; i < n; ++i){
cin >> par[i];
par[i]--;
adj[par[i]].insert(i);
}
int tot = n;
for(int i{}; i < n; ++i){
if(adj[i].size() == 1)
tot--;
}
vector<bool> leaf(n);
int lc{};
for(int i{1}; i < n; ++i){
if(adj[i].size() == 0){
leaf[i] = true;
lc++;
while(par[i] > 0 && adj[par[i]].size() == 1){
int p = par[i];
adj[p].erase(adj[p].begin());
par[i] = par[p];
adj[par[i]].insert(i);
adj[par[i]].erase(p);
}
}
}
vector<int> real(n), fake(n);
set<ar<int, 2>> nodes;
for(int i{}; i < n; ++i){
for(int j : adj[i]){
if(leaf[j]){
real[i]++;
}else
fake[i]++;
}
if(real[i] > 0)
nodes.insert({-real[i]-fake[i], i});
}
vector<int> out(lc, tot);
for(int i{1}; i < lc; ++i){
auto a = *nodes.begin();
nodes.erase(nodes.begin());
real[a[1]]--;
a[0] *= -1;
if(a[0] >= 3){
tot--;
if(real[a[1]] > 0){
nodes.insert({-real[a[1]]-fake[a[1]], a[1]});
}
}else{
tot -= 2;
while(par[a[1]] > 0 && adj[par[a[1]]].size() == 1){
int p = par[a[1]];
par[a[1]] = par[p];
}
nodes.erase({-real[par[a[1]]]-fake[par[a[1]]], par[a[1]]});
real[par[a[1]]]++;
nodes.erase({-real[par[a[1]]]-fake[par[a[1]]], par[a[1]]});
}
out[i] = tot;
}
reverse(out.begin(), out.end());
for(int i : out)
cout << i << ' ';
cout << '\n';
}
uci main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3480kb
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: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 1 1 2 2 1 1 1 2 4
output:
1 3 5 6 7 8 9
result:
ok 7 numbers
Test #3:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
10 1 2 2 4 5 6 1 2 4
output:
1 3 5 7 8
result:
ok 5 number(s): "1 3 5 7 8"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
10 1 2 3 3 3 5 6 4 9
output:
1 3 4
result:
ok 3 number(s): "1 3 4"
Test #5:
score: -100
Time Limit Exceeded
input:
1000 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 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...