QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#357675 | #8134. LCA Counting | kevinyang# | WA | 15ms | 50256kb | C++17 | 1.7kb | 2024-03-19 07:48:05 | 2024-03-19 07:48:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mxn = 200005;
vector<vector<int>>adj(mxn); int ln = 20;
vector<vector<int>>dp(mxn,vector<int>(ln));
vector<int>lvl(mxn);
vector<int>loc(mxn);
int timer = 0;
void dfs(int u, int p){
timer++;
loc[u] = timer;
lvl[u] = lvl[p]+1;
dp[u][0] = p;
for(int i = 1; i<ln; i++){
dp[u][i] = dp[dp[u][i-1]][i-1];
}
for(int nxt: adj[u]){
if(nxt==p)continue;
dfs(nxt,u);
}
}
int lca(int x, int y){
if(lvl[x]<lvl[y])swap(x,y);
int dif = lvl[x]-lvl[y];
for(int i = 0; i<ln; i++){
if((1<<i)&dif){
x = dp[x][i];
}
}
if(x==y)return x;
for(int i = ln-1; i>=0; i--){
if(dp[x][i]!=dp[y][i]){
x = dp[x][i]; y = dp[y][i];
}
}
return dp[x][0];
}
int dist(int x, int y){
int u = lca(x,y);
return lvl[x]+lvl[y]-2*lvl[u];
}
bool comp(int x, int y){
return loc[x] < loc[y];
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
for(int i = 2; i<=n; i++){
int x;
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
dfs(1,0);
vector<int>vec;
for(int i = 2; i<=n; i++){
if(adj[i].size()==1){
vec.push_back(i);
}
}
sort(vec.begin(),vec.end(),comp);
set<int>s;
for(int i = 1; i<vec.size(); i++){
s.insert(lca(vec[i-1],vec[i]));
}
int ans = 0;
for(int i = 0; i<vec.size(); i++){
ans++;
cout << ans;
if(i+1 < vec.size())cout << ' ';
if(i<s.size())ans++;
}
cout << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 15ms
memory: 50004kb
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: 9ms
memory: 49720kb
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: 11ms
memory: 50256kb
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: 15ms
memory: 50220kb
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
Wrong Answer
time: 3ms
memory: 50192kb
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 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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:
wrong answer 5th numbers differ - expected: '8', found: '9'