답안 #496234

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
496234 2021-12-21T03:38:40 Z hollwo_pelw Meetings 2 (JOI21_meetings2) C++17
100 / 100
234 ms 57332 KB
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(filein.c_str(), "r", stdin);
		freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}

void Hollwo_Pelw();

signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}

const int N = 2e5 + 5, L = 20;

vector<int> adj[N];

struct calc_dist {
	template<class T, const T& merge(const T&, const T&)> struct rmq {
		vector<T> st[L];
		rmq () {}
		rmq (const vector<T> &a) {
			st[0] = a; int n = a.size(); __build__(n);
		}
		rmq (T * __first, T * __last) {
			st[0] = vector<T> (__first, __last);
			int n = __last - __first; __build__(n);
		}
 
		inline void __build__(int n) {
			for (int j = 1; 1 << j <= n; j++) {
				st[j].resize(n - (1 << j) + 1);
				for (int i = 0; i + (1 << j) <= n; i++)
					st[j][i] = merge(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
			}
		}
 
		inline T query(int l, int r) {
			int j = __lg(r - l + 1);
			// return merge(st[j][l], st[j][r - (1 << j) + 1]); // 0 base
			return merge(st[j][l - 1], st[j][r - (1 << j)]); // 1 base
		}
	};
 
	rmq<int, min<int>> rmq_dist;
 
	int d[N], val[N << 1], tin[N], timer;
 
	void pre_dfs(int u, int p) {
		val[tin[u] = ++ timer] = d[u];
		for (int v : adj[u]) if (v != p) {
			d[v] = d[u] + 1;
			pre_dfs(v, u);
			val[++ timer] = d[u];
		}
	}
 
	inline void __init__() {
		pre_dfs(1, 0);
		rmq_dist = rmq<int, min<int>> (val + 1, val + timer + 1);
	}
 
	inline int operator () (int u, int v) {
		int l = tin[u], r = tin[v]; if (l > r) swap(l, r);
		return d[u] + d[v] - 2 * rmq_dist.query(l, r);
	}
} dist;

int n, res[N], sz[N], vis[N], deg[N];
vector<int> leaf[N];

inline int solve(int sz) {
	vector<int> lf;
	for (int i = sz; i <= n; i++)
		lf.insert(lf.end(), leaf[i].begin(), leaf[i].end());

	if (lf.empty()) return 1;

	int d1 = lf[0], d2 = lf[0];

	for (auto v : lf) {
		if (dist(d1, v) > dist(d1, d2))
			d2 = v;
	}

	for (auto v : lf) {
		if (dist(d2, v) > dist(d1, d2))
			d1 = v;
	}

	return dist(d1, d2) + 1;
}

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

	dist.__init__();

	fill(sz + 1, sz + n + 1, 1);
	for (int i = 1; i <= n; i++) {
		if (adj[i].size() == 1 + deg[i])
			vis[i] = 1, leaf[1].push_back(i);
	}

	for (int i = 1; i <= n; i++) for (auto u : leaf[i]) {
		for (auto v : adj[u]) if (!vis[v]) {
			sz[v] += sz[u];
			++ deg[v];

			if (adj[v].size() == 1 + deg[v])
				vis[v] = 1, leaf[sz[v]].push_back(v);
		}
	}

	fill(res + 1, res + n + 1, 1);

	int d1 = -1, d2 = -1;

	for (int i = n; i >= 1; i--) for (auto v : leaf[i]) {
		if (d1 == -1) d1 = v;
		if (d2 == -1) d2 = v;

		if (dist(v, d2) > dist(d1, d2)) d1 = v;
		if (dist(d1, v) > dist(d1, d2)) d2 = v;

		int j = min(sz[d1], sz[d2]) * 2;

		res[j] = max(res[j], dist(d1, d2) + 1);
	}

	for (int j = n - n % 2; j > 2; j -= 2)
		res[j - 2] = max(res[j - 2], res[j]);

	for (int i = 1; i <= n; i++)
		cout << res[i] << '\n';
}

Compilation message

meetings2.cpp: In function 'void Hollwo_Pelw()':
meetings2.cpp:138:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  138 |   if (adj[i].size() == 1 + deg[i])
      |       ~~~~~~~~~~~~~~^~~~~~~~~~~~~
meetings2.cpp:147:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  147 |    if (adj[v].size() == 1 + deg[v])
      |        ~~~~~~~~~~~~~~^~~~~~~~~~~~~
meetings2.cpp: In instantiation of 'void calc_dist::rmq<T, merge>::__build__(int) [with T = int; const T& (* merge)(const T&, const T&) = std::min<int>]':
meetings2.cpp:62:30:   required from 'calc_dist::rmq<T, merge>::rmq(T*, T*) [with T = int; const T& (* merge)(const T&, const T&) = std::min<int>]'
meetings2.cpp:95:58:   required from here
meetings2.cpp:69:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   69 |      st[j][i] = merge(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
      |                                                         ~~^~~
meetings2.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
meetings2.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
meetings2.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 6 ms 9752 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 7 ms 9668 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 7 ms 9676 KB Output is correct
17 Correct 7 ms 9732 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 5 ms 9648 KB Output is correct
20 Correct 5 ms 9676 KB Output is correct
21 Correct 8 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 6 ms 9752 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 7 ms 9668 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 7 ms 9676 KB Output is correct
17 Correct 7 ms 9732 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 5 ms 9648 KB Output is correct
20 Correct 5 ms 9676 KB Output is correct
21 Correct 8 ms 9676 KB Output is correct
22 Correct 6 ms 10316 KB Output is correct
23 Correct 7 ms 10416 KB Output is correct
24 Correct 7 ms 10384 KB Output is correct
25 Correct 6 ms 10316 KB Output is correct
26 Correct 11 ms 10368 KB Output is correct
27 Correct 8 ms 10316 KB Output is correct
28 Correct 7 ms 10316 KB Output is correct
29 Correct 9 ms 10316 KB Output is correct
30 Correct 10 ms 10448 KB Output is correct
31 Correct 7 ms 10316 KB Output is correct
32 Correct 6 ms 10444 KB Output is correct
33 Correct 7 ms 10420 KB Output is correct
34 Correct 7 ms 10316 KB Output is correct
35 Correct 7 ms 10432 KB Output is correct
36 Correct 6 ms 10316 KB Output is correct
37 Correct 7 ms 10444 KB Output is correct
38 Correct 9 ms 10444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 6 ms 9752 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 7 ms 9668 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 7 ms 9676 KB Output is correct
17 Correct 7 ms 9732 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 5 ms 9648 KB Output is correct
20 Correct 5 ms 9676 KB Output is correct
21 Correct 8 ms 9676 KB Output is correct
22 Correct 6 ms 10316 KB Output is correct
23 Correct 7 ms 10416 KB Output is correct
24 Correct 7 ms 10384 KB Output is correct
25 Correct 6 ms 10316 KB Output is correct
26 Correct 11 ms 10368 KB Output is correct
27 Correct 8 ms 10316 KB Output is correct
28 Correct 7 ms 10316 KB Output is correct
29 Correct 9 ms 10316 KB Output is correct
30 Correct 10 ms 10448 KB Output is correct
31 Correct 7 ms 10316 KB Output is correct
32 Correct 6 ms 10444 KB Output is correct
33 Correct 7 ms 10420 KB Output is correct
34 Correct 7 ms 10316 KB Output is correct
35 Correct 7 ms 10432 KB Output is correct
36 Correct 6 ms 10316 KB Output is correct
37 Correct 7 ms 10444 KB Output is correct
38 Correct 9 ms 10444 KB Output is correct
39 Correct 215 ms 50700 KB Output is correct
40 Correct 162 ms 49732 KB Output is correct
41 Correct 210 ms 50728 KB Output is correct
42 Correct 213 ms 51436 KB Output is correct
43 Correct 192 ms 51468 KB Output is correct
44 Correct 234 ms 51420 KB Output is correct
45 Correct 194 ms 55396 KB Output is correct
46 Correct 172 ms 57332 KB Output is correct
47 Correct 180 ms 51828 KB Output is correct
48 Correct 179 ms 51280 KB Output is correct
49 Correct 161 ms 52128 KB Output is correct
50 Correct 193 ms 51768 KB Output is correct
51 Correct 187 ms 55924 KB Output is correct