답안 #595715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
595715 2022-07-14T02:50:56 Z penguinhacker Meetings 2 (JOI21_meetings2) C++17
20 / 100
4000 ms 29952 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=2e5;
int n, d[mxN], s[mxN], anc[mxN][18], ans[mxN+1], tin[mxN], t;
vector<int> adj[mxN];
set<ar<int, 2>> active;

void dfs(int u=0) {
	s[u]=1;
	tin[u]=t++;
	for (int i=1; i<18; ++i)
		anc[u][i]=anc[anc[u][i-1]][i-1];
	for (int v : adj[u])
		if (v!=anc[u][0]) {
			d[v]=d[u]+1, anc[v][0]=u;
			dfs(v);
			s[u]+=s[v];
		}
}

int lca(int u, int v) {
	if (d[u]>d[v])
		swap(u, v);
	for (int i=17; ~i; --i)
		if (d[v]-(1<<i)>=d[u])
			v=anc[v][i];
	if (u==v)
		return u;
	for (int i=17; ~i; --i)
		if (anc[u][i]!=anc[v][i])
			u=anc[u][i], v=anc[v][i];
	return anc[u][0];
}

int dist(int u, int v) {
	return d[u]+d[v]-2*d[lca(u, v)];
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i=1; i<n; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs();
	vector<ar<int, 2>> upd;
	for (int i=1; i<n; ++i)
		upd.push_back({s[i], i});
	sort(upd.begin(), upd.end());
	fill(ans, ans+n+1, 1);
	int best=0;
	for (int i=n/2; i; --i) {
		/*while(upd.size()&&upd.back()[0]>=i) {
			int u=upd.back()[1];
			upd.pop_back();
			if (n-s[u]>=i) {
				int v=u;
				for (int j=17; ~j; --j)
					if (n-s[anc[v][j]]>=i)
						v=anc[v][j];
				best=max(best, d[u]-d[v]+2);
			}
			auto it=active.lower_bound({tin[u]});
			if (it!=active.begin()) {
				--it;
				int v=(*it)[1];
				if (tin[u]+s[u]<=tin[v]+s[v])
					active.erase(it);
			}
			for (ar<int, 2> x : active)
				best=max(best, dist(u, x[1])+1);
			active.insert({tin[u], u});
		}*/
		/*for (int j=0; j<n; ++j) {
			for (int k=j; k<n; ++k) {
				int u=j, v=k;
				if (tin[u]>tin[v])
					swap(u, v);
				if (tin[v]+s[v]<=tin[u]+s[u]) {
					if (s[v]>=i&&n-s[u]>=i)
						best=max(best, d[v]-d[u]+2);
				} else {
					if (s[u]>=i&&s[v]>=i)
						best=max(best, dist(u, v)+1);
				}
			}
		}
		ans[2*i]=best;*/
	}
	for (int i=0; i<n; ++i)
		for (int j=0; j<n; ++j) {
			if (i==j)
				continue;
			int ij=lca(i, j);
			if (ij==j)
				continue;
			int x=min(s[i], s[j]);
			if (ij==i) { // i is ancestor of j
				int u=j;
				for (int k=17; ~k; --k)
					if (d[anc[u][k]]>d[i])
						u=anc[u][k];
				x=min(s[j], n-s[u]);
			}
			ans[2*x]=max(ans[2*x], dist(i, j)+1);
		}
	for (int i=n/2-1; i>0; --i)
		ans[2*i]=max(ans[2*i], ans[2*i+2]);
	//cout << active.size() << endl;
	for (int i=1; i<=n; ++i)
		cout << ans[i] << "\n";
	return 0;
}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:59:6: warning: unused variable 'best' [-Wunused-variable]
   59 |  int best=0;
      |      ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5040 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5040 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 1301 ms 5588 KB Output is correct
23 Correct 1279 ms 5588 KB Output is correct
24 Correct 1282 ms 5568 KB Output is correct
25 Correct 1261 ms 5568 KB Output is correct
26 Correct 1276 ms 5564 KB Output is correct
27 Correct 1255 ms 5716 KB Output is correct
28 Correct 1282 ms 5588 KB Output is correct
29 Correct 1253 ms 5568 KB Output is correct
30 Correct 1290 ms 5588 KB Output is correct
31 Correct 1264 ms 5588 KB Output is correct
32 Correct 2237 ms 5672 KB Output is correct
33 Correct 2428 ms 5764 KB Output is correct
34 Correct 1022 ms 5588 KB Output is correct
35 Correct 959 ms 5588 KB Output is correct
36 Correct 1716 ms 5588 KB Output is correct
37 Correct 1067 ms 5588 KB Output is correct
38 Correct 1588 ms 5656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5040 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 1301 ms 5588 KB Output is correct
23 Correct 1279 ms 5588 KB Output is correct
24 Correct 1282 ms 5568 KB Output is correct
25 Correct 1261 ms 5568 KB Output is correct
26 Correct 1276 ms 5564 KB Output is correct
27 Correct 1255 ms 5716 KB Output is correct
28 Correct 1282 ms 5588 KB Output is correct
29 Correct 1253 ms 5568 KB Output is correct
30 Correct 1290 ms 5588 KB Output is correct
31 Correct 1264 ms 5588 KB Output is correct
32 Correct 2237 ms 5672 KB Output is correct
33 Correct 2428 ms 5764 KB Output is correct
34 Correct 1022 ms 5588 KB Output is correct
35 Correct 959 ms 5588 KB Output is correct
36 Correct 1716 ms 5588 KB Output is correct
37 Correct 1067 ms 5588 KB Output is correct
38 Correct 1588 ms 5656 KB Output is correct
39 Execution timed out 4078 ms 29952 KB Time limit exceeded
40 Halted 0 ms 0 KB -