답안 #627166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
627166 2022-08-12T10:08:31 Z MohamedFaresNebili 수도 (JOI20_capital_city) C++14
11 / 100
3000 ms 38324 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;
                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) return dfs_centroid(u, v, _N);
                }
                return v;
            }
            void dfs(int v, int p) {
                par[v] = p; cur[C[v]]++; used[C[v]] = 0;
                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[v]) {
                    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:51:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |                     if(cur[u] != S[u].size()) { ans = 1e9 + 7; break; }
      |                        ~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9720 KB Output is correct
3 Correct 6 ms 9720 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9644 KB Output is correct
6 Correct 7 ms 9660 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9720 KB Output is correct
9 Correct 8 ms 9684 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9720 KB Output is correct
3 Correct 6 ms 9720 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9644 KB Output is correct
6 Correct 7 ms 9660 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9720 KB Output is correct
9 Correct 8 ms 9684 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 7 ms 9800 KB Output is correct
14 Correct 8 ms 9812 KB Output is correct
15 Correct 22 ms 10284 KB Output is correct
16 Correct 15 ms 10092 KB Output is correct
17 Correct 27 ms 10464 KB Output is correct
18 Correct 26 ms 10632 KB Output is correct
19 Correct 29 ms 10552 KB Output is correct
20 Correct 40 ms 10620 KB Output is correct
21 Correct 28 ms 10592 KB Output is correct
22 Correct 66 ms 11404 KB Output is correct
23 Correct 36 ms 10648 KB Output is correct
24 Correct 20 ms 10244 KB Output is correct
25 Correct 44 ms 10840 KB Output is correct
26 Correct 63 ms 11152 KB Output is correct
27 Correct 47 ms 10904 KB Output is correct
28 Correct 40 ms 10712 KB Output is correct
29 Correct 34 ms 10544 KB Output is correct
30 Correct 36 ms 10624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3052 ms 38324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9720 KB Output is correct
3 Correct 6 ms 9720 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9644 KB Output is correct
6 Correct 7 ms 9660 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9720 KB Output is correct
9 Correct 8 ms 9684 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 7 ms 9800 KB Output is correct
14 Correct 8 ms 9812 KB Output is correct
15 Correct 22 ms 10284 KB Output is correct
16 Correct 15 ms 10092 KB Output is correct
17 Correct 27 ms 10464 KB Output is correct
18 Correct 26 ms 10632 KB Output is correct
19 Correct 29 ms 10552 KB Output is correct
20 Correct 40 ms 10620 KB Output is correct
21 Correct 28 ms 10592 KB Output is correct
22 Correct 66 ms 11404 KB Output is correct
23 Correct 36 ms 10648 KB Output is correct
24 Correct 20 ms 10244 KB Output is correct
25 Correct 44 ms 10840 KB Output is correct
26 Correct 63 ms 11152 KB Output is correct
27 Correct 47 ms 10904 KB Output is correct
28 Correct 40 ms 10712 KB Output is correct
29 Correct 34 ms 10544 KB Output is correct
30 Correct 36 ms 10624 KB Output is correct
31 Execution timed out 3052 ms 38324 KB Time limit exceeded
32 Halted 0 ms 0 KB -