답안 #1031111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031111 2024-07-22T15:04:07 Z Blagoj 수도 (JOI20_capital_city) C++17
100 / 100
405 ms 42004 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()

const int mxn = 2e5 + 100;

int n, k;

vector<int> g[mxn], type(mxn), sz(mxn), p(mxn), cnt(mxn), have[mxn];
bool done[mxn], merged[mxn];

int getSize(int cur, int par = -1) {
    sz[cur] = 1;
    for (auto x : g[cur]) {
        if (x != par && !done[x]) sz[cur] += getSize(x, cur);
    }
    return sz[cur];
}

int getCentroid(int cur, int des, int par = -1) {
    for (auto x : g[cur]) {
        if (sz[x] >= des && x != par && !done[x]) return getCentroid(x, des, cur);
    }
    return cur;
}

void Clear(int cur, int par) {
    p[cur] = par;
    cnt[type[cur]] = merged[type[cur]] = 0;
    for (auto x : g[cur]) {
        if (x != par && !done[x]) Clear(x, cur);
    }
}

void get(int cur, int par) {
    cnt[type[cur]]++;
    for (auto x : g[cur]) {
        if (x != par && !done[x]) get(x, cur);
    }
}

ll ans = 1e12;

void decompose(int cur = 1) {
    cur = getCentroid(cur, getSize(cur) / 2);
    done[cur] = 1;
    Clear(cur, cur);
    get(cur, cur);
    stack<int> s;
    s.push(type[cur]);
    ll curAns = 0;
    while (s.size()) {
        int top = s.top();
        s.pop();
        if (merged[top]) continue;
        if (cnt[top] != have[top].size()) {
            curAns = 1e12;
            break;
        }
        curAns++;
        merged[top] = 1;
        for (auto x : have[top]) s.push(type[p[x]]);
    }
    ans = min(ans, curAns - 1);
    for (auto x : g[cur]) if (!done[x]) decompose(x);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int f, t;
        cin >> f >> t;
        g[f].push_back(t);
        g[t].push_back(f);
    }
    for (int i = 1; i <= n; i++) {
        cin >> type[i];
        have[type[i]].push_back(i);
    }
    decompose();
    cout << ans;
}

Compilation message

capital_city.cpp: In function 'void decompose(int)':
capital_city.cpp:60:22: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         if (cnt[top] != have[top].size()) {
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 13148 KB Output is correct
2 Correct 5 ms 12892 KB Output is correct
3 Correct 5 ms 12800 KB Output is correct
4 Correct 5 ms 13016 KB Output is correct
5 Correct 5 ms 12892 KB Output is correct
6 Correct 5 ms 13012 KB Output is correct
7 Correct 5 ms 12892 KB Output is correct
8 Correct 6 ms 12892 KB Output is correct
9 Correct 6 ms 12888 KB Output is correct
10 Correct 5 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 13148 KB Output is correct
2 Correct 5 ms 12892 KB Output is correct
3 Correct 5 ms 12800 KB Output is correct
4 Correct 5 ms 13016 KB Output is correct
5 Correct 5 ms 12892 KB Output is correct
6 Correct 5 ms 13012 KB Output is correct
7 Correct 5 ms 12892 KB Output is correct
8 Correct 6 ms 12892 KB Output is correct
9 Correct 6 ms 12888 KB Output is correct
10 Correct 5 ms 12892 KB Output is correct
11 Correct 6 ms 12892 KB Output is correct
12 Correct 8 ms 13136 KB Output is correct
13 Correct 7 ms 12888 KB Output is correct
14 Correct 6 ms 12892 KB Output is correct
15 Correct 6 ms 13148 KB Output is correct
16 Correct 6 ms 13144 KB Output is correct
17 Correct 6 ms 13144 KB Output is correct
18 Correct 6 ms 13148 KB Output is correct
19 Correct 6 ms 13148 KB Output is correct
20 Correct 6 ms 13196 KB Output is correct
21 Correct 6 ms 13184 KB Output is correct
22 Correct 6 ms 13148 KB Output is correct
23 Correct 6 ms 13148 KB Output is correct
24 Correct 6 ms 13100 KB Output is correct
25 Correct 7 ms 13152 KB Output is correct
26 Correct 7 ms 13148 KB Output is correct
27 Correct 7 ms 13032 KB Output is correct
28 Correct 7 ms 13148 KB Output is correct
29 Correct 7 ms 12976 KB Output is correct
30 Correct 6 ms 13032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 303 ms 41296 KB Output is correct
2 Correct 137 ms 41812 KB Output is correct
3 Correct 313 ms 40996 KB Output is correct
4 Correct 140 ms 41832 KB Output is correct
5 Correct 330 ms 37968 KB Output is correct
6 Correct 157 ms 41932 KB Output is correct
7 Correct 315 ms 38508 KB Output is correct
8 Correct 134 ms 41420 KB Output is correct
9 Correct 373 ms 36668 KB Output is correct
10 Correct 376 ms 34388 KB Output is correct
11 Correct 380 ms 37144 KB Output is correct
12 Correct 360 ms 39960 KB Output is correct
13 Correct 378 ms 33872 KB Output is correct
14 Correct 388 ms 40268 KB Output is correct
15 Correct 354 ms 39764 KB Output is correct
16 Correct 342 ms 34940 KB Output is correct
17 Correct 367 ms 35476 KB Output is correct
18 Correct 333 ms 35636 KB Output is correct
19 Correct 357 ms 38736 KB Output is correct
20 Correct 367 ms 41044 KB Output is correct
21 Correct 6 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 13148 KB Output is correct
2 Correct 5 ms 12892 KB Output is correct
3 Correct 5 ms 12800 KB Output is correct
4 Correct 5 ms 13016 KB Output is correct
5 Correct 5 ms 12892 KB Output is correct
6 Correct 5 ms 13012 KB Output is correct
7 Correct 5 ms 12892 KB Output is correct
8 Correct 6 ms 12892 KB Output is correct
9 Correct 6 ms 12888 KB Output is correct
10 Correct 5 ms 12892 KB Output is correct
11 Correct 6 ms 12892 KB Output is correct
12 Correct 8 ms 13136 KB Output is correct
13 Correct 7 ms 12888 KB Output is correct
14 Correct 6 ms 12892 KB Output is correct
15 Correct 6 ms 13148 KB Output is correct
16 Correct 6 ms 13144 KB Output is correct
17 Correct 6 ms 13144 KB Output is correct
18 Correct 6 ms 13148 KB Output is correct
19 Correct 6 ms 13148 KB Output is correct
20 Correct 6 ms 13196 KB Output is correct
21 Correct 6 ms 13184 KB Output is correct
22 Correct 6 ms 13148 KB Output is correct
23 Correct 6 ms 13148 KB Output is correct
24 Correct 6 ms 13100 KB Output is correct
25 Correct 7 ms 13152 KB Output is correct
26 Correct 7 ms 13148 KB Output is correct
27 Correct 7 ms 13032 KB Output is correct
28 Correct 7 ms 13148 KB Output is correct
29 Correct 7 ms 12976 KB Output is correct
30 Correct 6 ms 13032 KB Output is correct
31 Correct 303 ms 41296 KB Output is correct
32 Correct 137 ms 41812 KB Output is correct
33 Correct 313 ms 40996 KB Output is correct
34 Correct 140 ms 41832 KB Output is correct
35 Correct 330 ms 37968 KB Output is correct
36 Correct 157 ms 41932 KB Output is correct
37 Correct 315 ms 38508 KB Output is correct
38 Correct 134 ms 41420 KB Output is correct
39 Correct 373 ms 36668 KB Output is correct
40 Correct 376 ms 34388 KB Output is correct
41 Correct 380 ms 37144 KB Output is correct
42 Correct 360 ms 39960 KB Output is correct
43 Correct 378 ms 33872 KB Output is correct
44 Correct 388 ms 40268 KB Output is correct
45 Correct 354 ms 39764 KB Output is correct
46 Correct 342 ms 34940 KB Output is correct
47 Correct 367 ms 35476 KB Output is correct
48 Correct 333 ms 35636 KB Output is correct
49 Correct 357 ms 38736 KB Output is correct
50 Correct 367 ms 41044 KB Output is correct
51 Correct 6 ms 12892 KB Output is correct
52 Correct 222 ms 25168 KB Output is correct
53 Correct 261 ms 25172 KB Output is correct
54 Correct 233 ms 25164 KB Output is correct
55 Correct 227 ms 25076 KB Output is correct
56 Correct 228 ms 25112 KB Output is correct
57 Correct 223 ms 25224 KB Output is correct
58 Correct 230 ms 28500 KB Output is correct
59 Correct 241 ms 28876 KB Output is correct
60 Correct 284 ms 27988 KB Output is correct
61 Correct 268 ms 27544 KB Output is correct
62 Correct 147 ms 41812 KB Output is correct
63 Correct 148 ms 42004 KB Output is correct
64 Correct 144 ms 41724 KB Output is correct
65 Correct 143 ms 41808 KB Output is correct
66 Correct 197 ms 33652 KB Output is correct
67 Correct 190 ms 33480 KB Output is correct
68 Correct 188 ms 33736 KB Output is correct
69 Correct 175 ms 33736 KB Output is correct
70 Correct 188 ms 33640 KB Output is correct
71 Correct 183 ms 33484 KB Output is correct
72 Correct 207 ms 33736 KB Output is correct
73 Correct 184 ms 32712 KB Output is correct
74 Correct 189 ms 33740 KB Output is correct
75 Correct 190 ms 33740 KB Output is correct
76 Correct 271 ms 31960 KB Output is correct
77 Correct 293 ms 30032 KB Output is correct
78 Correct 366 ms 35412 KB Output is correct
79 Correct 383 ms 33364 KB Output is correct
80 Correct 369 ms 40528 KB Output is correct
81 Correct 368 ms 36948 KB Output is correct
82 Correct 405 ms 37136 KB Output is correct
83 Correct 372 ms 33872 KB Output is correct
84 Correct 405 ms 39504 KB Output is correct
85 Correct 371 ms 37964 KB Output is correct
86 Correct 370 ms 33248 KB Output is correct
87 Correct 365 ms 35100 KB Output is correct
88 Correct 331 ms 36500 KB Output is correct
89 Correct 311 ms 32220 KB Output is correct
90 Correct 323 ms 32340 KB Output is correct
91 Correct 323 ms 34272 KB Output is correct
92 Correct 342 ms 33152 KB Output is correct
93 Correct 288 ms 32848 KB Output is correct
94 Correct 300 ms 32084 KB Output is correct
95 Correct 304 ms 33616 KB Output is correct
96 Correct 310 ms 32340 KB Output is correct
97 Correct 315 ms 34384 KB Output is correct