#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;
struct Node
{
int v, val;
bool operator < (const Node &node) const
{
return val < node.val;
}
};
int n,cnt,base,rozmiar_drzewa;
int A[N];
vector<int> G[N];
bool vis[N];
int pre[N];
int siz[N];
int P[N];
int deg[N];
vector<Node> tree;
void DFS(int v, int par)
{
//cout << "K: " << v << '\n';
siz[v] = 1, pre[v] = ++cnt, P[v] = par, tree[cnt+base] = {v,A[v]};
for(auto x : G[v])
{
if(x == par) continue;
deg[x] = deg[v] + 1, DFS(x,v), siz[v] += siz[x];
}
}
Node query(int l, int p)
{
l = l + base - 1, p = p + base + 1;
Node res = {-1,-1};
while(l/2 != p/2)
{
if(l % 2 == 0) res = max(res,tree[l+1]);
if(p % 2 == 1) res = max(res,tree[p-1]);
l /= 2, p /= 2;
}
return res;
}
void update(int v, Node val)
{
v += base, tree[v] = val, v /= 2;
while(v > 0) tree[v] = max(tree[v*2],tree[v*2+1]), v /= 2;
}
int odl(int a, int b)
{
int res = 0;
while(a != b)
{
++res;
if(deg[a] > deg[b]) a = P[a];
else b = P[b];
}
return res;
}
//int cm= 0;
ll DFS2(int v, int b)
{
//++cm;
//if(cm >= 8) return 0;
//cout << "V: " << v << " B: " << b << '\n';
vis[v] = 1;
ll val = 0;
update(pre[v],{-1,-1});
for(auto x : G[v])
{
if(vis[x]) continue;
if(deg[x] > deg[v])
{
Node akt = query(pre[x],pre[x]+siz[x]-1);
//cout << "VV: " << v << " XX: " << x << " AKT V: " << akt.v << " AKT VAL: " << akt.val << '\n';
val = max(val,(ll)odl(v,akt.v)+DFS2(akt.v,x));
}
}
if(v != b)
{
Node akt = query(pre[b],pre[b]+siz[b]-1);
//cout << "VVVVV: " << v << " AKT V: " << akt.v << " AKT VAL: " << akt.val << '\n';
val = max(val,(ll)odl(v,akt.v)+DFS2(akt.v,b));
}
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);
}
base = 1;
while(base <= n+5) base *= 2;
rozmiar_drzewa = base * 2;
tree.assign(rozmiar_drzewa,{-1,-1});
deg[1] = 1, DFS(1,0);
for(int i = base-1; i >= 1; --i) tree[i] = max(tree[i*2],tree[i*2+1]);
int wie = 1;
for(int i = 1; i <= n; ++i) if(A[i] == n) wie = i;
cout << DFS2(wie,1) << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
//cin >> T;
while(T--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
2 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
2 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16728 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
2 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16728 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
30 ms |
17704 KB |
Output is correct |
19 |
Correct |
17 ms |
17716 KB |
Output is correct |
20 |
Correct |
18 ms |
15072 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
2 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16728 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
30 ms |
17704 KB |
Output is correct |
19 |
Correct |
17 ms |
17716 KB |
Output is correct |
20 |
Correct |
18 ms |
15072 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
3 ms |
14172 KB |
Output is correct |
26 |
Correct |
12 ms |
14896 KB |
Output is correct |
27 |
Correct |
14 ms |
14940 KB |
Output is correct |
28 |
Correct |
11 ms |
14936 KB |
Output is correct |
29 |
Correct |
12 ms |
14940 KB |
Output is correct |
30 |
Correct |
5 ms |
14652 KB |
Output is correct |
31 |
Correct |
5 ms |
14680 KB |
Output is correct |
32 |
Correct |
6 ms |
14680 KB |
Output is correct |
33 |
Correct |
5 ms |
14524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
2 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16728 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
30 ms |
17704 KB |
Output is correct |
19 |
Correct |
17 ms |
17716 KB |
Output is correct |
20 |
Correct |
18 ms |
15072 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Execution timed out |
2029 ms |
47440 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14172 KB |
Output is correct |
3 |
Correct |
96 ms |
33104 KB |
Output is correct |
4 |
Correct |
75 ms |
33616 KB |
Output is correct |
5 |
Correct |
76 ms |
33104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
2 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16728 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
4 ms |
16728 KB |
Output is correct |
18 |
Correct |
30 ms |
17704 KB |
Output is correct |
19 |
Correct |
17 ms |
17716 KB |
Output is correct |
20 |
Correct |
18 ms |
15072 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
3 ms |
14172 KB |
Output is correct |
26 |
Correct |
12 ms |
14896 KB |
Output is correct |
27 |
Correct |
14 ms |
14940 KB |
Output is correct |
28 |
Correct |
11 ms |
14936 KB |
Output is correct |
29 |
Correct |
12 ms |
14940 KB |
Output is correct |
30 |
Correct |
5 ms |
14652 KB |
Output is correct |
31 |
Correct |
5 ms |
14680 KB |
Output is correct |
32 |
Correct |
6 ms |
14680 KB |
Output is correct |
33 |
Correct |
5 ms |
14524 KB |
Output is correct |
34 |
Execution timed out |
2029 ms |
47440 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |