Submission #408459

# Submission time Handle Problem Language Result Execution time Memory
408459 2021-05-19T07:33:19 Z ryansee Meetings 2 (JOI21_meetings2) C++14
100 / 100
502 ms 44612 KB
#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; 

long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (200006)
int n, p[MAXN][18], dist[MAXN], st[MAXN], en[MAXN], co, sz[MAXN];
vector<int> v[MAXN];
void dfs(int x,int par) {
	st[x] = co++, p[x][0]=par, sz[x] = 1;
	for(auto i:v[x]) if(i^par) dist[i]=dist[x]+1, dfs(i, x), sz[x] += sz[i];
	en[x] = co-1;
}
bool isp(int a,int b) { return st[a]<=st[b]&&en[a]>=en[b]; }
int lca(int x,int y) {
	if(isp(x,y)) return x;
	if(isp(y,x)) return y;
	DEC(i,17,0) if(!isp(p[x][i], y)) x=p[x][i];
	return p[x][0];
}
int disty(int a,int b) { return dist[a]+dist[b]-2*dist[lca(a,b)]; }
int A, B, out[MAXN], c;
void SZ(int x,int p) {
	sz[x] = 1;
	for(auto i:v[x]) if(i^p) SZ(i, x), sz[x] += sz[i];
}
int dfs2(int x,int p) {
	for(auto i:v[x]) if(i^p && sz[i] > n/2) return dfs2(i, x);
	return x;
}
int main() {
	FAST
	cin>>n;
	FOR(i,1,n-1) {
		int a, b;
		cin>>a>>b;
		v[a].eb(b), v[b].eb(a);
	}
	SZ(1, 1);
	c = dfs2(1, 1);
	dfs(c, c);
	FOR(j,1,17) FOR(i,1,n) p[i][j]=p[p[i][j-1]][j-1];
	A=B=-1;
	vector<pi> V;
	int mx = 0;
	for(auto j:v[c]) mx = max(mx, sz[j]);
	sz[c] -= mx;
	FOR(i,1,n) {
		// cerr<<i<<": "<<sz[i]<<'\n';
		V.eb(sz[i], i);
	}
	sort(all(V));
	for(ll i=n/2;i>=1;--i) {
		while(V.size() && V.back().f >= i) {
			int x = V.back().s; V.pop_back();
			if(A==-1) A=B=x;
			else {
				if(disty(x, A) >= disty(A, B) && disty(x, A) >= disty(x, B)) {
					B = x;
				} else if(disty(x, B) >= disty(A, B)) {
					A = x;
				}
			}
		}
		if(A == -1) out[i*2]=0;
		else out[i*2] = disty(A, B);
	}
	FOR(i,1,n) cout<<out[i]+1<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 5 ms 4940 KB Output is correct
10 Correct 4 ms 4960 KB Output is correct
11 Correct 5 ms 4940 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 5 ms 5164 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 5 ms 4940 KB Output is correct
16 Correct 4 ms 5060 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 5 ms 4960 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 5 ms 5024 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 5 ms 4940 KB Output is correct
10 Correct 4 ms 4960 KB Output is correct
11 Correct 5 ms 4940 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 5 ms 5164 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 5 ms 4940 KB Output is correct
16 Correct 4 ms 5060 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 5 ms 4960 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 5 ms 5024 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
22 Correct 10 ms 5640 KB Output is correct
23 Correct 9 ms 5692 KB Output is correct
24 Correct 10 ms 5728 KB Output is correct
25 Correct 12 ms 5728 KB Output is correct
26 Correct 8 ms 5708 KB Output is correct
27 Correct 9 ms 5708 KB Output is correct
28 Correct 9 ms 5736 KB Output is correct
29 Correct 10 ms 5716 KB Output is correct
30 Correct 10 ms 5612 KB Output is correct
31 Correct 12 ms 5708 KB Output is correct
32 Correct 11 ms 5836 KB Output is correct
33 Correct 8 ms 5816 KB Output is correct
34 Correct 9 ms 5688 KB Output is correct
35 Correct 7 ms 5708 KB Output is correct
36 Correct 10 ms 5668 KB Output is correct
37 Correct 7 ms 5648 KB Output is correct
38 Correct 7 ms 5812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 5 ms 4940 KB Output is correct
10 Correct 4 ms 4960 KB Output is correct
11 Correct 5 ms 4940 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 5 ms 5164 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 5 ms 4940 KB Output is correct
16 Correct 4 ms 5060 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 5 ms 4960 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 5 ms 5024 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
22 Correct 10 ms 5640 KB Output is correct
23 Correct 9 ms 5692 KB Output is correct
24 Correct 10 ms 5728 KB Output is correct
25 Correct 12 ms 5728 KB Output is correct
26 Correct 8 ms 5708 KB Output is correct
27 Correct 9 ms 5708 KB Output is correct
28 Correct 9 ms 5736 KB Output is correct
29 Correct 10 ms 5716 KB Output is correct
30 Correct 10 ms 5612 KB Output is correct
31 Correct 12 ms 5708 KB Output is correct
32 Correct 11 ms 5836 KB Output is correct
33 Correct 8 ms 5816 KB Output is correct
34 Correct 9 ms 5688 KB Output is correct
35 Correct 7 ms 5708 KB Output is correct
36 Correct 10 ms 5668 KB Output is correct
37 Correct 7 ms 5648 KB Output is correct
38 Correct 7 ms 5812 KB Output is correct
39 Correct 365 ms 35116 KB Output is correct
40 Correct 373 ms 34496 KB Output is correct
41 Correct 375 ms 35176 KB Output is correct
42 Correct 392 ms 35668 KB Output is correct
43 Correct 367 ms 35620 KB Output is correct
44 Correct 387 ms 35688 KB Output is correct
45 Correct 502 ms 40480 KB Output is correct
46 Correct 462 ms 44612 KB Output is correct
47 Correct 384 ms 36040 KB Output is correct
48 Correct 261 ms 36172 KB Output is correct
49 Correct 403 ms 36260 KB Output is correct
50 Correct 294 ms 36192 KB Output is correct
51 Correct 339 ms 43600 KB Output is correct