This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
Main.cpp: In function 'int main()':
Main.cpp:43:10: warning: unused variable 'j' [-Wunused-variable]
43 | ll i,j;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |