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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(a,b) for(int a = 0; a < (b); ++a)
#define all(t) t.begin(), t.end()
#define pb push_back
const int N = 5e5+5, INF = 2e9+54321;
const ll INF_L = (ll)2e18+54321;
int n,wie,mx,odl,nrw;
int A[N];
vector<int> G[N];
bool vis[N];
void DFS2(int v, int par, int di)
{
mx = max(mx,A[v]);
if(mx == A[v]) odl = di, nrw = v;
for(auto x : G[v])
{
if(x == par or vis[x]) continue;
DFS2(x,v,di+1);
}
}
ll DFS(int v)
{
//cout << "VV: " << v << '\n';
vis[v] = 1;
ll val = 0;
for(auto x : G[v])
{
mx = -1, odl = 0;
if(!vis[x])
{
DFS2(x,v,1);
val = max(val,odl+DFS(nrw));
}
}
//cout << "V: " << v << " VAL: " << val << '\n';
return val;
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; ++i) cin >> A[i];
rep(i,n-1)
{
int a,b;
cin >> a >> b;
G[a].pb(b), G[b].pb(a);
}
for(int i = 1; i <= n; ++i) if(A[i] == n) wie = i;
cout << DFS(wie) << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
//cin >> T;
while(T--) solve();
return 0;
}
# | 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... |