Submission #883275

# Submission time Handle Problem Language Result Execution time Memory
883275 2023-12-05T03:15:28 Z marvinthang Capital City (JOI20_capital_city) C++17
100 / 100
456 ms 42128 KB
/*************************************
*    author: marvinthang             *
*    created: 05.12.2023 09:58:14    *
*************************************/

#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-- > 0; )
#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 << '}'; }

// end of template

void process(void) {
	int n, k; cin >> n >> k;
	vector <vector <int>> adj(n);
	REP(i, n - 1) {
		int u, v; cin >> u >> v; --u; --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	vector <int> c(n);
	vector <vector <int>> list_idx(k);
	REP(i, n) {
		cin >> c[i];
		list_idx[--c[i]].push_back(i);
	}
	vector <bool> used(n), exist(k);
	vector <int> sz(n), par(n), cnt(k);
	function<void(int, int)> dfs_sz = [&] (int u, int par) {
		sz[u] = 1;
		exist[c[u]] = false;
		cnt[c[u]] = 0;
		for (int v: adj[u]) if (!used[v] && v != par) {
			dfs_sz(v, u);
			sz[u] += sz[v];
		}
	};
	function<int(int, int, int)> find = [&] (int u, int par, int n) {
		for (int v: adj[u]) if (!used[v] && v != par && sz[v] >= n / 2) return find(v, u, n);
		return u;
	};
	int res = k - 1;
	function<void(int)> solve = [&] (int u) {
		dfs_sz(u, -1);
		u = find(u, -1, sz[u]);
		par[u] = -1;
		function<void(int)> dfs = [&] (int u) {
			++cnt[c[u]];
			for (int v: adj[u]) if (!used[v] && v != par[u]) {
				par[v] = u;
				dfs(v);
			}
		};
		dfs(u);
		queue <int> q;
		q.push(c[u]);
		int cc = 0;
		exist[c[u]] = true;
		while (!q.empty()) {
			int f = q.front(); q.pop();
			if (cnt[f] != (int) list_idx[f].size()) {
				cc = k;
				break;
			}
			++cc;
			for (int u: list_idx[f]) {
				u = par[u];
				if (u >= 0 && !exist[c[u]]) {
					exist[c[u]] = true;
					q.push(c[u]);
				}
			}
		}
		res = min(res, cc - 1);
		used[u] = true;
		for (int v: adj[u]) if (!used[v]) solve(v);
	};
	solve(0);
	cout << res << '\n';
}

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

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.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); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:116:2: note: in expansion of macro 'file'
  116 |  file("JOI20_capital_city");
      |  ^~~~
capital_city.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); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:116:2: note: in expansion of macro 'file'
  116 |  file("JOI20_capital_city");
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 600 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 41632 KB Output is correct
2 Correct 181 ms 42128 KB Output is correct
3 Correct 341 ms 41044 KB Output is correct
4 Correct 175 ms 42128 KB Output is correct
5 Correct 377 ms 37400 KB Output is correct
6 Correct 184 ms 42068 KB Output is correct
7 Correct 353 ms 37408 KB Output is correct
8 Correct 178 ms 41168 KB Output is correct
9 Correct 448 ms 34708 KB Output is correct
10 Correct 392 ms 31760 KB Output is correct
11 Correct 422 ms 35276 KB Output is correct
12 Correct 409 ms 38224 KB Output is correct
13 Correct 422 ms 31052 KB Output is correct
14 Correct 398 ms 38740 KB Output is correct
15 Correct 420 ms 38224 KB Output is correct
16 Correct 456 ms 32208 KB Output is correct
17 Correct 403 ms 33016 KB Output is correct
18 Correct 392 ms 33620 KB Output is correct
19 Correct 422 ms 37032 KB Output is correct
20 Correct 410 ms 39652 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 600 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 532 KB Output is correct
31 Correct 355 ms 41632 KB Output is correct
32 Correct 181 ms 42128 KB Output is correct
33 Correct 341 ms 41044 KB Output is correct
34 Correct 175 ms 42128 KB Output is correct
35 Correct 377 ms 37400 KB Output is correct
36 Correct 184 ms 42068 KB Output is correct
37 Correct 353 ms 37408 KB Output is correct
38 Correct 178 ms 41168 KB Output is correct
39 Correct 448 ms 34708 KB Output is correct
40 Correct 392 ms 31760 KB Output is correct
41 Correct 422 ms 35276 KB Output is correct
42 Correct 409 ms 38224 KB Output is correct
43 Correct 422 ms 31052 KB Output is correct
44 Correct 398 ms 38740 KB Output is correct
45 Correct 420 ms 38224 KB Output is correct
46 Correct 456 ms 32208 KB Output is correct
47 Correct 403 ms 33016 KB Output is correct
48 Correct 392 ms 33620 KB Output is correct
49 Correct 422 ms 37032 KB Output is correct
50 Correct 410 ms 39652 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Correct 227 ms 19516 KB Output is correct
53 Correct 237 ms 19536 KB Output is correct
54 Correct 298 ms 19520 KB Output is correct
55 Correct 297 ms 19592 KB Output is correct
56 Correct 288 ms 19572 KB Output is correct
57 Correct 295 ms 19576 KB Output is correct
58 Correct 278 ms 25392 KB Output is correct
59 Correct 280 ms 25668 KB Output is correct
60 Correct 362 ms 24912 KB Output is correct
61 Correct 323 ms 24496 KB Output is correct
62 Correct 179 ms 42068 KB Output is correct
63 Correct 181 ms 42068 KB Output is correct
64 Correct 184 ms 41436 KB Output is correct
65 Correct 184 ms 42096 KB Output is correct
66 Correct 249 ms 30460 KB Output is correct
67 Correct 212 ms 30352 KB Output is correct
68 Correct 229 ms 30944 KB Output is correct
69 Correct 259 ms 30680 KB Output is correct
70 Correct 235 ms 30456 KB Output is correct
71 Correct 249 ms 30568 KB Output is correct
72 Correct 219 ms 30408 KB Output is correct
73 Correct 219 ms 29584 KB Output is correct
74 Correct 237 ms 30664 KB Output is correct
75 Correct 232 ms 30632 KB Output is correct
76 Correct 354 ms 30156 KB Output is correct
77 Correct 335 ms 27924 KB Output is correct
78 Correct 438 ms 32772 KB Output is correct
79 Correct 440 ms 30564 KB Output is correct
80 Correct 444 ms 39184 KB Output is correct
81 Correct 443 ms 34900 KB Output is correct
82 Correct 419 ms 35068 KB Output is correct
83 Correct 435 ms 30896 KB Output is correct
84 Correct 429 ms 38088 KB Output is correct
85 Correct 421 ms 36248 KB Output is correct
86 Correct 438 ms 30568 KB Output is correct
87 Correct 421 ms 32592 KB Output is correct
88 Correct 375 ms 33864 KB Output is correct
89 Correct 356 ms 29140 KB Output is correct
90 Correct 379 ms 28948 KB Output is correct
91 Correct 353 ms 31824 KB Output is correct
92 Correct 376 ms 30232 KB Output is correct
93 Correct 405 ms 29848 KB Output is correct
94 Correct 356 ms 29068 KB Output is correct
95 Correct 366 ms 30888 KB Output is correct
96 Correct 362 ms 29268 KB Output is correct
97 Correct 357 ms 31572 KB Output is correct