Submission #711836

# Submission time Handle Problem Language Result Execution time Memory
711836 2023-03-17T14:59:41 Z Jarif_Rahman Capital City (JOI20_capital_city) C++17
100 / 100
533 ms 81116 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, k;
vector<vector<int>> tree;
vector<pair<int, int>> edges;
vector<int> C, cnt;
vector<map<int, int>> cities;
vector<set<int>> graph;
vector<vector<int>> graph_r;

vector<int> order;
vector<bool> bl;
vector<int> comp_id;
vector<vector<int>> comp;

void dfs(int nd, int ss){
    for(int x: tree[nd]) if(x != ss) dfs(x, nd);

    for(int x: tree[nd]) if(x != ss && C[x] != C[nd] && cities[x][C[nd]] > 0)
        graph[C[nd]].insert(C[x]);
    for(int x: tree[nd]) if(x != ss){
        if(cities[x].size() > cities[nd].size()) swap(cities[x], cities[nd]);
        for(auto [c, y]: cities[x]) cities[nd][c]+=y;
        cities[x].clear();
    }
    cities[nd][C[nd]]++;
    if(ss != -1 && C[ss] != C[nd] && cities[nd][C[nd]] < cnt[C[nd]]) 
        graph[C[nd]].insert(C[ss]);
}

void dfs2(int nd){
    if(bl[nd]) return;
    bl[nd] = 1;
    for(int x: graph[nd]) dfs2(x);
    order.pb(nd);
}
void dfs3(int nd){
    if(bl[nd]) return;
    bl[nd] = 1;
    comp_id[nd] = int(comp.size())-1;
    comp.back().pb(nd);
    for(int x: graph_r[nd]) dfs3(x);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;

    tree.resize(n);
    C.resize(n);
    cnt.resize(k);
    cities.resize(n);
    graph.resize(k);
    graph_r.resize(k);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        tree[a].pb(b);
        tree[b].pb(a);
        edges.pb({a, b});
    }
    for(int &x: C) cin >> x, x--, cnt[x]++;

    dfs(0, -1);

    for(int i = 0; i < k; i++){
        for(int x: graph[i]) graph_r[x].pb(i);
    }

    bl.assign(k, 0);
    comp_id.resize(k);

    for(int i = 0; i < k; i++) if(!bl[i]) dfs2(i);
    reverse(order.begin(), order.end());

    bl.assign(k, 0);
    for(int x: order) if(!bl[x]){
        comp.pb({});
        dfs3(x);
    }

    int ans = n-1;
    for(int i = 0; i < comp.size(); i++){
        bool ok = 1;
        for(int x: comp[i]) for(int y: graph[x]) if(comp_id[y] != i) ok = 0;
        if(ok) ans = min(ans, int(comp[i].size())-1);
    }

    cout << ans << "\n";
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for(int i = 0; i < comp.size(); i++){
      |                    ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 3 ms 712 KB Output is correct
13 Correct 2 ms 748 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 2 ms 852 KB Output is correct
19 Correct 2 ms 844 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 2 ms 852 KB Output is correct
22 Correct 2 ms 1108 KB Output is correct
23 Correct 3 ms 980 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 2 ms 972 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 2 ms 836 KB Output is correct
29 Correct 2 ms 852 KB Output is correct
30 Correct 2 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 470 ms 80188 KB Output is correct
2 Correct 181 ms 81116 KB Output is correct
3 Correct 394 ms 79544 KB Output is correct
4 Correct 196 ms 81016 KB Output is correct
5 Correct 357 ms 73100 KB Output is correct
6 Correct 180 ms 80632 KB Output is correct
7 Correct 323 ms 72364 KB Output is correct
8 Correct 170 ms 77068 KB Output is correct
9 Correct 330 ms 62668 KB Output is correct
10 Correct 328 ms 58428 KB Output is correct
11 Correct 308 ms 63244 KB Output is correct
12 Correct 322 ms 67916 KB Output is correct
13 Correct 328 ms 57536 KB Output is correct
14 Correct 307 ms 68520 KB Output is correct
15 Correct 363 ms 67980 KB Output is correct
16 Correct 307 ms 59248 KB Output is correct
17 Correct 303 ms 60236 KB Output is correct
18 Correct 319 ms 60348 KB Output is correct
19 Correct 311 ms 65912 KB Output is correct
20 Correct 316 ms 69948 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 3 ms 712 KB Output is correct
13 Correct 2 ms 748 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 2 ms 852 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 2 ms 852 KB Output is correct
19 Correct 2 ms 844 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 2 ms 852 KB Output is correct
22 Correct 2 ms 1108 KB Output is correct
23 Correct 3 ms 980 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 2 ms 972 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 2 ms 836 KB Output is correct
29 Correct 2 ms 852 KB Output is correct
30 Correct 2 ms 844 KB Output is correct
31 Correct 470 ms 80188 KB Output is correct
32 Correct 181 ms 81116 KB Output is correct
33 Correct 394 ms 79544 KB Output is correct
34 Correct 196 ms 81016 KB Output is correct
35 Correct 357 ms 73100 KB Output is correct
36 Correct 180 ms 80632 KB Output is correct
37 Correct 323 ms 72364 KB Output is correct
38 Correct 170 ms 77068 KB Output is correct
39 Correct 330 ms 62668 KB Output is correct
40 Correct 328 ms 58428 KB Output is correct
41 Correct 308 ms 63244 KB Output is correct
42 Correct 322 ms 67916 KB Output is correct
43 Correct 328 ms 57536 KB Output is correct
44 Correct 307 ms 68520 KB Output is correct
45 Correct 363 ms 67980 KB Output is correct
46 Correct 307 ms 59248 KB Output is correct
47 Correct 303 ms 60236 KB Output is correct
48 Correct 319 ms 60348 KB Output is correct
49 Correct 311 ms 65912 KB Output is correct
50 Correct 316 ms 69948 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 512 ms 42760 KB Output is correct
53 Correct 462 ms 41180 KB Output is correct
54 Correct 533 ms 41872 KB Output is correct
55 Correct 467 ms 41432 KB Output is correct
56 Correct 506 ms 44220 KB Output is correct
57 Correct 494 ms 44040 KB Output is correct
58 Correct 285 ms 51396 KB Output is correct
59 Correct 292 ms 52336 KB Output is correct
60 Correct 367 ms 55344 KB Output is correct
61 Correct 420 ms 54772 KB Output is correct
62 Correct 178 ms 81080 KB Output is correct
63 Correct 182 ms 81100 KB Output is correct
64 Correct 182 ms 77988 KB Output is correct
65 Correct 182 ms 80640 KB Output is correct
66 Correct 225 ms 58948 KB Output is correct
67 Correct 278 ms 57420 KB Output is correct
68 Correct 236 ms 58836 KB Output is correct
69 Correct 237 ms 57820 KB Output is correct
70 Correct 227 ms 57640 KB Output is correct
71 Correct 225 ms 58844 KB Output is correct
72 Correct 266 ms 58756 KB Output is correct
73 Correct 238 ms 56104 KB Output is correct
74 Correct 232 ms 58880 KB Output is correct
75 Correct 279 ms 57856 KB Output is correct
76 Correct 312 ms 63112 KB Output is correct
77 Correct 326 ms 60192 KB Output is correct
78 Correct 320 ms 60204 KB Output is correct
79 Correct 313 ms 56912 KB Output is correct
80 Correct 335 ms 69116 KB Output is correct
81 Correct 311 ms 62912 KB Output is correct
82 Correct 356 ms 63264 KB Output is correct
83 Correct 295 ms 57292 KB Output is correct
84 Correct 369 ms 67720 KB Output is correct
85 Correct 308 ms 64856 KB Output is correct
86 Correct 317 ms 56588 KB Output is correct
87 Correct 324 ms 59636 KB Output is correct
88 Correct 320 ms 60984 KB Output is correct
89 Correct 277 ms 53836 KB Output is correct
90 Correct 273 ms 52856 KB Output is correct
91 Correct 296 ms 57780 KB Output is correct
92 Correct 289 ms 55288 KB Output is correct
93 Correct 272 ms 54972 KB Output is correct
94 Correct 275 ms 53808 KB Output is correct
95 Correct 278 ms 56288 KB Output is correct
96 Correct 297 ms 54148 KB Output is correct
97 Correct 294 ms 57704 KB Output is correct