답안 #827891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827891 2023-08-16T21:44:31 Z CSQ31 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
409 ms 64160 KB
#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],n;
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];
}

////lca template
int t[4*MAXN];
void upd(int v,int pos,int tl,int tr,int val){
	if(tl==tr){
		t[v] = val;
		return;
	}
	int tm = (tl+tr)/2;
	if(pos<=tm)upd(2*v,pos,tl,tm,val);
	else upd(2*v+1,pos,tm+1,tr,val);
	t[v] = max(t[2*v],t[2*v+1]);
}
int query(int v,int l,int r,int tl,int tr){
	if(l>r)return -1;
	if(l==tl && r==tr)return t[v];
	int tm = (tl+tr)/2;
	return max(
	query(2*v,l,min(r,tm),tl,tm),
	query(2*v+1,max(l,tm+1),r,tm+1,tr));
}
int ded[MAXN];
int solve(int v){
	int rt = query(1,tin[v],tout[v],1,n);
	//cout<<v<<" "<<rt<<'\n';
	rt = rp[rt];
	ded[rt] = 1;
	upd(1,tin[rt],1,n,-1);
	for(int x:adj[rt]){
		if(x == par[0][rt] || ded[x])continue;
		int y = solve(x);
		g[rt].pb(y);
	}
	if(rt != v){
		int y = solve(v);
		g[rt].pb(y);
	}
	return rt;
}
ll dfs2(int v){
	ll mx = 0;
	for(int x:g[v]){
		mx = max(mx,dfs2(x) + dist(x,v));
	}
	return mx;
}
//segtree
int main()
{
	owo
	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);
	for(int i=1;i<=n;i++)upd(1,tin[i],1,n,p[i]);
	
	solve(rp[n]);
	cout<<dfs2(rp[n]);
	
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 6 ms 9828 KB Output is correct
3 Correct 6 ms 9796 KB Output is correct
4 Correct 4 ms 9784 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9860 KB Output is correct
8 Correct 4 ms 9828 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 6 ms 9828 KB Output is correct
3 Correct 6 ms 9796 KB Output is correct
4 Correct 4 ms 9784 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9860 KB Output is correct
8 Correct 4 ms 9828 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 5 ms 9940 KB Output is correct
12 Correct 4 ms 9812 KB Output is correct
13 Correct 4 ms 9940 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 6 ms 9828 KB Output is correct
3 Correct 6 ms 9796 KB Output is correct
4 Correct 4 ms 9784 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9860 KB Output is correct
8 Correct 4 ms 9828 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 5 ms 9940 KB Output is correct
12 Correct 4 ms 9812 KB Output is correct
13 Correct 4 ms 9940 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9888 KB Output is correct
18 Correct 8 ms 11092 KB Output is correct
19 Correct 8 ms 11148 KB Output is correct
20 Correct 8 ms 11092 KB Output is correct
21 Correct 8 ms 10964 KB Output is correct
22 Correct 9 ms 10836 KB Output is correct
23 Correct 8 ms 10836 KB Output is correct
24 Correct 10 ms 10860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 6 ms 9828 KB Output is correct
3 Correct 6 ms 9796 KB Output is correct
4 Correct 4 ms 9784 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9860 KB Output is correct
8 Correct 4 ms 9828 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 5 ms 9940 KB Output is correct
12 Correct 4 ms 9812 KB Output is correct
13 Correct 4 ms 9940 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9888 KB Output is correct
18 Correct 8 ms 11092 KB Output is correct
19 Correct 8 ms 11148 KB Output is correct
20 Correct 8 ms 11092 KB Output is correct
21 Correct 8 ms 10964 KB Output is correct
22 Correct 9 ms 10836 KB Output is correct
23 Correct 8 ms 10836 KB Output is correct
24 Correct 10 ms 10860 KB Output is correct
25 Correct 6 ms 9812 KB Output is correct
26 Correct 10 ms 10964 KB Output is correct
27 Correct 9 ms 11036 KB Output is correct
28 Correct 8 ms 10964 KB Output is correct
29 Correct 12 ms 10960 KB Output is correct
30 Correct 9 ms 10580 KB Output is correct
31 Correct 8 ms 10580 KB Output is correct
32 Correct 8 ms 10580 KB Output is correct
33 Correct 8 ms 10580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 6 ms 9828 KB Output is correct
3 Correct 6 ms 9796 KB Output is correct
4 Correct 4 ms 9784 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9860 KB Output is correct
8 Correct 4 ms 9828 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 5 ms 9940 KB Output is correct
12 Correct 4 ms 9812 KB Output is correct
13 Correct 4 ms 9940 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9888 KB Output is correct
18 Correct 8 ms 11092 KB Output is correct
19 Correct 8 ms 11148 KB Output is correct
20 Correct 8 ms 11092 KB Output is correct
21 Correct 8 ms 10964 KB Output is correct
22 Correct 9 ms 10836 KB Output is correct
23 Correct 8 ms 10836 KB Output is correct
24 Correct 10 ms 10860 KB Output is correct
25 Correct 163 ms 62608 KB Output is correct
26 Correct 168 ms 64040 KB Output is correct
27 Correct 202 ms 64160 KB Output is correct
28 Correct 165 ms 57256 KB Output is correct
29 Correct 162 ms 59332 KB Output is correct
30 Correct 162 ms 58832 KB Output is correct
31 Correct 172 ms 59620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 5 ms 9868 KB Output is correct
3 Correct 196 ms 42848 KB Output is correct
4 Correct 195 ms 43100 KB Output is correct
5 Correct 192 ms 42856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 6 ms 9828 KB Output is correct
3 Correct 6 ms 9796 KB Output is correct
4 Correct 4 ms 9784 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9860 KB Output is correct
8 Correct 4 ms 9828 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 5 ms 9940 KB Output is correct
12 Correct 4 ms 9812 KB Output is correct
13 Correct 4 ms 9940 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9888 KB Output is correct
18 Correct 8 ms 11092 KB Output is correct
19 Correct 8 ms 11148 KB Output is correct
20 Correct 8 ms 11092 KB Output is correct
21 Correct 8 ms 10964 KB Output is correct
22 Correct 9 ms 10836 KB Output is correct
23 Correct 8 ms 10836 KB Output is correct
24 Correct 10 ms 10860 KB Output is correct
25 Correct 6 ms 9812 KB Output is correct
26 Correct 10 ms 10964 KB Output is correct
27 Correct 9 ms 11036 KB Output is correct
28 Correct 8 ms 10964 KB Output is correct
29 Correct 12 ms 10960 KB Output is correct
30 Correct 9 ms 10580 KB Output is correct
31 Correct 8 ms 10580 KB Output is correct
32 Correct 8 ms 10580 KB Output is correct
33 Correct 8 ms 10580 KB Output is correct
34 Correct 163 ms 62608 KB Output is correct
35 Correct 168 ms 64040 KB Output is correct
36 Correct 202 ms 64160 KB Output is correct
37 Correct 165 ms 57256 KB Output is correct
38 Correct 162 ms 59332 KB Output is correct
39 Correct 162 ms 58832 KB Output is correct
40 Correct 172 ms 59620 KB Output is correct
41 Correct 5 ms 9812 KB Output is correct
42 Correct 5 ms 9868 KB Output is correct
43 Correct 196 ms 42848 KB Output is correct
44 Correct 195 ms 43100 KB Output is correct
45 Correct 192 ms 42856 KB Output is correct
46 Correct 409 ms 59736 KB Output is correct
47 Correct 374 ms 59844 KB Output is correct
48 Correct 396 ms 59768 KB Output is correct
49 Correct 367 ms 59720 KB Output is correct
50 Correct 333 ms 45660 KB Output is correct
51 Correct 395 ms 45764 KB Output is correct
52 Correct 376 ms 45664 KB Output is correct
53 Correct 333 ms 45756 KB Output is correct