#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 2e5+5;
vector<int>adj[MAXN],g[MAXN];
int p[MAXN],rp[MAXN],t[4*MAXN],lazy[4*MAXN];
int tin[MAXN],tout[MAXN],dep[MAXN],par[20][MAXN];
int timer = 0;
void dfs(int v,int u){
tin[v] = ++timer;
for(int x:adj[v]){
if(x == u)continue;
par[0][x] = v;
dep[x] = dep[v]+1;
for(int i=1;i<=18;i++){
par[i][x] = par[i-1][par[i-1][x]];
}
dfs(x,v);
}
tout[v] = timer;
}
bool ancestor(int v,int u){return(tin[v] >= tin[u] && tout[u] >= tout[v]);}//is u an ancestor of v?
int lca(int v,int u){
if(dep[v] > dep[u])swap(u,v);
int dist = dep[u]-dep[v];
for(int i=0;dist;i++,dist>>=1){
if(dist&1)u = par[i][u];
}
if(u == v)return u;
for(int i=18;i>=0;i--){
if(par[i][v] != par[i][u]){
u = par[i][u];
v = par[i][v];
}
}
return par[0][v];
}
int dist(int u,int v){
int w = lca(u,v);
return dep[u] + dep[v] - 2*dep[w];
}
int dfs2(int v){
int mx = 0;
for(int x:g[v]){
mx = max(mx,dfs2(x) + dist(x,v));
}
return mx;
}
int ded[MAXN];
int find(int v,int u){
int res = p[v];
for(int x:adj[v]){
if(x==u || ded[x])continue;
res = max(res,find(x,v));
}
return res;
}
void solve(int v,int p){
v = rp[find(v,-1)];
ded[v] = 1;
if(p)g[p].pb(v);
for(int x:adj[v]){
if(ded[x])continue;
solve(x,v);
}
}
int main()
{
owo
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>p[i];
rp[p[i]] = i;
}
for(int i=1;i<n;i++){
int v,u;
cin>>v>>u;
adj[v].pb(u);
adj[u].pb(v);
}
dfs(rp[n],-1);
solve(1,0);
cout<<dfs2(rp[n]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9840 KB |
Output is correct |
9 |
Correct |
4 ms |
9812 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9840 KB |
Output is correct |
9 |
Correct |
4 ms |
9812 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
4 ms |
9812 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
4 ms |
9864 KB |
Output is correct |
17 |
Correct |
4 ms |
9880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9840 KB |
Output is correct |
9 |
Correct |
4 ms |
9812 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
4 ms |
9812 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
4 ms |
9864 KB |
Output is correct |
17 |
Correct |
4 ms |
9880 KB |
Output is correct |
18 |
Correct |
89 ms |
11140 KB |
Output is correct |
19 |
Correct |
79 ms |
11012 KB |
Output is correct |
20 |
Correct |
79 ms |
10904 KB |
Output is correct |
21 |
Correct |
7 ms |
10892 KB |
Output is correct |
22 |
Correct |
7 ms |
10836 KB |
Output is correct |
23 |
Correct |
6 ms |
10836 KB |
Output is correct |
24 |
Correct |
7 ms |
10836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9840 KB |
Output is correct |
9 |
Correct |
4 ms |
9812 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
4 ms |
9812 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
4 ms |
9864 KB |
Output is correct |
17 |
Correct |
4 ms |
9880 KB |
Output is correct |
18 |
Correct |
89 ms |
11140 KB |
Output is correct |
19 |
Correct |
79 ms |
11012 KB |
Output is correct |
20 |
Correct |
79 ms |
10904 KB |
Output is correct |
21 |
Correct |
7 ms |
10892 KB |
Output is correct |
22 |
Correct |
7 ms |
10836 KB |
Output is correct |
23 |
Correct |
6 ms |
10836 KB |
Output is correct |
24 |
Correct |
7 ms |
10836 KB |
Output is correct |
25 |
Correct |
4 ms |
9812 KB |
Output is correct |
26 |
Correct |
89 ms |
10784 KB |
Output is correct |
27 |
Correct |
101 ms |
11048 KB |
Output is correct |
28 |
Correct |
89 ms |
10832 KB |
Output is correct |
29 |
Correct |
88 ms |
10820 KB |
Output is correct |
30 |
Correct |
10 ms |
10580 KB |
Output is correct |
31 |
Correct |
16 ms |
10580 KB |
Output is correct |
32 |
Correct |
12 ms |
10580 KB |
Output is correct |
33 |
Correct |
15 ms |
10580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9840 KB |
Output is correct |
9 |
Correct |
4 ms |
9812 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
4 ms |
9812 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
4 ms |
9864 KB |
Output is correct |
17 |
Correct |
4 ms |
9880 KB |
Output is correct |
18 |
Correct |
89 ms |
11140 KB |
Output is correct |
19 |
Correct |
79 ms |
11012 KB |
Output is correct |
20 |
Correct |
79 ms |
10904 KB |
Output is correct |
21 |
Correct |
7 ms |
10892 KB |
Output is correct |
22 |
Correct |
7 ms |
10836 KB |
Output is correct |
23 |
Correct |
6 ms |
10836 KB |
Output is correct |
24 |
Correct |
7 ms |
10836 KB |
Output is correct |
25 |
Execution timed out |
2087 ms |
50424 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Execution timed out |
2075 ms |
39360 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9840 KB |
Output is correct |
9 |
Correct |
4 ms |
9812 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
4 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
4 ms |
9812 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
4 ms |
9864 KB |
Output is correct |
17 |
Correct |
4 ms |
9880 KB |
Output is correct |
18 |
Correct |
89 ms |
11140 KB |
Output is correct |
19 |
Correct |
79 ms |
11012 KB |
Output is correct |
20 |
Correct |
79 ms |
10904 KB |
Output is correct |
21 |
Correct |
7 ms |
10892 KB |
Output is correct |
22 |
Correct |
7 ms |
10836 KB |
Output is correct |
23 |
Correct |
6 ms |
10836 KB |
Output is correct |
24 |
Correct |
7 ms |
10836 KB |
Output is correct |
25 |
Correct |
4 ms |
9812 KB |
Output is correct |
26 |
Correct |
89 ms |
10784 KB |
Output is correct |
27 |
Correct |
101 ms |
11048 KB |
Output is correct |
28 |
Correct |
89 ms |
10832 KB |
Output is correct |
29 |
Correct |
88 ms |
10820 KB |
Output is correct |
30 |
Correct |
10 ms |
10580 KB |
Output is correct |
31 |
Correct |
16 ms |
10580 KB |
Output is correct |
32 |
Correct |
12 ms |
10580 KB |
Output is correct |
33 |
Correct |
15 ms |
10580 KB |
Output is correct |
34 |
Execution timed out |
2087 ms |
50424 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |