답안 #776977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776977 2023-07-08T12:54:34 Z marvinthang Meetings 2 (JOI21_meetings2) C++17
100 / 100
302 ms 26700 KB
/*************************************
*    author: marvinthang             *
*    created: 08.07.2023 19:22:52    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

// end of template

const int MAX = 2e5 + 5;

int N;
vector <int> adj[MAX];

void init(void) {
	cin >> N;
	FOR(i, 1, N) {
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
}

int res[MAX], treeSize[MAX];
bool used[MAX];

void calcSize(int u, int par) {
	treeSize[u] = 1;
	for (int v: adj[u]) if (!used[v] && v != par) {
		calcSize(v, u);
		treeSize[u] += treeSize[v];
	}
}

int findCentroid(int u, int par, int n) {
	for (int v: adj[u]) if (!used[v] && v != par && treeSize[v] > n / 2) {
		treeSize[u] = n - treeSize[v];
		return findCentroid(v, u, n);
	}
	return u;
}

int mx[MAX];
pair <int, int> best[MAX];

void dfs(int u, int par, int d) {
	maximize(mx[treeSize[u]], d);
	for (int v: adj[u]) if (!used[v] && v != par) dfs(v, u, d + 1);
}

void solve(int u) {
	calcSize(u, 0);
	int n = treeSize[u];
	u = findCentroid(u, 0, n);
	REP(i, n) best[i] = make_pair(-N, -N);
	for (int v: adj[u]) if (!used[v]) {
		dfs(v, u, 1);
		REPD(i, treeSize[v]) maximize(mx[i], mx[i + 1]);
		REP(i, treeSize[v] + 1) {
			if (best[i].fi < mx[i]) {
				best[i].se = best[i].fi;
				best[i].fi = mx[i];
			} else maximize(best[i].se, mx[i]);
			if (i <= n - treeSize[v]) maximize(res[i], mx[i]);
			mx[i] = 0;
		}
	}
	REP(i, n) maximize(res[i], best[i].fi + best[i].se);
	used[u] = true;
	for (int v: adj[u]) if (!used[v]) solve(v);
}

void process(void) {
	solve(1);
	FORE(i, 1, N) cout << (i & 1 ? 1 : res[i / 2] + 1) << '\n';
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("meetings2");
	init();
	process();
	cerr << "Time elapsed: " << TIME << " s.\n";
	return (0^0);
}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
meetings2.cpp:117:2: note: in expansion of macro 'file'
  117 |  file("meetings2");
      |  ^~~~
meetings2.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
meetings2.cpp:117:2: note: in expansion of macro 'file'
  117 |  file("meetings2");
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 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 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 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 2 ms 4948 KB Output is correct
17 Correct 2 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 2 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 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 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 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 2 ms 4948 KB Output is correct
17 Correct 2 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 2 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 4 ms 5204 KB Output is correct
23 Correct 5 ms 5288 KB Output is correct
24 Correct 6 ms 5204 KB Output is correct
25 Correct 4 ms 5204 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 4 ms 5248 KB Output is correct
28 Correct 4 ms 5304 KB Output is correct
29 Correct 5 ms 5180 KB Output is correct
30 Correct 4 ms 5304 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 7 ms 5332 KB Output is correct
33 Correct 5 ms 5460 KB Output is correct
34 Correct 3 ms 5176 KB Output is correct
35 Correct 3 ms 5204 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5204 KB Output is correct
38 Correct 4 ms 5372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 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 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 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 2 ms 4948 KB Output is correct
17 Correct 2 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 2 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 4 ms 5204 KB Output is correct
23 Correct 5 ms 5288 KB Output is correct
24 Correct 6 ms 5204 KB Output is correct
25 Correct 4 ms 5204 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 4 ms 5248 KB Output is correct
28 Correct 4 ms 5304 KB Output is correct
29 Correct 5 ms 5180 KB Output is correct
30 Correct 4 ms 5304 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 7 ms 5332 KB Output is correct
33 Correct 5 ms 5460 KB Output is correct
34 Correct 3 ms 5176 KB Output is correct
35 Correct 3 ms 5204 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5204 KB Output is correct
38 Correct 4 ms 5372 KB Output is correct
39 Correct 170 ms 17372 KB Output is correct
40 Correct 213 ms 17084 KB Output is correct
41 Correct 205 ms 17436 KB Output is correct
42 Correct 239 ms 17628 KB Output is correct
43 Correct 234 ms 17564 KB Output is correct
44 Correct 189 ms 17552 KB Output is correct
45 Correct 302 ms 22596 KB Output is correct
46 Correct 227 ms 26700 KB Output is correct
47 Correct 108 ms 17260 KB Output is correct
48 Correct 64 ms 17460 KB Output is correct
49 Correct 148 ms 18344 KB Output is correct
50 Correct 72 ms 18244 KB Output is correct
51 Correct 155 ms 25588 KB Output is correct