QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#310158 | #8134. LCA Counting | ucup-team992# | WA | 1ms | 3904kb | C++17 | 2.9kb | 2024-01-21 04:39:13 | 2024-01-21 04:39:14 |
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;
//cout << "removing " << a[1] << '\n';
if(a[0] >= 3){
tot--;
if(real[a[1]] > 0){
nodes.insert({-real[a[1]]-fake[a[1]], a[1]});
}
}else{
tot -= 2;
int p = a[1];
auto it = adj[a[1]].begin();
it++;
int b = *adj[a[1]].begin();
int other =*it;
if(!leaf[b]){
swap(b, other);
}
adj[a[1]].erase(b);
while(par[a[1]] > 0 && adj[par[a[1]]].size() == 1){
p = par[a[1]];
par[a[1]] = par[p];
}
adj[par[a[1]]].erase(p);
adj[par[a[1]]].insert(other);
par[other] = par[a[1]];
if(real[a[1]] > 0){
nodes.erase({-real[par[a[1]]]-fake[par[a[1]]], par[a[1]]});
real[par[a[1]]]++;
fake[par[a[1]]]--;
nodes.insert({-real[par[a[1]]]-fake[par[a[1]]], par[a[1]]});
}
}
/*
for(int i{}; i < n; ++i){
cout << "at vertex: " << i << '\n';
for(int j : adj[i]){
cout << j << ' ';
}
cout << '\n';
}
cout << "=========\n";
for(int i{}; i < n; ++i)
cout << par[i] << ' ';
cout << '\n';
cout << "=========\n";
*/
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: 3744kb
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: 3508kb
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: 0ms
memory: 3504kb
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: 0ms
memory: 3464kb
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: 0
Accepted
time: 1ms
memory: 3904kb
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...
output:
1 3 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35 37 38 40 41 43 44 46 47 49 50 52 53 55 56 58 59 61 62 64 65 67 68 70 71 73 74 76 77 79 80 82 83 85 86 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 12...
result:
ok 962 numbers
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 3636kb
input:
1000 1 1 1 1 1 1 1 2 2 1 2 3 1 1 1 2 2 1 1 1 2 1 2 3 8 2 2 2 8 1 6 3 1 1 1 4 2 14 8 2 1 1 1 3 1 4 1 1 3 11 1 1 2 2 3 2 5 5 6 5 3 3 10 5 3 1 9 14 3 19 4 15 4 3 3 10 3 10 5 1 2 12 7 13 3 9 6 19 9 6 4 3 5 28 10 16 8 12 39 1 11 12 2 2 10 15 5 28 10 4 4 5 5 18 15 15 11 5 25 7 2 10 1 24 8 15 14 11 38 9 9 ...
output:
-1 1 3 5 7 8 10 12 14 15 17 19 20 22 24 26 27 29 30 32 34 35 37 38 40 42 43 45 46 48 50 52 53 55 57 59 60 62 64 66 67 69 70 72 73 75 77 79 80 82 84 86 87 89 90 92 93 95 96 98 100 102 103 105 106 108 109 111 113 115 117 119 120 122 123 125 127 129 130 132 134 136 137 139 140 142 143 145 146 148 149 1...
result:
wrong answer 1st numbers differ - expected: '1', found: '-1'