Submission #808587

# Submission time Handle Problem Language Result Execution time Memory
808587 2023-08-05T08:02:07 Z FedShat Capital City (JOI20_capital_city) C++17
100 / 100
1872 ms 346788 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;

template<class T>
istream &operator>>(istream &is, vector<T> &a) {
    for (auto &i : a) {
        is >> i;
    }
    return is;
}

#ifdef __APPLE__
#include "debug.h"
#else
#define debug(...) 42
#endif

struct LCA {
    vector<int> depth, parent, up;

    LCA() {}
    LCA(int n) {
        depth.resize(n);
        parent.resize(n);
        up.resize(n);
    }

    void add_leaf(int v, int par) {
        parent[v] = par;
        depth[v] = depth[par] + 1;
        if (depth[par] - depth[up[par]] == depth[up[par]] - depth[up[up[par]]]) {
            up[v] = up[up[par]];
        } else {
            up[v] = par;
        }
    }

    int la(int v, int k) {
        int h = max(depth[v] - k, 0);
        while (depth[v] != h) {
            if (depth[up[v]] >= h) {
                v = up[v];
            } else {
                v = parent[v];
            }
        }
        return v;
    }

    int lca(int v, int u) {
        if (depth[v] > depth[u]) {
            v = la(v, depth[v] - depth[u]);
        } else {
            u = la(u, depth[u] - depth[v]);
        }
        while (v != u) {
            if (up[v] != up[u]) {
                v = up[v];
                u = up[u];
            } else {
                v = parent[v];
                u = parent[u];
            }
        }
        return v;
    }
};

vector<vector<int>> tr, g, gg;
vector<int> tin, tout, c, ggg;
int timer = 0;
LCA st;
constexpr int LG = 18;

void add(int v, int p) {
    g.emplace_back();
    ggg[v] = g.size() - 1;
    g.back().push_back(c[v]);
    gg[0][v] = ggg[p];
    for (int i = 1; i < LG; ++i) {
        g.emplace_back();
        gg[i][v] = g.size() - 1;
        g.back().emplace_back(gg[i - 1][v]);
        g.back().emplace_back(gg[i - 1][st.la(v, 1 << (i - 1))]);
    }
}

void dfs(int v, int p) {
    tin[v] = timer++;
    if (p == -1) {
        g.emplace_back();
        ggg[v] = g.size() - 1;
        g.back().push_back(c[v]);
    }
    for (auto u : tr[v]) {
        if (u != p) {
            st.add_leaf(u, v);
            add(u, v);
            dfs(u, v);
        }
    }
    tout[v] = timer;
}

vector<int> val;
vector<int> curst;
vector<int> cond, siz;
int cnt = 0;
int k;

int dfs_scc(int j) {
    static int timer = 0;
    int low = val[j] = ++timer;
    curst.push_back(j);
    for (auto e : g[j]) {
        if (cond[e] < 0) {
            low = min(low, val[e] ? val[e] : dfs_scc(e));
        }
    }
    if (low == val[j]) {
        int x;
        do {
            x = curst.back();
            curst.pop_back();
            siz[cnt] += (x < k);
            cond[x] = cnt;
        } while (x != j);
        ++cnt;
    }
    return val[j] = low;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
#endif
    int n;
    cin >> n >> k;
    tr.resize(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        tr[u].push_back(v);
        tr[v].push_back(u);
    }
    c.resize(n);
    cin >> c;
    for (auto &i : c) {
        --i;
    }
    tin.resize(n);
    tout.resize(n);
    st = LCA(n);
    g.reserve(LG * n + k);
    g.resize(k);
    gg.resize(LG, vector<int>(n));
    ggg.resize(n);
    dfs(0, -1);
    vector<vector<int>> cc(k);
    for (int i = 0; i < n; ++i) {
        cc[c[i]].push_back(i);
    }
    auto add_edges = [&](int v, int d) {
        int cv = c[v];
        // for (int i = 0; i < d; ++i) {
        //     v = st.up[0][v];
        //     g[cv].push_back(c[v]);
        // }
        // if (cv == 2) {
        //     cout << "nigga\n";
        // }
        for (int i = LG - 1; i >= 0; --i) {
            if (d >= (1 << i)) {
                g[cv].push_back(gg[i][v]);
                d -= (1 << i);
                v = st.la(v, (1 << i));
            }
        }
    };
    for (int i = 0; i < k; ++i) {
        sort(cc[i].begin(), cc[i].end(), [&](int u, int v) {
            return tin[u] < tin[v];
        });
        for (int j = 0; j + 1 < (int) cc[i].size(); ++j) {
            int l = st.lca(cc[i][j], cc[i][j + 1]);
            add_edges(cc[i][j], st.depth[cc[i][j]] - st.depth[l]);
            add_edges(cc[i][j + 1], st.depth[cc[i][j + 1]] - st.depth[l]);
        }
    }
    // for (int i = 0; i < (int) g.size(); ++i) {
    //     for (int j : g[i]) {
    //         cout << i + 1 << " " << j + 1 << "\n";
    //     }
    // }
    gg.clear();
    gg.shrink_to_fit();
    int sz = g.size();
    val.resize(sz);
    cond.resize(sz, -1);
    siz.resize(sz);
    for (int i = 0; i < sz; ++i) {
        if (cond[i] < 0) {
            dfs_scc(i);
        }
    }
    vector<int> iznas(cnt);
    for (int i = 0; i < sz; ++i) {
        for (auto j : g[i]) {
            if (cond[i] != cond[j]) {
                ++iznas[cond[i]];
            }
        }
    }
    // debug(siz);
    // debug(iznas);
    int ans = INF;
    for (int i = 0; i < cnt; ++i) {
        if (iznas[i] == 0) {
            ans = min(ans, siz[i]);
        }
    }
    cout << ans - 1 << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 5 ms 3284 KB Output is correct
12 Correct 5 ms 3284 KB Output is correct
13 Correct 5 ms 3156 KB Output is correct
14 Correct 5 ms 3284 KB Output is correct
15 Correct 5 ms 3284 KB Output is correct
16 Correct 5 ms 3284 KB Output is correct
17 Correct 5 ms 3284 KB Output is correct
18 Correct 5 ms 3284 KB Output is correct
19 Correct 6 ms 3276 KB Output is correct
20 Correct 6 ms 3284 KB Output is correct
21 Correct 5 ms 3284 KB Output is correct
22 Correct 6 ms 3284 KB Output is correct
23 Correct 7 ms 3668 KB Output is correct
24 Correct 7 ms 3284 KB Output is correct
25 Correct 6 ms 3308 KB Output is correct
26 Correct 7 ms 3284 KB Output is correct
27 Correct 6 ms 3284 KB Output is correct
28 Correct 5 ms 3292 KB Output is correct
29 Correct 6 ms 3300 KB Output is correct
30 Correct 5 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1872 ms 346788 KB Output is correct
2 Correct 1018 ms 301696 KB Output is correct
3 Correct 1783 ms 314168 KB Output is correct
4 Correct 998 ms 301700 KB Output is correct
5 Correct 1713 ms 341812 KB Output is correct
6 Correct 985 ms 301440 KB Output is correct
7 Correct 1731 ms 329852 KB Output is correct
8 Correct 925 ms 299320 KB Output is correct
9 Correct 1333 ms 289148 KB Output is correct
10 Correct 1356 ms 286716 KB Output is correct
11 Correct 1309 ms 289668 KB Output is correct
12 Correct 1335 ms 292096 KB Output is correct
13 Correct 1370 ms 286352 KB Output is correct
14 Correct 1347 ms 292512 KB Output is correct
15 Correct 1360 ms 292024 KB Output is correct
16 Correct 1456 ms 287072 KB Output is correct
17 Correct 1303 ms 287600 KB Output is correct
18 Correct 1395 ms 288036 KB Output is correct
19 Correct 1341 ms 291024 KB Output is correct
20 Correct 1380 ms 293556 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 5 ms 3284 KB Output is correct
12 Correct 5 ms 3284 KB Output is correct
13 Correct 5 ms 3156 KB Output is correct
14 Correct 5 ms 3284 KB Output is correct
15 Correct 5 ms 3284 KB Output is correct
16 Correct 5 ms 3284 KB Output is correct
17 Correct 5 ms 3284 KB Output is correct
18 Correct 5 ms 3284 KB Output is correct
19 Correct 6 ms 3276 KB Output is correct
20 Correct 6 ms 3284 KB Output is correct
21 Correct 5 ms 3284 KB Output is correct
22 Correct 6 ms 3284 KB Output is correct
23 Correct 7 ms 3668 KB Output is correct
24 Correct 7 ms 3284 KB Output is correct
25 Correct 6 ms 3308 KB Output is correct
26 Correct 7 ms 3284 KB Output is correct
27 Correct 6 ms 3284 KB Output is correct
28 Correct 5 ms 3292 KB Output is correct
29 Correct 6 ms 3300 KB Output is correct
30 Correct 5 ms 3284 KB Output is correct
31 Correct 1872 ms 346788 KB Output is correct
32 Correct 1018 ms 301696 KB Output is correct
33 Correct 1783 ms 314168 KB Output is correct
34 Correct 998 ms 301700 KB Output is correct
35 Correct 1713 ms 341812 KB Output is correct
36 Correct 985 ms 301440 KB Output is correct
37 Correct 1731 ms 329852 KB Output is correct
38 Correct 925 ms 299320 KB Output is correct
39 Correct 1333 ms 289148 KB Output is correct
40 Correct 1356 ms 286716 KB Output is correct
41 Correct 1309 ms 289668 KB Output is correct
42 Correct 1335 ms 292096 KB Output is correct
43 Correct 1370 ms 286352 KB Output is correct
44 Correct 1347 ms 292512 KB Output is correct
45 Correct 1360 ms 292024 KB Output is correct
46 Correct 1456 ms 287072 KB Output is correct
47 Correct 1303 ms 287600 KB Output is correct
48 Correct 1395 ms 288036 KB Output is correct
49 Correct 1341 ms 291024 KB Output is correct
50 Correct 1380 ms 293556 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 922 ms 283780 KB Output is correct
53 Correct 910 ms 283880 KB Output is correct
54 Correct 890 ms 283884 KB Output is correct
55 Correct 947 ms 283916 KB Output is correct
56 Correct 947 ms 283824 KB Output is correct
57 Correct 924 ms 283812 KB Output is correct
58 Correct 1269 ms 286724 KB Output is correct
59 Correct 1269 ms 290200 KB Output is correct
60 Correct 1411 ms 290404 KB Output is correct
61 Correct 1423 ms 286492 KB Output is correct
62 Correct 984 ms 301712 KB Output is correct
63 Correct 1034 ms 301692 KB Output is correct
64 Correct 954 ms 299868 KB Output is correct
65 Correct 1008 ms 301504 KB Output is correct
66 Correct 1058 ms 286680 KB Output is correct
67 Correct 1156 ms 287300 KB Output is correct
68 Correct 1106 ms 286572 KB Output is correct
69 Correct 1136 ms 286484 KB Output is correct
70 Correct 1143 ms 286580 KB Output is correct
71 Correct 1094 ms 286576 KB Output is correct
72 Correct 1170 ms 286624 KB Output is correct
73 Correct 1431 ms 287008 KB Output is correct
74 Correct 1335 ms 286512 KB Output is correct
75 Correct 1236 ms 286584 KB Output is correct
76 Correct 1306 ms 288832 KB Output is correct
77 Correct 1323 ms 288124 KB Output is correct
78 Correct 1403 ms 287636 KB Output is correct
79 Correct 1424 ms 285764 KB Output is correct
80 Correct 1343 ms 293068 KB Output is correct
81 Correct 1291 ms 289504 KB Output is correct
82 Correct 1363 ms 289320 KB Output is correct
83 Correct 1338 ms 286032 KB Output is correct
84 Correct 1344 ms 292152 KB Output is correct
85 Correct 1343 ms 290508 KB Output is correct
86 Correct 1314 ms 285764 KB Output is correct
87 Correct 1296 ms 287508 KB Output is correct
88 Correct 1575 ms 289492 KB Output is correct
89 Correct 1460 ms 285312 KB Output is correct
90 Correct 1496 ms 285244 KB Output is correct
91 Correct 1497 ms 287824 KB Output is correct
92 Correct 1499 ms 286044 KB Output is correct
93 Correct 1483 ms 285684 KB Output is correct
94 Correct 1579 ms 285152 KB Output is correct
95 Correct 1580 ms 286872 KB Output is correct
96 Correct 1622 ms 285412 KB Output is correct
97 Correct 1613 ms 287500 KB Output is correct