Submission #627171

# Submission time Handle Problem Language Result Execution time Memory
627171 2022-08-12T10:13:36 Z MohamedFaresNebili Capital City (JOI20_capital_city) C++14
100 / 100
553 ms 38332 KB
#include <bits/stdc++.h>

            using namespace std;

            using ll = long long;
            using ld = long double;
            using pi = pair<pair<int, int>, pair<int, int>>;

            #define ff first
            #define ss second
            #define pb push_back
            #define all(x) (x).begin(), (x).end()
            #define lb lower_bound

            int N, K, res = 1e9 + 7, C[200005];
            int cur[200005], par[200005], sz[200005];
            bool used[200005], seen[200005];
            vector<int> adj[200005], S[200005];

            int dfs_size(int v, int p) {
                sz[v] = 1; cur[C[v]] = 0; used[C[v]] = 0;
                for(auto u : adj[v]) {
                    if(u == p || seen[u]) continue;
                    sz[v] += dfs_size(u, v);
                }
                return sz[v];
            }
            int dfs_centroid(int v, int p, int _N) {
                for(auto u : adj[v]) {
                    if(u == p || seen[u]) continue;
                    if(sz[u] >= _N / 2) 
                        return dfs_centroid(u, v, _N);
                }
                return v;
            }
            void dfs(int v, int p) {
                par[v] = p; cur[C[v]]++; 
                for(auto u : adj[v]) {
                    if(u == p || seen[u]) continue;
                    dfs(u, v);
                }
            }
            void build(int v) {
                int _N = dfs_size(v, v);
                int centroid = dfs_centroid(v, v, _N);
                dfs(centroid, centroid); seen[centroid] = 1;
                int ans = 0;
                stack<int> st; st.push(C[centroid]);
                while(!st.empty()) {
                    int u = st.top(); st.pop();
                    if(used[u]) continue;
                    if(cur[u] != S[u].size()) { ans = 1e9 + 7; break; }
                    used[u] = 1; ans++;
                    for(auto e : S[u]) st.push(C[par[e]]);
                }
                res = min(res, ans - 1);
                for(auto u : adj[centroid]) {
                    if(seen[u]) continue;
                    build(u);
                }
            }

            int32_t main() {
                ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
                cin >> N >> K;
                for(int l = 0; l < N - 1; l++) {
                    int A, B; cin >> A >> B;
                    adj[A].push_back(B);
                    adj[B].push_back(A);
                }
                for(int l = 1; l <= N; l++)
                    cin >> C[l], S[C[l]].push_back(l);
                build(1); cout << res << "\n";
                return 0;
            }

Compilation message

capital_city.cpp: In function 'void build(int)':
capital_city.cpp:52:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |                     if(cur[u] != S[u].size()) { ans = 1e9 + 7; break; }
      |                        ~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 5 ms 9668 KB Output is correct
3 Correct 5 ms 9700 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9748 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9688 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 5 ms 9668 KB Output is correct
3 Correct 5 ms 9700 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9748 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9688 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9740 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 8 ms 9812 KB Output is correct
15 Correct 8 ms 9812 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 7 ms 9908 KB Output is correct
18 Correct 6 ms 9828 KB Output is correct
19 Correct 6 ms 9928 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 7 ms 9924 KB Output is correct
22 Correct 7 ms 9940 KB Output is correct
23 Correct 6 ms 9812 KB Output is correct
24 Correct 8 ms 9820 KB Output is correct
25 Correct 7 ms 9840 KB Output is correct
26 Correct 7 ms 9940 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 8 ms 9812 KB Output is correct
29 Correct 7 ms 9812 KB Output is correct
30 Correct 7 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 440 ms 34272 KB Output is correct
2 Correct 192 ms 34640 KB Output is correct
3 Correct 436 ms 34024 KB Output is correct
4 Correct 179 ms 34556 KB Output is correct
5 Correct 431 ms 31560 KB Output is correct
6 Correct 176 ms 38320 KB Output is correct
7 Correct 409 ms 35412 KB Output is correct
8 Correct 177 ms 37880 KB Output is correct
9 Correct 517 ms 33848 KB Output is correct
10 Correct 528 ms 31972 KB Output is correct
11 Correct 540 ms 34212 KB Output is correct
12 Correct 511 ms 36332 KB Output is correct
13 Correct 525 ms 31584 KB Output is correct
14 Correct 511 ms 36568 KB Output is correct
15 Correct 517 ms 36304 KB Output is correct
16 Correct 508 ms 32320 KB Output is correct
17 Correct 553 ms 32776 KB Output is correct
18 Correct 511 ms 33008 KB Output is correct
19 Correct 500 ms 35484 KB Output is correct
20 Correct 529 ms 37232 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 5 ms 9668 KB Output is correct
3 Correct 5 ms 9700 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9748 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9688 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9740 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 8 ms 9812 KB Output is correct
15 Correct 8 ms 9812 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 7 ms 9908 KB Output is correct
18 Correct 6 ms 9828 KB Output is correct
19 Correct 6 ms 9928 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 7 ms 9924 KB Output is correct
22 Correct 7 ms 9940 KB Output is correct
23 Correct 6 ms 9812 KB Output is correct
24 Correct 8 ms 9820 KB Output is correct
25 Correct 7 ms 9840 KB Output is correct
26 Correct 7 ms 9940 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 8 ms 9812 KB Output is correct
29 Correct 7 ms 9812 KB Output is correct
30 Correct 7 ms 9812 KB Output is correct
31 Correct 440 ms 34272 KB Output is correct
32 Correct 192 ms 34640 KB Output is correct
33 Correct 436 ms 34024 KB Output is correct
34 Correct 179 ms 34556 KB Output is correct
35 Correct 431 ms 31560 KB Output is correct
36 Correct 176 ms 38320 KB Output is correct
37 Correct 409 ms 35412 KB Output is correct
38 Correct 177 ms 37880 KB Output is correct
39 Correct 517 ms 33848 KB Output is correct
40 Correct 528 ms 31972 KB Output is correct
41 Correct 540 ms 34212 KB Output is correct
42 Correct 511 ms 36332 KB Output is correct
43 Correct 525 ms 31584 KB Output is correct
44 Correct 511 ms 36568 KB Output is correct
45 Correct 517 ms 36304 KB Output is correct
46 Correct 508 ms 32320 KB Output is correct
47 Correct 553 ms 32776 KB Output is correct
48 Correct 511 ms 33008 KB Output is correct
49 Correct 500 ms 35484 KB Output is correct
50 Correct 529 ms 37232 KB Output is correct
51 Correct 5 ms 9684 KB Output is correct
52 Correct 323 ms 24428 KB Output is correct
53 Correct 387 ms 24432 KB Output is correct
54 Correct 348 ms 24396 KB Output is correct
55 Correct 356 ms 24436 KB Output is correct
56 Correct 366 ms 24428 KB Output is correct
57 Correct 407 ms 24432 KB Output is correct
58 Correct 366 ms 27280 KB Output is correct
59 Correct 334 ms 27680 KB Output is correct
60 Correct 446 ms 27208 KB Output is correct
61 Correct 433 ms 26956 KB Output is correct
62 Correct 175 ms 38332 KB Output is correct
63 Correct 177 ms 38276 KB Output is correct
64 Correct 168 ms 37988 KB Output is correct
65 Correct 182 ms 38208 KB Output is correct
66 Correct 278 ms 31408 KB Output is correct
67 Correct 285 ms 31316 KB Output is correct
68 Correct 285 ms 31560 KB Output is correct
69 Correct 294 ms 31448 KB Output is correct
70 Correct 276 ms 31512 KB Output is correct
71 Correct 273 ms 31444 KB Output is correct
72 Correct 272 ms 31328 KB Output is correct
73 Correct 277 ms 30816 KB Output is correct
74 Correct 339 ms 31436 KB Output is correct
75 Correct 269 ms 31484 KB Output is correct
76 Correct 413 ms 30416 KB Output is correct
77 Correct 413 ms 28984 KB Output is correct
78 Correct 499 ms 32748 KB Output is correct
79 Correct 512 ms 31168 KB Output is correct
80 Correct 522 ms 36820 KB Output is correct
81 Correct 539 ms 34020 KB Output is correct
82 Correct 511 ms 34024 KB Output is correct
83 Correct 482 ms 31420 KB Output is correct
84 Correct 498 ms 36180 KB Output is correct
85 Correct 510 ms 34868 KB Output is correct
86 Correct 513 ms 31060 KB Output is correct
87 Correct 521 ms 32528 KB Output is correct
88 Correct 439 ms 33372 KB Output is correct
89 Correct 422 ms 30248 KB Output is correct
90 Correct 431 ms 30116 KB Output is correct
91 Correct 443 ms 31960 KB Output is correct
92 Correct 430 ms 30960 KB Output is correct
93 Correct 483 ms 30676 KB Output is correct
94 Correct 439 ms 30284 KB Output is correct
95 Correct 441 ms 31364 KB Output is correct
96 Correct 442 ms 30284 KB Output is correct
97 Correct 462 ms 31924 KB Output is correct