Submission #1027404

# Submission time Handle Problem Language Result Execution time Memory
1027404 2024-07-19T05:54:39 Z adaawf Capital City (JOI20_capital_city) C++17
100 / 100
414 ms 45288 KB
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> g[200005], v[200005], vv;
int c[200005], dd[200005], da[200005], db[200005], a[200005];
int tn[200005], tt[200005], d[200005], s[200005], z = 0;
void dfs(int x, int p) {
    tn[x] = ++z;
    s[x] = 1;
    c[a[x]]++;
    d[x] = p;
    for (int w : g[x]) {
        if (w == p || dd[w] == 1) continue;
        dfs(w, x);
        s[x] += s[w];
    }
    tt[x] = z;
}
void dfs2(int x, int p) {
    da[a[x]] = db[x] = c[a[x]] = 0;
    for (int w : g[x]) {
        if (w == p || dd[w] == 1) continue;
        dfs2(w, x);
    }
}
int centroid(int x, int p, int h) {
    for (int w : g[x]) {
        if (w == p || dd[w] == 1) continue;
        if (s[w] > h / 2) {
            return centroid(w, x, h);
        }
    }
    return x;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k, mi = 1e9;
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        v[a[i]].push_back(i);
    }
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int p = q.front();
        q.pop();
        if (dd[p] == 1) continue;
        z = 0;
        dfs(p, -1);
        int h = centroid(p, -1, s[p]), flag = 0, k = h, res = 0;
        vv.push_back(a[h]);
        da[a[h]] = 1;
        for (int i = 0; i < vv.size(); i++) {
            int w = vv[i];
            if (c[w] != v[w].size()) {
                flag = 1;
                break;
            }
            res++;
            for (int ww : v[w]) {
                while (tn[k] > tn[ww] || tt[k] < tt[ww]) {
                    k = d[k];
                    db[k] = 1;
                    if (da[a[k]] == 0) {
                        vv.push_back(a[k]);
                        da[a[k]] = 1;
                    }
                }
                int h = ww;
                while (db[h] == 0 && h != d[k]) {
                    if (da[a[h]] == 0) {
                        vv.push_back(a[h]);
                        da[a[h]] = 1;
                    }
                    db[h] = 1;
                    h = d[h];
                }
            }
        }
        if (flag == 0) mi = min(mi, res - 1);
        dfs2(p, -1);
        dd[h] = 1;
        vv.clear();
        for (int w : g[h]) {
            if (dd[w] == 0) {
                q.push(w);
            }
        }
    }
    cout << mi;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         for (int i = 0; i < vv.size(); i++) {
      |                         ~~^~~~~~~~~~~
capital_city.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |             if (c[w] != v[w].size()) {
      |                 ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 3 ms 10584 KB Output is correct
3 Correct 3 ms 9820 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9816 KB Output is correct
9 Correct 3 ms 9896 KB Output is correct
10 Correct 5 ms 9976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 3 ms 10584 KB Output is correct
3 Correct 3 ms 9820 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9816 KB Output is correct
9 Correct 3 ms 9896 KB Output is correct
10 Correct 5 ms 9976 KB Output is correct
11 Correct 6 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 4 ms 9820 KB Output is correct
14 Correct 4 ms 9820 KB Output is correct
15 Correct 5 ms 9872 KB Output is correct
16 Correct 5 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 4 ms 10140 KB Output is correct
20 Correct 4 ms 10076 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 4 ms 9936 KB Output is correct
24 Correct 5 ms 10328 KB Output is correct
25 Correct 6 ms 10328 KB Output is correct
26 Correct 6 ms 10332 KB Output is correct
27 Correct 5 ms 10124 KB Output is correct
28 Correct 5 ms 10076 KB Output is correct
29 Correct 4 ms 10832 KB Output is correct
30 Correct 4 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 275 ms 41296 KB Output is correct
2 Correct 140 ms 41600 KB Output is correct
3 Correct 286 ms 40788 KB Output is correct
4 Correct 125 ms 41812 KB Output is correct
5 Correct 263 ms 37968 KB Output is correct
6 Correct 146 ms 44888 KB Output is correct
7 Correct 287 ms 41556 KB Output is correct
8 Correct 137 ms 44444 KB Output is correct
9 Correct 376 ms 39356 KB Output is correct
10 Correct 349 ms 36952 KB Output is correct
11 Correct 378 ms 40424 KB Output is correct
12 Correct 366 ms 43204 KB Output is correct
13 Correct 328 ms 37124 KB Output is correct
14 Correct 380 ms 43348 KB Output is correct
15 Correct 353 ms 42580 KB Output is correct
16 Correct 363 ms 38140 KB Output is correct
17 Correct 337 ms 38564 KB Output is correct
18 Correct 391 ms 38864 KB Output is correct
19 Correct 340 ms 42068 KB Output is correct
20 Correct 344 ms 43604 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 3 ms 10584 KB Output is correct
3 Correct 3 ms 9820 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 3 ms 9820 KB Output is correct
8 Correct 3 ms 9816 KB Output is correct
9 Correct 3 ms 9896 KB Output is correct
10 Correct 5 ms 9976 KB Output is correct
11 Correct 6 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 4 ms 9820 KB Output is correct
14 Correct 4 ms 9820 KB Output is correct
15 Correct 5 ms 9872 KB Output is correct
16 Correct 5 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 4 ms 10140 KB Output is correct
20 Correct 4 ms 10076 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 4 ms 9936 KB Output is correct
24 Correct 5 ms 10328 KB Output is correct
25 Correct 6 ms 10328 KB Output is correct
26 Correct 6 ms 10332 KB Output is correct
27 Correct 5 ms 10124 KB Output is correct
28 Correct 5 ms 10076 KB Output is correct
29 Correct 4 ms 10832 KB Output is correct
30 Correct 4 ms 10844 KB Output is correct
31 Correct 275 ms 41296 KB Output is correct
32 Correct 140 ms 41600 KB Output is correct
33 Correct 286 ms 40788 KB Output is correct
34 Correct 125 ms 41812 KB Output is correct
35 Correct 263 ms 37968 KB Output is correct
36 Correct 146 ms 44888 KB Output is correct
37 Correct 287 ms 41556 KB Output is correct
38 Correct 137 ms 44444 KB Output is correct
39 Correct 376 ms 39356 KB Output is correct
40 Correct 349 ms 36952 KB Output is correct
41 Correct 378 ms 40424 KB Output is correct
42 Correct 366 ms 43204 KB Output is correct
43 Correct 328 ms 37124 KB Output is correct
44 Correct 380 ms 43348 KB Output is correct
45 Correct 353 ms 42580 KB Output is correct
46 Correct 363 ms 38140 KB Output is correct
47 Correct 337 ms 38564 KB Output is correct
48 Correct 391 ms 38864 KB Output is correct
49 Correct 340 ms 42068 KB Output is correct
50 Correct 344 ms 43604 KB Output is correct
51 Correct 3 ms 10588 KB Output is correct
52 Correct 215 ms 27220 KB Output is correct
53 Correct 254 ms 27220 KB Output is correct
54 Correct 220 ms 27180 KB Output is correct
55 Correct 219 ms 27220 KB Output is correct
56 Correct 227 ms 27220 KB Output is correct
57 Correct 224 ms 27188 KB Output is correct
58 Correct 295 ms 31212 KB Output is correct
59 Correct 241 ms 31576 KB Output is correct
60 Correct 250 ms 31428 KB Output is correct
61 Correct 305 ms 31312 KB Output is correct
62 Correct 140 ms 45288 KB Output is correct
63 Correct 141 ms 45288 KB Output is correct
64 Correct 125 ms 44628 KB Output is correct
65 Correct 133 ms 44916 KB Output is correct
66 Correct 168 ms 36556 KB Output is correct
67 Correct 193 ms 36548 KB Output is correct
68 Correct 186 ms 36300 KB Output is correct
69 Correct 207 ms 36676 KB Output is correct
70 Correct 169 ms 36552 KB Output is correct
71 Correct 171 ms 36552 KB Output is correct
72 Correct 206 ms 36044 KB Output is correct
73 Correct 180 ms 35788 KB Output is correct
74 Correct 177 ms 36036 KB Output is correct
75 Correct 170 ms 36556 KB Output is correct
76 Correct 308 ms 35176 KB Output is correct
77 Correct 301 ms 33644 KB Output is correct
78 Correct 344 ms 38568 KB Output is correct
79 Correct 387 ms 36592 KB Output is correct
80 Correct 349 ms 43604 KB Output is correct
81 Correct 366 ms 39508 KB Output is correct
82 Correct 406 ms 39620 KB Output is correct
83 Correct 372 ms 36940 KB Output is correct
84 Correct 414 ms 42836 KB Output is correct
85 Correct 340 ms 41300 KB Output is correct
86 Correct 367 ms 36580 KB Output is correct
87 Correct 342 ms 37716 KB Output is correct
88 Correct 304 ms 38828 KB Output is correct
89 Correct 289 ms 35412 KB Output is correct
90 Correct 297 ms 35176 KB Output is correct
91 Correct 283 ms 36968 KB Output is correct
92 Correct 295 ms 35668 KB Output is correct
93 Correct 290 ms 35380 KB Output is correct
94 Correct 280 ms 35412 KB Output is correct
95 Correct 270 ms 36692 KB Output is correct
96 Correct 281 ms 35412 KB Output is correct
97 Correct 292 ms 37204 KB Output is correct