Submission #1036728

# Submission time Handle Problem Language Result Execution time Memory
1036728 2024-07-27T16:06:06 Z vladilius Capital City (JOI20_capital_city) C++17
100 / 100
552 ms 157544 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert

struct STG{
    vector<vector<int>> g;
    vector<int> t;
    int n, k;
    STG(int& ns, int& ks, vector<int>& ts){
        n = ns;
        k = ks;
        t = ts;
        g.resize(4 * n + k);
        build(1, 1, n);
    }
    void build(int v, int tl, int tr){
        if (tl == tr){
            g[v + k].pb(t[tl]);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        build(vv, tl, tm);
        build(vv + 1, tm + 1, tr);
        v += k; vv += k;
        g[v].pb(vv); g[v].pb(vv + 1);
    }
    void add(int v, int tl, int tr, int& l, int& r, int& u){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            g[u].pb(v + k);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        add(vv, tl, tm, l, r, u);
        add(vv + 1, tm + 1, tr, l, r, u);
    }
    void add(int l, int r, int u){
        add(1, 1, n, l, r, u);
    }
    vector<vector<int>> gt;
    vector<int> order;
    vector<bool> used;
    void dfs1(int v){
        used[v] = 1;
        for (int i: g[v]){
            if (!used[i]){
                dfs1(i);
            }
        }
        order.pb(v);
    }
    vector<int> cp, cc;
    int cur;
    void dfs2(int v){
        cp[v] = cur;
        cc[cur] += (v <= k);
        for (int i: gt[v]){
            if (!cp[i]){
                dfs2(i);
            }
        }
    }
    vector<vector<int>> gg;
    void dfs3(int v){
        used[v] = 1;
        for (int i: gg[v]){
            if (!used[i]){
                dfs3(i);
            }
        }
    }
    void scc(){
        used.resize(g.size());
        for (int i = 1; i <= k + 1; i++){
            if (!used[i]) dfs1(i);
        }
        reverse(order.begin(), order.end());
        
        gt.resize(g.size());
        for (int i = 1; i < g.size(); i++){
            for (int j: g[i]){
                gt[j].pb(i);
            }
        }
        
        cp.resize(g.size());
        cc.resize(g.size());
        cur = 0;
        for (int i: order){
            if (!cp[i]){
                cur++;
                dfs2(i);
            }
        }
        
        gg.resize(cur + 1);
        for (int i = 1; i < g.size(); i++){
            for (int j: g[i]){
                if (cp[i] != cp[j]){
                    gg[cp[i]].pb(cp[j]);
                }
            }
        }
        int out = k;
        for (int i = 1; i <= cur; i++){
            if (gg[i].empty()){
                out = min(out, cc[i]);
            }
        }
        cout<<--out<<"\n";
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, k; cin>>n>>k;
    vector<int> g[n + 1];
    for (int i = 1; i < n; i++){
        int x, y; cin>>x>>y;
        g[x].pb(y);
        g[y].pb(x);
    }
    vector<int> ct[k + 1], c(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>c[i];
        ct[c[i]].pb(i);
    }
    
    vector<int> sz(n + 1), p(n + 1), h(n + 1), d(n + 1);
    function<void(int, int)> fill = [&](int v, int pr){
        sz[v] = 1;
        p[v] = pr;
        for (int i: g[v]){
            if (i == pr) continue;
            d[i] = d[v] + 1;
            fill(i, v);
            sz[v] += sz[i];
            if (sz[i] > sz[h[v]]){
                h[v] = i;
            }
        }
    };
    fill(1, 0);
    
    vector<int> head(n + 1), pos(n + 1);
    int timer = 0;
    function<void(int, int)> fill_hld = [&](int v, int H){
        head[v] = H;
        pos[v] = ++timer;
        if (!h[v]) return;
        fill_hld(h[v], H);
        for (int i: g[v]){
            if (pos[i]) continue;
            fill_hld(i, i);
        }
    };
    fill_hld(1, 1);
    
    vector<int> t(n + 1);
    for (int i = 1; i <= n; i++){
        t[pos[i]] = c[i];
    }
    
    STG T(n, k, t);
    
    auto add = [&](int x, int y, int i){
        while (head[x] != head[y]){
            if (d[head[x]] > d[head[y]]) swap(x, y);
            T.add(pos[head[y]], pos[y], i);
            y = p[head[y]];
        }
        if (d[x] > d[y]) swap(x, y);
        T.add(pos[x], pos[y], i);
    };
    
    for (int i = 1; i <= k; i++){
        for (int j = 0; j + 1 < ct[i].size(); j++){
            add(ct[i][j], ct[i][j + 1], i);
        }
    }
    
    T.scc();
}

Compilation message

capital_city.cpp: In member function 'void STG::scc()':
capital_city.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int i = 1; i < g.size(); i++){
      |                         ~~^~~~~~~~~~
capital_city.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for (int i = 1; i < g.size(); i++){
      |                         ~~^~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:185:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |         for (int j = 0; j + 1 < ct[i].size(); j++){
      |                         ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 1628 KB Output is correct
12 Correct 3 ms 1580 KB Output is correct
13 Correct 3 ms 1628 KB Output is correct
14 Correct 3 ms 1628 KB Output is correct
15 Correct 2 ms 1628 KB Output is correct
16 Correct 3 ms 1628 KB Output is correct
17 Correct 2 ms 1628 KB Output is correct
18 Correct 2 ms 1628 KB Output is correct
19 Correct 2 ms 1628 KB Output is correct
20 Correct 2 ms 1748 KB Output is correct
21 Correct 2 ms 1628 KB Output is correct
22 Correct 2 ms 1884 KB Output is correct
23 Correct 2 ms 1884 KB Output is correct
24 Correct 2 ms 1628 KB Output is correct
25 Correct 2 ms 1448 KB Output is correct
26 Correct 2 ms 1628 KB Output is correct
27 Correct 2 ms 1628 KB Output is correct
28 Correct 2 ms 1616 KB Output is correct
29 Correct 2 ms 1616 KB Output is correct
30 Correct 2 ms 1608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 395 ms 155844 KB Output is correct
2 Correct 238 ms 157512 KB Output is correct
3 Correct 363 ms 157124 KB Output is correct
4 Correct 239 ms 157472 KB Output is correct
5 Correct 382 ms 152388 KB Output is correct
6 Correct 249 ms 157124 KB Output is correct
7 Correct 331 ms 150656 KB Output is correct
8 Correct 225 ms 150344 KB Output is correct
9 Correct 307 ms 128720 KB Output is correct
10 Correct 323 ms 125656 KB Output is correct
11 Correct 330 ms 130156 KB Output is correct
12 Correct 327 ms 131852 KB Output is correct
13 Correct 312 ms 124756 KB Output is correct
14 Correct 324 ms 132616 KB Output is correct
15 Correct 327 ms 131788 KB Output is correct
16 Correct 323 ms 126376 KB Output is correct
17 Correct 319 ms 126980 KB Output is correct
18 Correct 325 ms 128260 KB Output is correct
19 Correct 334 ms 131292 KB Output is correct
20 Correct 326 ms 133156 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 1628 KB Output is correct
12 Correct 3 ms 1580 KB Output is correct
13 Correct 3 ms 1628 KB Output is correct
14 Correct 3 ms 1628 KB Output is correct
15 Correct 2 ms 1628 KB Output is correct
16 Correct 3 ms 1628 KB Output is correct
17 Correct 2 ms 1628 KB Output is correct
18 Correct 2 ms 1628 KB Output is correct
19 Correct 2 ms 1628 KB Output is correct
20 Correct 2 ms 1748 KB Output is correct
21 Correct 2 ms 1628 KB Output is correct
22 Correct 2 ms 1884 KB Output is correct
23 Correct 2 ms 1884 KB Output is correct
24 Correct 2 ms 1628 KB Output is correct
25 Correct 2 ms 1448 KB Output is correct
26 Correct 2 ms 1628 KB Output is correct
27 Correct 2 ms 1628 KB Output is correct
28 Correct 2 ms 1616 KB Output is correct
29 Correct 2 ms 1616 KB Output is correct
30 Correct 2 ms 1608 KB Output is correct
31 Correct 395 ms 155844 KB Output is correct
32 Correct 238 ms 157512 KB Output is correct
33 Correct 363 ms 157124 KB Output is correct
34 Correct 239 ms 157472 KB Output is correct
35 Correct 382 ms 152388 KB Output is correct
36 Correct 249 ms 157124 KB Output is correct
37 Correct 331 ms 150656 KB Output is correct
38 Correct 225 ms 150344 KB Output is correct
39 Correct 307 ms 128720 KB Output is correct
40 Correct 323 ms 125656 KB Output is correct
41 Correct 330 ms 130156 KB Output is correct
42 Correct 327 ms 131852 KB Output is correct
43 Correct 312 ms 124756 KB Output is correct
44 Correct 324 ms 132616 KB Output is correct
45 Correct 327 ms 131788 KB Output is correct
46 Correct 323 ms 126376 KB Output is correct
47 Correct 319 ms 126980 KB Output is correct
48 Correct 325 ms 128260 KB Output is correct
49 Correct 334 ms 131292 KB Output is correct
50 Correct 326 ms 133156 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 529 ms 132804 KB Output is correct
53 Correct 552 ms 133572 KB Output is correct
54 Correct 531 ms 134084 KB Output is correct
55 Correct 509 ms 133368 KB Output is correct
56 Correct 532 ms 133572 KB Output is correct
57 Correct 528 ms 133192 KB Output is correct
58 Correct 348 ms 135072 KB Output is correct
59 Correct 334 ms 135736 KB Output is correct
60 Correct 412 ms 145600 KB Output is correct
61 Correct 422 ms 148932 KB Output is correct
62 Correct 241 ms 157544 KB Output is correct
63 Correct 237 ms 157380 KB Output is correct
64 Correct 269 ms 152136 KB Output is correct
65 Correct 252 ms 157124 KB Output is correct
66 Correct 329 ms 140032 KB Output is correct
67 Correct 341 ms 139740 KB Output is correct
68 Correct 329 ms 140052 KB Output is correct
69 Correct 336 ms 139820 KB Output is correct
70 Correct 328 ms 139824 KB Output is correct
71 Correct 320 ms 139820 KB Output is correct
72 Correct 329 ms 139852 KB Output is correct
73 Correct 315 ms 139744 KB Output is correct
74 Correct 317 ms 139824 KB Output is correct
75 Correct 320 ms 139820 KB Output is correct
76 Correct 297 ms 131556 KB Output is correct
77 Correct 278 ms 129220 KB Output is correct
78 Correct 330 ms 127704 KB Output is correct
79 Correct 321 ms 123752 KB Output is correct
80 Correct 312 ms 132544 KB Output is correct
81 Correct 338 ms 129480 KB Output is correct
82 Correct 357 ms 129808 KB Output is correct
83 Correct 306 ms 124872 KB Output is correct
84 Correct 314 ms 131872 KB Output is correct
85 Correct 320 ms 130376 KB Output is correct
86 Correct 324 ms 123624 KB Output is correct
87 Correct 336 ms 126680 KB Output is correct
88 Correct 318 ms 134356 KB Output is correct
89 Correct 331 ms 130252 KB Output is correct
90 Correct 321 ms 130240 KB Output is correct
91 Correct 341 ms 132868 KB Output is correct
92 Correct 326 ms 131292 KB Output is correct
93 Correct 321 ms 129988 KB Output is correct
94 Correct 331 ms 129048 KB Output is correct
95 Correct 333 ms 131656 KB Output is correct
96 Correct 329 ms 129992 KB Output is correct
97 Correct 338 ms 136520 KB Output is correct