#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
const ll N=2*1e5+50,lg=20;
//map<pair<int,int>,int>mapa;
vector<ll>E[N];
set<ll>st[N];
ll dp[N];
ll idx[N],parent[N][lg+10],depth[N];
bool was[N];
void DFScalc(ll u,ll j,ll par,ll d)
{
depth[u]=d;
parent[u][0]=par;
for(auto i:E[u])
{
if(i!=par)
{
DFScalc(i,j,u,d+1);
}
}
parent[u][j]=parent[parent[u][j-1]][j-1];
}
ll dist(ll u,ll v)
{
ll res=1;
if(depth[u]<depth[v]) swap(u,v);
ll e=1<<(lg+1);
for(ll i=lg;i>=0;i--)
{
e/=2;
//if(parent[u][i]==0) continue;
if(depth[parent[u][i]]>=depth[v])
{
u=parent[u][i];
res+=e;
}
}
e=1<<(lg+1);
if(u==v) return res;
for(ll i=lg;i>=0;i--)
{
e/=2;
if(parent[u][i]==0 || parent[v][i]==0) continue;
if(parent[u][i]!=parent[v][i])
{
res+=e*2;
u=parent[u][i];
v=parent[v][i];
}
}
res+=2;
return res;
}
/*pair<int,int> DFS(int u,int par,int depth)
{
pair<int,int> x=make_pair(u,depth);
for(auto i:E[u])
{
if(was[i] && i!=par)
{
pair<int,int>y=DFS(i,u,depth+1);
if(x.fi<y.fi) x=y;
}
}
return x;
}*/
int main()
{
ll n;scanf("%lld",&n);
ll a[n+1];//L[n+1],R[n+1];
//vector<int>mono;
for(ll i=1;i<=n;i++)
{
//L[i]=0,R[i]=n+1;
idx[i]=i;
scanf("%lld",&a[i]);
/*while(!mono.empty() && a[mono.back()]<a[i])
{
R[mono.back()]=i;
mono.pop_back();
}
if(!mono.empty()) L[i]=mono.back();
mono.push_back(i);*/
}
/*pair<int,int>par[n+1];
for(int i=1;i<=n;i++)
{
mapa[{L[i],R[i]}]=i;
par[i]=make_pair(R[i]-L[i],i);
}
sort(par+1,par+n+1);
for(int i=1;i<=n;i++)
{
int j=par[i].se,maks=0,l=mapa[{L[j],j}],r=mapa[{j,R[j]}];
if(j-L[j]>1)dp[j]=max(dp[l]+j-l,dp[j]);
if(R[j]-j>1)dp[j]=max(dp[r]+r-j,dp[j]);
}*/
//bool bul=true;
for(ll i=1;i<n;i++)
{
ll u,v;
scanf("%lld%lld",&u,&v);
E[a[u]].push_back(a[v]);
E[a[v]].push_back(a[u]);
//if(!(u==i && v==i+1))bul=false;
}
for(ll i=1;i<=lg;i++)
{
DFScalc(n,i,0,0);
}
/*for(int i=1;i<=n;i++)
{
for(int j=0;j<=3;j++)
{
printf("%lld ",parent[i][j]);
}
printf("\n");
}*/
//printf("%i\n",dist(3,5));
//if(bul) {printf("%lld\n",dp[mapa[{0,n+1}]]);return 0;}
for(ll i=1;i<=n;i++)
{
dp[i]=0;
ll ind=i,sz=0;
for(auto u:E[i])
{
if(was[u])
{
if(st[idx[u]].size()>sz)
{
ind=u;
sz=st[idx[u]].size();
}
ll maks=-*st[idx[u]].begin();
ll x=dist(u,maks);
dp[i]=max(dp[i],dp[maks]+x);
//printf("%i %lld %i: %lld\n",u,maks,i,x);
//pair<int,int> x=DFS(u,i,1);
//dp[i]=max(dp[x.fi]+x.se,dp[i]);
}
}
//printf("%lld: %lld %lld\n",i,ind,sz);
for(auto u:E[i])
{
if(was[u] && u!=ind)
{
int temp=idx[u];
while(!st[temp].empty())
{
ll x=*st[temp].begin();
st[idx[ind]].insert(x);
idx[-x]=idx[ind];
st[temp].erase(x);
}
}
}
was[i]=true;
st[idx[ind]].insert(-i);
idx[i]=idx[ind];
//printf("%i: %lld\n",i,dp[i]);
}
printf("%lld\n",dp[n]);
/*for(int i=1;i<=n;i++)
{
printf("%lld ",dp[i]);
}*/
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:132:25: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
132 | if(st[idx[u]].size()>sz)
| ~~~~~~~~~~~~~~~~~^~~
Main.cpp:72:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | ll n;scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
Main.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%lld",&a[i]);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf("%lld%lld",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
3 ms |
21080 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
3 ms |
21080 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
5 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21336 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
3 ms |
21080 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
5 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21336 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
7 ms |
23896 KB |
Output is correct |
19 |
Correct |
8 ms |
23896 KB |
Output is correct |
20 |
Correct |
8 ms |
23896 KB |
Output is correct |
21 |
Correct |
11 ms |
23896 KB |
Output is correct |
22 |
Correct |
11 ms |
23640 KB |
Output is correct |
23 |
Correct |
11 ms |
23616 KB |
Output is correct |
24 |
Correct |
10 ms |
24100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
3 ms |
21080 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
5 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21336 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
7 ms |
23896 KB |
Output is correct |
19 |
Correct |
8 ms |
23896 KB |
Output is correct |
20 |
Correct |
8 ms |
23896 KB |
Output is correct |
21 |
Correct |
11 ms |
23896 KB |
Output is correct |
22 |
Correct |
11 ms |
23640 KB |
Output is correct |
23 |
Correct |
11 ms |
23616 KB |
Output is correct |
24 |
Correct |
10 ms |
24100 KB |
Output is correct |
25 |
Correct |
4 ms |
21080 KB |
Output is correct |
26 |
Correct |
8 ms |
23900 KB |
Output is correct |
27 |
Correct |
8 ms |
23776 KB |
Output is correct |
28 |
Correct |
8 ms |
23640 KB |
Output is correct |
29 |
Correct |
8 ms |
23640 KB |
Output is correct |
30 |
Correct |
13 ms |
23644 KB |
Output is correct |
31 |
Correct |
10 ms |
23540 KB |
Output is correct |
32 |
Correct |
11 ms |
23384 KB |
Output is correct |
33 |
Correct |
11 ms |
23384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
3 ms |
21080 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
5 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21336 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
7 ms |
23896 KB |
Output is correct |
19 |
Correct |
8 ms |
23896 KB |
Output is correct |
20 |
Correct |
8 ms |
23896 KB |
Output is correct |
21 |
Correct |
11 ms |
23896 KB |
Output is correct |
22 |
Correct |
11 ms |
23640 KB |
Output is correct |
23 |
Correct |
11 ms |
23616 KB |
Output is correct |
24 |
Correct |
10 ms |
24100 KB |
Output is correct |
25 |
Correct |
309 ms |
101100 KB |
Output is correct |
26 |
Correct |
377 ms |
101508 KB |
Output is correct |
27 |
Correct |
412 ms |
101200 KB |
Output is correct |
28 |
Correct |
881 ms |
97540 KB |
Output is correct |
29 |
Correct |
836 ms |
99532 KB |
Output is correct |
30 |
Correct |
824 ms |
98992 KB |
Output is correct |
31 |
Correct |
819 ms |
99736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21336 KB |
Output is correct |
2 |
Correct |
4 ms |
21080 KB |
Output is correct |
3 |
Correct |
588 ms |
85044 KB |
Output is correct |
4 |
Correct |
620 ms |
89056 KB |
Output is correct |
5 |
Correct |
557 ms |
88804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
3 ms |
21080 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
5 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21336 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
7 ms |
23896 KB |
Output is correct |
19 |
Correct |
8 ms |
23896 KB |
Output is correct |
20 |
Correct |
8 ms |
23896 KB |
Output is correct |
21 |
Correct |
11 ms |
23896 KB |
Output is correct |
22 |
Correct |
11 ms |
23640 KB |
Output is correct |
23 |
Correct |
11 ms |
23616 KB |
Output is correct |
24 |
Correct |
10 ms |
24100 KB |
Output is correct |
25 |
Correct |
4 ms |
21080 KB |
Output is correct |
26 |
Correct |
8 ms |
23900 KB |
Output is correct |
27 |
Correct |
8 ms |
23776 KB |
Output is correct |
28 |
Correct |
8 ms |
23640 KB |
Output is correct |
29 |
Correct |
8 ms |
23640 KB |
Output is correct |
30 |
Correct |
13 ms |
23644 KB |
Output is correct |
31 |
Correct |
10 ms |
23540 KB |
Output is correct |
32 |
Correct |
11 ms |
23384 KB |
Output is correct |
33 |
Correct |
11 ms |
23384 KB |
Output is correct |
34 |
Correct |
309 ms |
101100 KB |
Output is correct |
35 |
Correct |
377 ms |
101508 KB |
Output is correct |
36 |
Correct |
412 ms |
101200 KB |
Output is correct |
37 |
Correct |
881 ms |
97540 KB |
Output is correct |
38 |
Correct |
836 ms |
99532 KB |
Output is correct |
39 |
Correct |
824 ms |
98992 KB |
Output is correct |
40 |
Correct |
819 ms |
99736 KB |
Output is correct |
41 |
Correct |
3 ms |
21336 KB |
Output is correct |
42 |
Correct |
4 ms |
21080 KB |
Output is correct |
43 |
Correct |
588 ms |
85044 KB |
Output is correct |
44 |
Correct |
620 ms |
89056 KB |
Output is correct |
45 |
Correct |
557 ms |
88804 KB |
Output is correct |
46 |
Correct |
464 ms |
98728 KB |
Output is correct |
47 |
Correct |
445 ms |
98736 KB |
Output is correct |
48 |
Correct |
456 ms |
100300 KB |
Output is correct |
49 |
Correct |
453 ms |
98732 KB |
Output is correct |
50 |
Correct |
916 ms |
88348 KB |
Output is correct |
51 |
Correct |
907 ms |
88400 KB |
Output is correct |
52 |
Correct |
856 ms |
88384 KB |
Output is correct |
53 |
Correct |
927 ms |
88352 KB |
Output is correct |