#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mkp make_pair
#define MEM(x) memset(x,0,sizeof(x))
#define ALL(x) begin(x), end(x)
#define PH push
#define PB push_back
#define REP(i,N) for( int i = 0; i <(N); ++i )
#define FOR(i,a,b) for( int i = (a); i <= (b); ++i )
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
template<typename T>
using V = vector<T>;
#ifdef LOCAL
int __lg( int x ) {
return 31 - __builtin_clz( x );
}
#endif
struct DSU {
const int N;
V<int> fth;
int find( int a ) { return a == fth[a] ? a : fth[a] = find(fth[a]);};
int merge( int a, int b ) { // make a be father
const int fa = find( a ), fb = find( b );
if( fa == fb ) return 0;
fth[ fb ] = fa;
return 1;
}
DSU( int N ) : N(N) {
fth.assign( N+1, 0 );
iota( ALL(fth), 0 );
}
};
struct LCA {
const int N;
int lgN, tm;
V<int> dis;
V<int> tin, tout;
V<V<int>> fth;
V<V<int>> G;
void dfs( int cur, int par ) {
tin[cur]=++tm;
for( int i = 1; i < lgN; ++i )
fth[ i ][ cur ] = fth[ i-1 ][ fth[i-1][cur] ];
for( auto &i : G[cur] ) {
if( i == par ) continue;
fth[ 0 ][ i ] = cur;
dis[ i ] = dis[ cur ] + 1;
dfs( i, cur );
}
tout[cur]=++tm;
}
bool isfather( int a, int b ) {
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca( int a, int b ) {
if( isfather(a,b) ) return a;
if( isfather(b,a) ) return b;
for( int i = lgN-1; i >= 0; --i ) {
if( !isfather( fth[ i ][ a ], b ) )
a = fth[ i ][ a];
}
return fth[ 0 ][ a ];
}
int Dis( int a, int b ) {
return dis[a]+dis[b]-2*dis[lca(a,b)];
}
LCA( V<V<int>>&_G, int N ) : N(N) {
tm = 0;
lgN = __lg( N ) + 1;
tin.assign( N+1, 0 );
tout.assign( N+1, 0 );
fth.assign( lgN, V<int>(N+1,0));
dis.assign( N+1, 0 );
G=_G;
dfs( 1, 0 );
V<V<int>>().swap( G );
tin[0] = 0;
tout[0] = 1000000000;
}
};
void solve() {
int N; cin >> N;
V<int> NodeOf(N+1,0);
FOR(i,1,N) {
int p; cin >> p;
NodeOf[ p ] = i;
}
V<V<int>> G(N+1);
FOR(i,2,N) {
int a, b; cin >> a >> b;
G[a].PB( b );
G[b].PB( a );
}
LCA lca(G, N);
DSU dsu(N+1);
V<int> vis(N+1,0);
V<ll> dp(N+1,0);
FOR(_,1,N) {
auto nd = NodeOf[_];
for( auto&j : G[nd] ) {
if( !vis[j] ) continue;
auto to = dsu.find( j );
dp[ nd ] = max( dp[ nd ], dp[ to ] + lca.Dis(nd,to));
dsu.merge( nd, j );
}
vis[ nd ] = true;
}
cout << dp[ NodeOf[N] ] << '\n';
}
int main () {
int T=1;
#ifdef LOCAL
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );
cin >> T;
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
while(T--) solve();
}
# |
Verdict |
Execution time |
Memory |
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 |
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 |
0 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 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 |
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 |
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 |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1624 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
124 ms |
47316 KB |
Output is correct |
26 |
Correct |
110 ms |
47424 KB |
Output is correct |
27 |
Correct |
111 ms |
47292 KB |
Output is correct |
28 |
Correct |
140 ms |
47392 KB |
Output is correct |
29 |
Correct |
130 ms |
47348 KB |
Output is correct |
30 |
Correct |
130 ms |
47428 KB |
Output is correct |
31 |
Correct |
130 ms |
47264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
248 ms |
43308 KB |
Output is correct |
4 |
Correct |
221 ms |
43076 KB |
Output is correct |
5 |
Correct |
274 ms |
43308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1624 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1232 KB |
Output is correct |
34 |
Correct |
124 ms |
47316 KB |
Output is correct |
35 |
Correct |
110 ms |
47424 KB |
Output is correct |
36 |
Correct |
111 ms |
47292 KB |
Output is correct |
37 |
Correct |
140 ms |
47392 KB |
Output is correct |
38 |
Correct |
130 ms |
47348 KB |
Output is correct |
39 |
Correct |
130 ms |
47428 KB |
Output is correct |
40 |
Correct |
130 ms |
47264 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
248 ms |
43308 KB |
Output is correct |
44 |
Correct |
221 ms |
43076 KB |
Output is correct |
45 |
Correct |
274 ms |
43308 KB |
Output is correct |
46 |
Correct |
282 ms |
47548 KB |
Output is correct |
47 |
Correct |
304 ms |
46916 KB |
Output is correct |
48 |
Correct |
251 ms |
48024 KB |
Output is correct |
49 |
Correct |
272 ms |
46912 KB |
Output is correct |
50 |
Correct |
332 ms |
43772 KB |
Output is correct |
51 |
Correct |
281 ms |
43300 KB |
Output is correct |
52 |
Correct |
284 ms |
43328 KB |
Output is correct |
53 |
Correct |
313 ms |
43356 KB |
Output is correct |