Submission #217212

# Submission time Handle Problem Language Result Execution time Memory
217212 2020-03-29T08:49:39 Z DodgeBallMan Capital City (JOI20_capital_city) C++14
100 / 100
1102 ms 41208 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5+5;

int n, k, ans = 1e9;
int C[N], cnt[N];
vector<int> g[N];

int sz[N];
bitset<N> chk;

int get_sz(int u, int p) {
    sz[u] = 1;
    for(int v : g[u]) if(v != p && !chk[v])
        sz[u] += get_sz(v, u);
    return sz[u];
}

int find_cen(int u, int p, int all, int &ret) {
    int mx = all - sz[u];
    for(int v : g[u]) if(v != p && !chk[v])
        mx = max(mx, find_cen(v, u, all, ret));
    if(mx <= (all + 1) / 2) ret = u;
    return sz[u];
}

int par[N], used[N];
vector<int> col[N];
queue<int> Q;

void dfs(int u, int p, bool fill) {
    if(fill) col[C[u]].emplace_back(u), par[u] = p;
    else col[C[u]].clear(), used[C[u]] = 0;
    for(int v : g[u]) if(v != p && !chk[v])
        dfs(v, u, fill);
}

void process(int u) {
    get_sz(u, u), find_cen(u, u, sz[u], u);
    dfs(u, 0, 1);

    int ret = 1;
    bool valid = true;
    Q.emplace(C[u]), used[C[u]] = 1;
    while(!Q.empty()) {
        int now = Q.front(); Q.pop();
        if(col[now].size() != cnt[now]) { valid = false; break; }
        for(int x : col[now]) if(x != u && !used[C[par[x]]]) {
            used[C[par[x]]] = 1, ++ret;
            Q.emplace(C[par[x]]);
        }
    }
    if(valid) ans = min(ans, ret);

    dfs(u, 0, 0), chk[u] = 1;
    for(int v : g[u]) if(!chk[v]) process(v); 
}

int main() {
    scanf("%d %d", &n, &k);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b), g[b].emplace_back(a);
    }
    for(int i = 1; i <= n; i++) scanf("%d", C + i), ++cnt[C[i]];
    process(1);

    printf("%d\n", ans - 1);

    return 0;
}

Compilation message

capital_city.cpp: In function 'void process(int)':
capital_city.cpp:49:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(col[now].size() != cnt[now]) { valid = false; break; }
            ~~~~~~~~~~~~~~~~^~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:67:51: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= n; i++) scanf("%d", C + i), ++cnt[C[i]];
                                 ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 12 ms 9856 KB Output is correct
16 Correct 14 ms 9984 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 11 ms 9984 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9856 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 14 ms 10368 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 960 ms 40468 KB Output is correct
2 Correct 318 ms 41208 KB Output is correct
3 Correct 938 ms 40060 KB Output is correct
4 Correct 304 ms 41088 KB Output is correct
5 Correct 926 ms 36472 KB Output is correct
6 Correct 301 ms 40952 KB Output is correct
7 Correct 879 ms 36832 KB Output is correct
8 Correct 301 ms 40568 KB Output is correct
9 Correct 970 ms 34680 KB Output is correct
10 Correct 970 ms 31864 KB Output is correct
11 Correct 976 ms 35160 KB Output is correct
12 Correct 992 ms 38552 KB Output is correct
13 Correct 1010 ms 31356 KB Output is correct
14 Correct 964 ms 38648 KB Output is correct
15 Correct 1100 ms 38328 KB Output is correct
16 Correct 1102 ms 32296 KB Output is correct
17 Correct 1020 ms 32988 KB Output is correct
18 Correct 1034 ms 33180 KB Output is correct
19 Correct 1016 ms 36984 KB Output is correct
20 Correct 993 ms 39676 KB Output is correct
21 Correct 9 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 12 ms 9856 KB Output is correct
16 Correct 14 ms 9984 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 11 ms 9984 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9856 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 14 ms 10368 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 960 ms 40468 KB Output is correct
32 Correct 318 ms 41208 KB Output is correct
33 Correct 938 ms 40060 KB Output is correct
34 Correct 304 ms 41088 KB Output is correct
35 Correct 926 ms 36472 KB Output is correct
36 Correct 301 ms 40952 KB Output is correct
37 Correct 879 ms 36832 KB Output is correct
38 Correct 301 ms 40568 KB Output is correct
39 Correct 970 ms 34680 KB Output is correct
40 Correct 970 ms 31864 KB Output is correct
41 Correct 976 ms 35160 KB Output is correct
42 Correct 992 ms 38552 KB Output is correct
43 Correct 1010 ms 31356 KB Output is correct
44 Correct 964 ms 38648 KB Output is correct
45 Correct 1100 ms 38328 KB Output is correct
46 Correct 1102 ms 32296 KB Output is correct
47 Correct 1020 ms 32988 KB Output is correct
48 Correct 1034 ms 33180 KB Output is correct
49 Correct 1016 ms 36984 KB Output is correct
50 Correct 993 ms 39676 KB Output is correct
51 Correct 9 ms 9728 KB Output is correct
52 Correct 665 ms 20344 KB Output is correct
53 Correct 798 ms 20636 KB Output is correct
54 Correct 861 ms 20448 KB Output is correct
55 Correct 748 ms 20472 KB Output is correct
56 Correct 733 ms 20472 KB Output is correct
57 Correct 710 ms 20472 KB Output is correct
58 Correct 637 ms 24824 KB Output is correct
59 Correct 616 ms 25468 KB Output is correct
60 Correct 844 ms 24444 KB Output is correct
61 Correct 908 ms 23928 KB Output is correct
62 Correct 295 ms 41080 KB Output is correct
63 Correct 305 ms 41024 KB Output is correct
64 Correct 310 ms 40696 KB Output is correct
65 Correct 315 ms 41084 KB Output is correct
66 Correct 469 ms 30832 KB Output is correct
67 Correct 482 ms 30964 KB Output is correct
68 Correct 477 ms 30832 KB Output is correct
69 Correct 495 ms 30828 KB Output is correct
70 Correct 489 ms 30960 KB Output is correct
71 Correct 472 ms 30832 KB Output is correct
72 Correct 490 ms 30960 KB Output is correct
73 Correct 477 ms 29808 KB Output is correct
74 Correct 475 ms 31084 KB Output is correct
75 Correct 513 ms 31084 KB Output is correct
76 Correct 745 ms 29024 KB Output is correct
77 Correct 717 ms 26888 KB Output is correct
78 Correct 972 ms 32828 KB Output is correct
79 Correct 978 ms 30456 KB Output is correct
80 Correct 974 ms 39160 KB Output is correct
81 Correct 1002 ms 34936 KB Output is correct
82 Correct 986 ms 34936 KB Output is correct
83 Correct 987 ms 30968 KB Output is correct
84 Correct 978 ms 38060 KB Output is correct
85 Correct 984 ms 36216 KB Output is correct
86 Correct 970 ms 30460 KB Output is correct
87 Correct 1006 ms 32760 KB Output is correct
88 Correct 860 ms 33912 KB Output is correct
89 Correct 875 ms 29172 KB Output is correct
90 Correct 827 ms 29048 KB Output is correct
91 Correct 858 ms 31944 KB Output is correct
92 Correct 876 ms 30072 KB Output is correct
93 Correct 866 ms 30072 KB Output is correct
94 Correct 843 ms 29180 KB Output is correct
95 Correct 858 ms 30840 KB Output is correct
96 Correct 861 ms 29304 KB Output is correct
97 Correct 843 ms 31844 KB Output is correct