QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791833 | #5515. Cat Exercise | _8_8_ | 0 | 62ms | 52072kb | C++17 | 1.7kb | 2024-11-28 21:17:27 | 2024-11-28 21:17:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int)2e5 + 12, MOD = 998244353;
const int B = 19;
int n, p[N],f[N], up[N][19], timer, tin[N], tout[N], dep[N];
ll dp[N];
vector<int> g[N], G[N];
int get(int v) {
if(f[v] == v) return v;
return f[v] = get(f[v]);
}
void dfs(int v, int pr = 1) {
tin[v] = ++timer;
up[v][0] = pr;
for(int i = 1; i < B; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to : G[v]) if(to != pr) {
dep[to] = dep[v] + 1;
dfs(to, v);
}
tout[v] = ++timer;
}
bool is(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
if(is(v, u)) return v;
if(is(u, v)) return u;
for(int i = B - 1; i >= 0; i--) {
if(!is(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
int dist(int x, int y) {
return dep[x] + dep[y] - dep[lca(x, y)] * 2;
}
void test() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> p[i];
f[i] = i;
}
for(int i = 1; i <= n -1; i++) {
int a, b;
cin >> a >> b;a = p[a];b = p[b];
g[b].push_back(a);
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1);
for(int i = 1; i <= n; i++) {
for(int to : g[i]) {
dp[i] = max(dp[i], dp[get(to)] + dist(get(to), to) + 1);
f[get(to)] = i;
}
}
cout << dp[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 13052kb
input:
10 9 4 3 2 1 10 7 5 6 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
output:
2
result:
wrong answer 1st lines differ - expected: '10', found: '2'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #32:
score: 0
Wrong Answer
time: 62ms
memory: 52072kb
input:
200000 200000 199998 199996 199994 199992 199990 199988 199986 199984 199982 199980 199978 199976 199974 199972 199970 199968 199966 199964 199962 199960 199958 199956 199954 199952 199950 199948 199946 199944 199942 199940 199938 199936 199934 199932 199930 199928 199926 199924 199922 199920 199918...
output:
0
result:
wrong answer 1st lines differ - expected: '19999900000', found: '0'
Subtask #6:
score: 0
Wrong Answer
Test #39:
score: 0
Wrong Answer
time: 3ms
memory: 13132kb
input:
200 49 181 133 160 142 76 134 189 25 167 10 54 59 158 186 53 58 145 95 9 27 30 116 77 140 173 131 21 151 128 190 51 19 112 40 161 136 93 46 52 45 84 18 42 63 43 73 188 147 153 124 127 177 32 75 150 96 175 34 183 106 13 80 196 141 198 67 26 92 192 191 172 90 83 164 180 107 143 121 146 125 174 139 104...
output:
9
result:
wrong answer 1st lines differ - expected: '256', found: '9'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%