#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define eb emplace_back
#define MOD 1000000007
#define nl '\n'
#define INF 1000000007
#define LLONG_MAX 9223372036854775807
#define pii pair<int,int>
#define tpl tuple<int,int,int,int>
//#pragma GCC optimize("O3")
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
/*
*
*
----------------DEMONSTRATION-------------------
---------------------END------------------------
*/
#define NMAX 200001
int n;
vector<int>v;
vector<vector<int>>G(NMAX);
struct LCA{
int depth[NMAX];
int tata[NMAX][20];
void dfs(int node,int parent,int dep)
{
depth[node]=dep;
tata[node][0]=parent;
for(auto x:G[node])
{
if(x!=parent)
{
dfs(x,node,dep+1);
}
}
}
void init()
{
dfs(1,0,0);
for(int d=0;d<19;++d)
{
for(int i=1;i<=n;++i)
{
tata[i][d+1]=tata[tata[i][d]][d];
}
}
}
int lca(int a,int b)
{
if(depth[a]>depth[b])
swap(a,b);
for(int d=19;d>=0;--d)
{
if(depth[a]<=depth[b]-(1<<d))
{
b=tata[b][d];
}
}
if(a==b)
return a;
for(int d=19;d>=0;--d)
{
if(tata[a][d]!=tata[b][d])
{
a=tata[a][d];
b=tata[b][d];
}
}
return tata[a][0];
}
int dist(int a,int b)
{
int c=lca(a,b);
return depth[a]+depth[b]-2*depth[c];
}
};
LCA lca;
struct UnionFind{
int tata[NMAX];
void init()
{
for(int i=1;i<=n;++i)
tata[i]=i;
}
int root(int a)
{
if(a==tata[a])
return a;
else
return tata[a]=root(tata[a]);
}
void unite(int a,int b)
{
a=root(a);
b=root(b);
if(a!=b)
{
if(v[a]>v[b])
swap(a,b);
tata[a]=b;
}
}
};
UnionFind kruskal;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
v.resize(n+1);
int root;
map<int,int>mp;
for(int i=1;i<=n;++i)
{
cin>>v[i];
mp[v[i]]=i;
}
for(int i=1;i<n;++i)
{
int x,y;
cin>>x>>y;
G[x].pb(y);
G[y].pb(x);
}
lca.init();
kruskal.init();
vector<int>dp(n+1,0);
for(int i=1;i<=n;++i)
{
int x=mp[i];
int ans=0;
for(auto z:G[x])
{
if(v[z]<v[x])
{
int v=kruskal.root(z);
ans=max(ans,dp[v]+lca.dist(v,x));
kruskal.unite(x,z);
}
}
dp[x]=ans;
}
root=mp[n];
cout<<dp[root];
return 0;
}
Compilation message
Main.cpp:9: warning: "LLONG_MAX" redefined
9 | #define LLONG_MAX 9223372036854775807
|
In file included from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:195,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:34,
from /usr/include/c++/10/climits:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:39,
from Main.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9308 KB |
Output is correct |
12 |
Correct |
2 ms |
9308 KB |
Output is correct |
13 |
Correct |
1 ms |
9308 KB |
Output is correct |
14 |
Correct |
1 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
9308 KB |
Output is correct |
16 |
Correct |
2 ms |
9308 KB |
Output is correct |
17 |
Correct |
2 ms |
9308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9308 KB |
Output is correct |
12 |
Correct |
2 ms |
9308 KB |
Output is correct |
13 |
Correct |
1 ms |
9308 KB |
Output is correct |
14 |
Correct |
1 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
9308 KB |
Output is correct |
16 |
Correct |
2 ms |
9308 KB |
Output is correct |
17 |
Correct |
2 ms |
9308 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9820 KB |
Output is correct |
23 |
Correct |
5 ms |
9820 KB |
Output is correct |
24 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9308 KB |
Output is correct |
12 |
Correct |
2 ms |
9308 KB |
Output is correct |
13 |
Correct |
1 ms |
9308 KB |
Output is correct |
14 |
Correct |
1 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
9308 KB |
Output is correct |
16 |
Correct |
2 ms |
9308 KB |
Output is correct |
17 |
Correct |
2 ms |
9308 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9820 KB |
Output is correct |
23 |
Correct |
5 ms |
9820 KB |
Output is correct |
24 |
Correct |
5 ms |
9820 KB |
Output is correct |
25 |
Correct |
2 ms |
9052 KB |
Output is correct |
26 |
Correct |
5 ms |
9820 KB |
Output is correct |
27 |
Correct |
5 ms |
9816 KB |
Output is correct |
28 |
Correct |
5 ms |
9872 KB |
Output is correct |
29 |
Correct |
5 ms |
9820 KB |
Output is correct |
30 |
Correct |
5 ms |
9816 KB |
Output is correct |
31 |
Correct |
5 ms |
9816 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9308 KB |
Output is correct |
12 |
Correct |
2 ms |
9308 KB |
Output is correct |
13 |
Correct |
1 ms |
9308 KB |
Output is correct |
14 |
Correct |
1 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
9308 KB |
Output is correct |
16 |
Correct |
2 ms |
9308 KB |
Output is correct |
17 |
Correct |
2 ms |
9308 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9820 KB |
Output is correct |
23 |
Correct |
5 ms |
9820 KB |
Output is correct |
24 |
Correct |
5 ms |
9820 KB |
Output is correct |
25 |
Correct |
149 ms |
67648 KB |
Output is correct |
26 |
Correct |
125 ms |
71504 KB |
Output is correct |
27 |
Correct |
123 ms |
71504 KB |
Output is correct |
28 |
Correct |
200 ms |
71504 KB |
Output is correct |
29 |
Correct |
209 ms |
71504 KB |
Output is correct |
30 |
Correct |
197 ms |
71504 KB |
Output is correct |
31 |
Correct |
197 ms |
71504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9212 KB |
Output is correct |
2 |
Correct |
1 ms |
9308 KB |
Output is correct |
3 |
Correct |
216 ms |
63056 KB |
Output is correct |
4 |
Correct |
219 ms |
62892 KB |
Output is correct |
5 |
Correct |
224 ms |
63056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9308 KB |
Output is correct |
12 |
Correct |
2 ms |
9308 KB |
Output is correct |
13 |
Correct |
1 ms |
9308 KB |
Output is correct |
14 |
Correct |
1 ms |
9308 KB |
Output is correct |
15 |
Correct |
2 ms |
9308 KB |
Output is correct |
16 |
Correct |
2 ms |
9308 KB |
Output is correct |
17 |
Correct |
2 ms |
9308 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9820 KB |
Output is correct |
23 |
Correct |
5 ms |
9820 KB |
Output is correct |
24 |
Correct |
5 ms |
9820 KB |
Output is correct |
25 |
Correct |
2 ms |
9052 KB |
Output is correct |
26 |
Correct |
5 ms |
9820 KB |
Output is correct |
27 |
Correct |
5 ms |
9816 KB |
Output is correct |
28 |
Correct |
5 ms |
9872 KB |
Output is correct |
29 |
Correct |
5 ms |
9820 KB |
Output is correct |
30 |
Correct |
5 ms |
9816 KB |
Output is correct |
31 |
Correct |
5 ms |
9816 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
5 ms |
9820 KB |
Output is correct |
34 |
Correct |
149 ms |
67648 KB |
Output is correct |
35 |
Correct |
125 ms |
71504 KB |
Output is correct |
36 |
Correct |
123 ms |
71504 KB |
Output is correct |
37 |
Correct |
200 ms |
71504 KB |
Output is correct |
38 |
Correct |
209 ms |
71504 KB |
Output is correct |
39 |
Correct |
197 ms |
71504 KB |
Output is correct |
40 |
Correct |
197 ms |
71504 KB |
Output is correct |
41 |
Correct |
3 ms |
9212 KB |
Output is correct |
42 |
Correct |
1 ms |
9308 KB |
Output is correct |
43 |
Correct |
216 ms |
63056 KB |
Output is correct |
44 |
Correct |
219 ms |
62892 KB |
Output is correct |
45 |
Correct |
224 ms |
63056 KB |
Output is correct |
46 |
Correct |
253 ms |
69040 KB |
Output is correct |
47 |
Correct |
254 ms |
68432 KB |
Output is correct |
48 |
Correct |
249 ms |
69448 KB |
Output is correct |
49 |
Correct |
255 ms |
68448 KB |
Output is correct |
50 |
Correct |
259 ms |
66280 KB |
Output is correct |
51 |
Correct |
256 ms |
66240 KB |
Output is correct |
52 |
Correct |
255 ms |
66128 KB |
Output is correct |
53 |
Correct |
266 ms |
66136 KB |
Output is correct |