답안 #949889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949889 2024-03-19T20:23:15 Z amirhoseinfar1385 Meetings 2 (JOI21_meetings2) C++17
100 / 100
195 ms 55908 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10,lg=19;
int n,lca[lg][maxn],high[maxn],timea=0,sz[maxn];
vector<int>adj[maxn];
vector<pair<int,int>>ady[maxn];
pair<int,int>stf[maxn];

bool zird(int u,int v){
	return stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second;
}

int getlca(int u,int v){
	if(zird(u,v)){
		return v;
	}
	if(zird(v,u)){
		return u;
	}
	for(int i=lg-1;i>=0;i--){
		if(lca[i][u]!=0&&zird(v,lca[i][u])==0){
			u=lca[i][u];
		}
	}
	return lca[0][u];
}

int dis(int u,int v){
	int uv=getlca(u,v);
	return high[u]+high[v]-2*high[uv];
}

struct dsu{
	int res=0;
	int par[maxn];
	pair<int,int>di[maxn];
	dsu(){
		for(int i=0;i<maxn;i++){
			par[i]=i;
			di[i]=make_pair(i,i);
		}
	}
	int root(int u){
		if(par[u]==u){
			return u;
		}
		return par[u]=root(par[u]);
	}
	void con(int u,int v){
		u=root(u),v=root(v);
		if(u!=v){
			par[v]=u;
			pair<int,int>hey;
			int mx=-1;
			vector<int>nag={di[u].first,di[u].second,di[v].first,di[v].second};
			for(int i=0;i<4;i++){
				for(int j=i+1;j<4;j++){
					if(dis(nag[i],nag[j])>mx){
						mx=dis(nag[i],nag[j]);
						hey=make_pair(nag[i],nag[j]);
					}
				}
			}
			di[u]=hey;
			res=max(res,mx);
		}
	}
}ds;

void vorod(){
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
}

void callca(){
	for(int i=1;i<lg;i++){
		for(int j=0;j<maxn;j++){
			lca[i][j]=lca[i-1][lca[i-1][j]];
		}
	}
}

void dfs(int u,int par=-1){
	sz[u]=1;
	timea++;
	stf[u].first=timea;
	for(auto x:adj[u]){
		if(x==par){
			continue;
		}
		high[x]=high[u]+1;
		lca[0][x]=u;
		dfs(x,u);
		sz[u]+=sz[x];
		ady[min(sz[x],n-sz[x])].push_back(make_pair(u,x));
	}
	stf[u].second=timea;
}

void pre(){
	dfs(1);
	callca();
}

void solve(){
	vector<int>res;
	for(int i=n;i>=1;i--){
		if(i&1){
			res.push_back(1);
			continue;
		}
		for(auto x:ady[i/2]){
			ds.con(x.first,x.second);
		}
		res.push_back(ds.res+1);
	}
	reverse(res.begin(),res.end());
	for(auto x:res){
		cout<<x<<"\n";
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("inp.txt","r",stdin);
	vorod();
	pre();
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29020 KB Output is correct
2 Correct 7 ms 30044 KB Output is correct
3 Correct 8 ms 30044 KB Output is correct
4 Correct 8 ms 30040 KB Output is correct
5 Correct 8 ms 30044 KB Output is correct
6 Correct 8 ms 30044 KB Output is correct
7 Correct 8 ms 30040 KB Output is correct
8 Correct 7 ms 30044 KB Output is correct
9 Correct 8 ms 30044 KB Output is correct
10 Correct 8 ms 30044 KB Output is correct
11 Correct 10 ms 30044 KB Output is correct
12 Correct 8 ms 30040 KB Output is correct
13 Correct 9 ms 30044 KB Output is correct
14 Correct 8 ms 30144 KB Output is correct
15 Correct 8 ms 30040 KB Output is correct
16 Correct 8 ms 30132 KB Output is correct
17 Correct 8 ms 30300 KB Output is correct
18 Correct 8 ms 30200 KB Output is correct
19 Correct 8 ms 30044 KB Output is correct
20 Correct 9 ms 30044 KB Output is correct
21 Correct 8 ms 30044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29020 KB Output is correct
2 Correct 7 ms 30044 KB Output is correct
3 Correct 8 ms 30044 KB Output is correct
4 Correct 8 ms 30040 KB Output is correct
5 Correct 8 ms 30044 KB Output is correct
6 Correct 8 ms 30044 KB Output is correct
7 Correct 8 ms 30040 KB Output is correct
8 Correct 7 ms 30044 KB Output is correct
9 Correct 8 ms 30044 KB Output is correct
10 Correct 8 ms 30044 KB Output is correct
11 Correct 10 ms 30044 KB Output is correct
12 Correct 8 ms 30040 KB Output is correct
13 Correct 9 ms 30044 KB Output is correct
14 Correct 8 ms 30144 KB Output is correct
15 Correct 8 ms 30040 KB Output is correct
16 Correct 8 ms 30132 KB Output is correct
17 Correct 8 ms 30300 KB Output is correct
18 Correct 8 ms 30200 KB Output is correct
19 Correct 8 ms 30044 KB Output is correct
20 Correct 9 ms 30044 KB Output is correct
21 Correct 8 ms 30044 KB Output is correct
22 Correct 10 ms 30296 KB Output is correct
23 Correct 10 ms 30300 KB Output is correct
24 Correct 10 ms 30300 KB Output is correct
25 Correct 10 ms 30300 KB Output is correct
26 Correct 9 ms 30300 KB Output is correct
27 Correct 10 ms 30344 KB Output is correct
28 Correct 9 ms 30296 KB Output is correct
29 Correct 10 ms 30368 KB Output is correct
30 Correct 10 ms 30300 KB Output is correct
31 Correct 10 ms 30460 KB Output is correct
32 Correct 10 ms 30556 KB Output is correct
33 Correct 10 ms 30556 KB Output is correct
34 Correct 9 ms 30296 KB Output is correct
35 Correct 9 ms 30300 KB Output is correct
36 Correct 10 ms 30296 KB Output is correct
37 Correct 10 ms 30296 KB Output is correct
38 Correct 10 ms 30368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29020 KB Output is correct
2 Correct 7 ms 30044 KB Output is correct
3 Correct 8 ms 30044 KB Output is correct
4 Correct 8 ms 30040 KB Output is correct
5 Correct 8 ms 30044 KB Output is correct
6 Correct 8 ms 30044 KB Output is correct
7 Correct 8 ms 30040 KB Output is correct
8 Correct 7 ms 30044 KB Output is correct
9 Correct 8 ms 30044 KB Output is correct
10 Correct 8 ms 30044 KB Output is correct
11 Correct 10 ms 30044 KB Output is correct
12 Correct 8 ms 30040 KB Output is correct
13 Correct 9 ms 30044 KB Output is correct
14 Correct 8 ms 30144 KB Output is correct
15 Correct 8 ms 30040 KB Output is correct
16 Correct 8 ms 30132 KB Output is correct
17 Correct 8 ms 30300 KB Output is correct
18 Correct 8 ms 30200 KB Output is correct
19 Correct 8 ms 30044 KB Output is correct
20 Correct 9 ms 30044 KB Output is correct
21 Correct 8 ms 30044 KB Output is correct
22 Correct 10 ms 30296 KB Output is correct
23 Correct 10 ms 30300 KB Output is correct
24 Correct 10 ms 30300 KB Output is correct
25 Correct 10 ms 30300 KB Output is correct
26 Correct 9 ms 30300 KB Output is correct
27 Correct 10 ms 30344 KB Output is correct
28 Correct 9 ms 30296 KB Output is correct
29 Correct 10 ms 30368 KB Output is correct
30 Correct 10 ms 30300 KB Output is correct
31 Correct 10 ms 30460 KB Output is correct
32 Correct 10 ms 30556 KB Output is correct
33 Correct 10 ms 30556 KB Output is correct
34 Correct 9 ms 30296 KB Output is correct
35 Correct 9 ms 30300 KB Output is correct
36 Correct 10 ms 30296 KB Output is correct
37 Correct 10 ms 30296 KB Output is correct
38 Correct 10 ms 30368 KB Output is correct
39 Correct 137 ms 42952 KB Output is correct
40 Correct 129 ms 41976 KB Output is correct
41 Correct 125 ms 42948 KB Output is correct
42 Correct 135 ms 43208 KB Output is correct
43 Correct 135 ms 43064 KB Output is correct
44 Correct 133 ms 43208 KB Output is correct
45 Correct 189 ms 50360 KB Output is correct
46 Correct 195 ms 55908 KB Output is correct
47 Correct 114 ms 43364 KB Output is correct
48 Correct 109 ms 43324 KB Output is correct
49 Correct 143 ms 43204 KB Output is correct
50 Correct 119 ms 43324 KB Output is correct
51 Correct 127 ms 53748 KB Output is correct