#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 1e6 + 7, Inf = 1e9 + 7;
void print(bool x) { cout << (x ? "YES" : "NO") << endl; }
string tostring (__int128 x)
{
string ans = "";
while(x > 0)
{
ans += (x % 10 + '0');
x /= 10;
}
reverse(all(ans));
return ans;
}
vector <vector<int>> v;
struct LowerCommonAncestor
{
vector <int> L, E, H; int idx, n;
void dfs(int cur, int depth, int parent)
{
H[cur] = idx;
E[idx] = cur;
L[idx++] = depth;
for (auto& u : v[cur]) if(u != parent)
{
dfs(u, depth + 1, cur);
E[idx] = cur;
L[idx++] = depth;
}
}
vector <int> tree; int l;
int query(int node, int x, int y, int s, int e)
{
if (x > e || y < s) return 0;
if (s >= x && e <= y)
return tree[node];
int middle = (s + e) / 2;
int id1 = query(node * 2, x, y, s, middle),
id2 = query(node * 2 + 1, x, y, middle + 1, e);
return (L[id1] > L[id2] ? id2 : id1);
}
int query(int i, int j) { return query(1, i, j, 1, l); }
void Segment_tree(int n)
{
for (l = 1; l < n; l = (l << 1));
tree.assign(2 * l, 0); L[0] = Inf;
for (int i = 0; i < n; i++)
tree[i + l] = i + 1;
for (int i = l - 1; i > 0; i--)
{
tree[i] = (L[tree[2 * i]] > L[tree[2 * i + 1]] ?
tree[2 * i + 1] : tree[2 * i]);
}
}
int Ancestor(int i, int j)
{
int a = min(H[i], H[j]), b = max(H[i], H[j]);
return E[query(a, b)];
}
int Dist(int x)
{
return L[H[x]];
}
int sol(int i, int j)
{
int g = Ancestor(i, j);
return (Dist(i) + Dist(j) - (2 * Dist(g)));
}
LowerCommonAncestor(int lenght, int x)
{
n = lenght; idx = 1;
L.assign(2 * n, -1);
E.assign(2 * n, -1);
H.assign(2 * n, -1);
dfs(x, 0, 0); Segment_tree(2*n-2); //RMQ();
}
};
struct DSU
{
vector <int> link, size, mx;
int find(int x)
{
while (x != link[x]) x = link[x];
return x;
}
void unite(int a, int b)
{
a = find(a); b = find(b);
if(a == b) return;
if(size[a] < size[b]) swap(a, b);
mx[a] = max(mx[a], mx[b]);
size[a] += size[b];
link[b] = a;
}
DSU(int n)
{
mx.assign(n+1, 0);
link.assign(n+1, 0);
size.assign(n+1, 1);
_for(i, n+1) link[i] = mx[i] = i;
}
};
void solve()
{
int n, ans = 0; cin >> n;
vector <int> s(n+1), dp(n+1);
_for(i, n) cin >> s[i+1];
v.assign(n+1, vector <int> ());
vector <pair<int, int>> in;
_for(i, n-1)
{
int a, b; cin >> a >> b;
a = s[a]; b = s[b];
if(a < b) swap(a, b);
v[a].push_back(b);
v[b].push_back(a);
in.push_back({a, b});
}
LowerCommonAncestor D(n, n);
sort(all(in)); DSU St(n);
for(auto& u : in)
{
int a = u.fs, b = u.sd;
a = St.find(a); b = St.find(b);
if(a == b) continue;
a = St.mx[a]; b = St.mx[b];
St.unite(a, b);
if(St.mx[a] < St.mx[b]) swap(a, b);
dp[a] = max(dp[a], dp[b] + D.sol(a, b));
ans = max(ans, dp[a]);
}
cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Q = 1; //cin >> Q;
while (Q--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1636 KB |
Output is correct |
19 |
Correct |
3 ms |
1624 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1624 KB |
Output is correct |
24 |
Correct |
3 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1636 KB |
Output is correct |
19 |
Correct |
3 ms |
1624 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1624 KB |
Output is correct |
24 |
Correct |
3 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1628 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1624 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 KB |
Output is correct |
32 |
Correct |
4 ms |
1628 KB |
Output is correct |
33 |
Correct |
4 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1636 KB |
Output is correct |
19 |
Correct |
3 ms |
1624 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1624 KB |
Output is correct |
24 |
Correct |
3 ms |
1628 KB |
Output is correct |
25 |
Correct |
128 ms |
51076 KB |
Output is correct |
26 |
Correct |
130 ms |
51836 KB |
Output is correct |
27 |
Correct |
125 ms |
51388 KB |
Output is correct |
28 |
Correct |
152 ms |
49084 KB |
Output is correct |
29 |
Correct |
166 ms |
50096 KB |
Output is correct |
30 |
Correct |
158 ms |
49848 KB |
Output is correct |
31 |
Correct |
154 ms |
50616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
177 ms |
45112 KB |
Output is correct |
4 |
Correct |
192 ms |
45128 KB |
Output is correct |
5 |
Correct |
175 ms |
44984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1636 KB |
Output is correct |
19 |
Correct |
3 ms |
1624 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1624 KB |
Output is correct |
24 |
Correct |
3 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1628 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1624 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 KB |
Output is correct |
32 |
Correct |
4 ms |
1628 KB |
Output is correct |
33 |
Correct |
4 ms |
1492 KB |
Output is correct |
34 |
Correct |
128 ms |
51076 KB |
Output is correct |
35 |
Correct |
130 ms |
51836 KB |
Output is correct |
36 |
Correct |
125 ms |
51388 KB |
Output is correct |
37 |
Correct |
152 ms |
49084 KB |
Output is correct |
38 |
Correct |
166 ms |
50096 KB |
Output is correct |
39 |
Correct |
158 ms |
49848 KB |
Output is correct |
40 |
Correct |
154 ms |
50616 KB |
Output is correct |
41 |
Correct |
1 ms |
504 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
177 ms |
45112 KB |
Output is correct |
44 |
Correct |
192 ms |
45128 KB |
Output is correct |
45 |
Correct |
175 ms |
44984 KB |
Output is correct |
46 |
Correct |
137 ms |
49288 KB |
Output is correct |
47 |
Correct |
151 ms |
50096 KB |
Output is correct |
48 |
Correct |
150 ms |
50052 KB |
Output is correct |
49 |
Correct |
148 ms |
49344 KB |
Output is correct |
50 |
Correct |
216 ms |
44704 KB |
Output is correct |
51 |
Correct |
202 ms |
45032 KB |
Output is correct |
52 |
Correct |
208 ms |
44732 KB |
Output is correct |
53 |
Correct |
210 ms |
44988 KB |
Output is correct |