#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<math.h>
#include<assert.h>
using namespace std;
const double PI = (double) atan(1.0) * 4;
const int64_t INF = (int64_t) 1e18 + 777;
const int64_t mINF = (int64_t) 2e9 + 777;
const int64_t offset = (int64_t) 1e6 + 777;
const int64_t MOD = 1e9 + 7;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
struct LCA
{
int n; int t;
int LOG;
vector<int> f;
vector<int> pos;
vector<int> depth;
vector<vector<int> > rmq;
vector<vector<int> > adj;
LCA(int n_)
{
n = n_; t = 0;
LOG = 0;
f.resize(n, -1);
adj.resize(n);
depth.resize(n, 0);
}
void prep()
{
DFS();
while((1 << LOG) <= t) LOG++;
rmq.resize(LOG, vector<int>(t, 0));
for(int i = 0; i < t; i++)
{
rmq[0][i] = pos[i];
}
for(int i = 1; i < LOG; i++)
{
for(int j = 0; j + (1 << i) - 1 < t; j++)
{
int d1 = depth[ rmq[i - 1][j] ];
int d2 = depth[ rmq[i - 1][j + (1 << (i - 1))] ];
if(d1 < d2) rmq[i][j] = rmq[i - 1][j];
else rmq[i][j] = rmq[i - 1][j + (1 << (i - 1))];
}
}
}
int getLCA(int i, int j)
{
i = f[i]; j = f[j];
if(i > j) swap(i, j);
int len = j - i + 1;
int bt = 31 - __builtin_clz(len);
if(depth[ rmq[bt][i] ] < depth[ rmq[bt][j - (1 << bt) + 1] ]) return rmq[bt][i];
return rmq[bt][j - (1 << bt) + 1];
}
int dis(int u, int v)
{
int x = getLCA(u, v);
return depth[u] + depth[v] - 2 * depth[x];
}
void addEdge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void DFS(int u = 0, int p = -1)
{
f[u] = t++;
pos.push_back(u);
for(int i = 0; i < (int) adj[u].size(); i++)
{
int v = adj[u][i];
if(v == p) continue;
depth[v] = depth[u] + 1;
DFS(v, u);
pos.push_back(u);
t++;
}
}
};
struct Solution
{
int n;
vector<int> a;
vector<int> ver;
vector<int> link;
vector<int> size;
vector<vector<int> > adj;
Solution() {}
void solve()
{
cin >> n;
a.resize(n);
ver.resize(n, -1);
LCA lca(n);
adj.resize(n);
for(int i = 0; i < n; i++)
{
cin >> a[i];
a[i]--;
ver[a[i]] = i;
}
for(int i = 0; i < n - 1; i++)
{
int u; int v;
cin >> u >> v;
u--; v--;
lca.addEdge(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
}
lca.prep();
link.resize(n);
for(int i = 0; i < n; i++)
{
link[i] = i;
}
vector<int64_t> dp(n, 0);
for(int i = 0; i < n; i++)
{
int u = ver[i];
for(int j = 0; j < (int) adj[u].size(); j++)
{
int v = adj[u][j];
if(a[v] > a[u]) continue;
int x = find(v);
dp[u] = max(dp[u], dp[x] + lca.dis(u, x));
unite(u, x);
}
}
cout << dp[ver[n - 1]] << "\n";
}
int find(int x)
{
while(x != link[x])
{
x = link[x];
}
// cerr << "c = " << c << "\n";
return x;
}
void unite(int x, int y)
{
y = find(y);
link[y] = x;
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int64_t Abs(int64_t t1)
{
if(t1 < 0) return -t1;
return t1;
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
1892 KB |
Output is correct |
22 |
Correct |
3 ms |
1764 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
1892 KB |
Output is correct |
22 |
Correct |
3 ms |
1764 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1912 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1824 KB |
Output is correct |
27 |
Correct |
3 ms |
1884 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1872 KB |
Output is correct |
30 |
Correct |
3 ms |
1880 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1736 KB |
Output is correct |
33 |
Correct |
3 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
1892 KB |
Output is correct |
22 |
Correct |
3 ms |
1764 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1912 KB |
Output is correct |
25 |
Correct |
100 ms |
68284 KB |
Output is correct |
26 |
Correct |
102 ms |
68452 KB |
Output is correct |
27 |
Correct |
141 ms |
68288 KB |
Output is correct |
28 |
Correct |
124 ms |
68460 KB |
Output is correct |
29 |
Correct |
128 ms |
68228 KB |
Output is correct |
30 |
Correct |
128 ms |
68440 KB |
Output is correct |
31 |
Correct |
147 ms |
68264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
444 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
621 ms |
62868 KB |
Output is correct |
4 |
Correct |
755 ms |
62868 KB |
Output is correct |
5 |
Correct |
520 ms |
62632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
1892 KB |
Output is correct |
22 |
Correct |
3 ms |
1764 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1912 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1824 KB |
Output is correct |
27 |
Correct |
3 ms |
1884 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1872 KB |
Output is correct |
30 |
Correct |
3 ms |
1880 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1736 KB |
Output is correct |
33 |
Correct |
3 ms |
1664 KB |
Output is correct |
34 |
Correct |
100 ms |
68284 KB |
Output is correct |
35 |
Correct |
102 ms |
68452 KB |
Output is correct |
36 |
Correct |
141 ms |
68288 KB |
Output is correct |
37 |
Correct |
124 ms |
68460 KB |
Output is correct |
38 |
Correct |
128 ms |
68228 KB |
Output is correct |
39 |
Correct |
128 ms |
68440 KB |
Output is correct |
40 |
Correct |
147 ms |
68264 KB |
Output is correct |
41 |
Correct |
1 ms |
444 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
621 ms |
62868 KB |
Output is correct |
44 |
Correct |
755 ms |
62868 KB |
Output is correct |
45 |
Correct |
520 ms |
62632 KB |
Output is correct |
46 |
Correct |
679 ms |
66004 KB |
Output is correct |
47 |
Correct |
698 ms |
65532 KB |
Output is correct |
48 |
Correct |
678 ms |
66472 KB |
Output is correct |
49 |
Correct |
663 ms |
65444 KB |
Output is correct |
50 |
Correct |
257 ms |
63176 KB |
Output is correct |
51 |
Correct |
248 ms |
63144 KB |
Output is correct |
52 |
Correct |
264 ms |
62940 KB |
Output is correct |
53 |
Correct |
281 ms |
62988 KB |
Output is correct |