이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
int cur;
vector<int> g[maxn];
int ans=0;
int dp[maxn];
vector<pii> v;
int arr[maxn];
int pp;
int mx=0;
void dfs(int u,int p,int d){
if(arr[u]>cur) return;
if(pp==-1 or arr[u]>arr[pp]) pp=u,mx=dp[u]+d;
for(int x:g[u]){
if(x==p) continue;
dfs(x,u,d+1);
}
}
int main(){
int n;
cin>>n;
REP(i,n){
int x;
cin>>x;
--x;
v.pb({x,i});
arr[i]=x;
}
REP(i,n-1){
int a,b;
cin>>a>>b;
--a,--b;
g[a].pb(b),g[b].pb(a);
}
sort(ALL(v));
for(auto x:v){
cur=x.f;
for(int a:g[x.s]){
pp=-1;
dfs(a,x.s,1);
if(pp!=-1) MXTO(dp[x.s],mx);
}
//cout<<dp[x.s]<<' ';
}
cout<<dp[v.back().s];
}
# | 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... |