#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define bit(x,y) ((x >> y) & 1LL)
const ll mod = 1e9 + 7;
const ll N = 1e6 + 5;
const ll inf = LLONG_MAX/4;
ll p[N],h[N],ans[N],tmp[N];
vector<ll> a[N];
ll fp(ll x)
{
if(p[x] == -1) return x;
else
{
p[x] = fp(p[x]);
return p[x];
}
}
void hop(ll x, ll y)
{
ll px = x;
ll py = fp(y);
ans[px] = max(ans[px],ans[py] + abs(h[px] - h[py]));
p[py] = px;
}
void build(ll x, ll px)
{
for(auto y : a[x])
{
if(y != px)
{
h[y] = h[x] + 1;
build(y,x);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
ll i,j;
for(i = 1;i <= n;i++)
{
cin >> tmp[i];
}
for(i = 1;i < n;i++)
{
ll u,v;
cin >> u>> v;
a[tmp[u]].push_back(tmp[v]);
a[tmp[v]].push_back(tmp[u]);
}
build(n,0);
for(i = 1;i <= n;i++)
{
p[i] = -1;
ans[i] = 0;
}
for(i = 1;i <= n;i++)
{
for(auto x : a[i])
{
if(x < i) hop(i,x);
}
}
cout << ans[n];
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:43:10: warning: unused variable 'j' [-Wunused-variable]
43 | ll i,j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23980 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23980 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
12 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23948 KB |
Output is correct |
13 |
Correct |
11 ms |
23768 KB |
Output is correct |
14 |
Correct |
11 ms |
23928 KB |
Output is correct |
15 |
Correct |
10 ms |
23896 KB |
Output is correct |
16 |
Correct |
12 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23980 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
12 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23948 KB |
Output is correct |
13 |
Correct |
11 ms |
23768 KB |
Output is correct |
14 |
Correct |
11 ms |
23928 KB |
Output is correct |
15 |
Correct |
10 ms |
23896 KB |
Output is correct |
16 |
Correct |
12 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
24412 KB |
Output is correct |
19 |
Correct |
13 ms |
24412 KB |
Output is correct |
20 |
Correct |
12 ms |
24408 KB |
Output is correct |
21 |
Correct |
12 ms |
24412 KB |
Output is correct |
22 |
Correct |
11 ms |
24412 KB |
Output is correct |
23 |
Correct |
13 ms |
24664 KB |
Output is correct |
24 |
Correct |
13 ms |
24220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23980 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
12 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23948 KB |
Output is correct |
13 |
Correct |
11 ms |
23768 KB |
Output is correct |
14 |
Correct |
11 ms |
23928 KB |
Output is correct |
15 |
Correct |
10 ms |
23896 KB |
Output is correct |
16 |
Correct |
12 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
24412 KB |
Output is correct |
19 |
Correct |
13 ms |
24412 KB |
Output is correct |
20 |
Correct |
12 ms |
24408 KB |
Output is correct |
21 |
Correct |
12 ms |
24412 KB |
Output is correct |
22 |
Correct |
11 ms |
24412 KB |
Output is correct |
23 |
Correct |
13 ms |
24664 KB |
Output is correct |
24 |
Correct |
13 ms |
24220 KB |
Output is correct |
25 |
Correct |
10 ms |
23900 KB |
Output is correct |
26 |
Correct |
13 ms |
24408 KB |
Output is correct |
27 |
Correct |
13 ms |
24664 KB |
Output is correct |
28 |
Correct |
12 ms |
24272 KB |
Output is correct |
29 |
Correct |
11 ms |
24328 KB |
Output is correct |
30 |
Incorrect |
12 ms |
24156 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23980 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
12 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23948 KB |
Output is correct |
13 |
Correct |
11 ms |
23768 KB |
Output is correct |
14 |
Correct |
11 ms |
23928 KB |
Output is correct |
15 |
Correct |
10 ms |
23896 KB |
Output is correct |
16 |
Correct |
12 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
24412 KB |
Output is correct |
19 |
Correct |
13 ms |
24412 KB |
Output is correct |
20 |
Correct |
12 ms |
24408 KB |
Output is correct |
21 |
Correct |
12 ms |
24412 KB |
Output is correct |
22 |
Correct |
11 ms |
24412 KB |
Output is correct |
23 |
Correct |
13 ms |
24664 KB |
Output is correct |
24 |
Correct |
13 ms |
24220 KB |
Output is correct |
25 |
Correct |
71 ms |
45652 KB |
Output is correct |
26 |
Correct |
83 ms |
45728 KB |
Output is correct |
27 |
Correct |
60 ms |
45652 KB |
Output is correct |
28 |
Correct |
74 ms |
44372 KB |
Output is correct |
29 |
Correct |
87 ms |
45140 KB |
Output is correct |
30 |
Correct |
89 ms |
44860 KB |
Output is correct |
31 |
Correct |
79 ms |
45136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23900 KB |
Output is correct |
2 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23980 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
12 ms |
23900 KB |
Output is correct |
12 |
Correct |
11 ms |
23948 KB |
Output is correct |
13 |
Correct |
11 ms |
23768 KB |
Output is correct |
14 |
Correct |
11 ms |
23928 KB |
Output is correct |
15 |
Correct |
10 ms |
23896 KB |
Output is correct |
16 |
Correct |
12 ms |
23900 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
24412 KB |
Output is correct |
19 |
Correct |
13 ms |
24412 KB |
Output is correct |
20 |
Correct |
12 ms |
24408 KB |
Output is correct |
21 |
Correct |
12 ms |
24412 KB |
Output is correct |
22 |
Correct |
11 ms |
24412 KB |
Output is correct |
23 |
Correct |
13 ms |
24664 KB |
Output is correct |
24 |
Correct |
13 ms |
24220 KB |
Output is correct |
25 |
Correct |
10 ms |
23900 KB |
Output is correct |
26 |
Correct |
13 ms |
24408 KB |
Output is correct |
27 |
Correct |
13 ms |
24664 KB |
Output is correct |
28 |
Correct |
12 ms |
24272 KB |
Output is correct |
29 |
Correct |
11 ms |
24328 KB |
Output is correct |
30 |
Incorrect |
12 ms |
24156 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |