Submission #993210

# Submission time Handle Problem Language Result Execution time Memory
993210 2024-06-05T12:44:37 Z prvocislo Capital City (JOI20_capital_city) C++17
100 / 100
458 ms 62684 KB
// meow cat, please meow back =^_^=

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;

int n, k;
const int logn = 18;
const int maxn = 200005;
vector<int> adj[maxn], adj_rev[maxn];
bool used[maxn], in[maxn];
vector<int> order, component;
void dfs1(int v) {
    used[v] = true;

    for (auto u : adj[v])
        if (!used[u])
            dfs1(u);

    order.push_back(v);
}

void dfs2(int v) {
    used[v] = true;
    component.push_back(v);

    for (auto u : adj_rev[v])
        if (!used[u])
            dfs2(u);
}
void add(int u, int v)
{
    adj[u].push_back(v);
    adj_rev[v].push_back(u);
}
int solve()
{
    fill(used, used + k, false);
    for (int i = 0; i < k; i++)
        if (!used[i])
            dfs1(i);
    fill(used, used + k, false);
    reverse(order.begin(), order.end());
    int ans = k;
    for (auto v : order)
        if (!used[v]) {
            dfs2(v);
            bool okay = true;
            for (int i : component) in[i] = true;
            for (int i : component)
            {
                for (int j : adj[i]) if (!in[j]) okay = false;
            }
            for (int i : component) in[i] = false;
            if (okay)
                ans = min(ans, (int)component.size() - 1);
            component.clear();
        }
    return ans;
}

vector<vector<int> > g, l, col;
vector<int> d, c;
void dfs(int u)
{
    for (int j = 1; j < logn; j++) l[j][u] = l[j - 1][l[j - 1][u]];
    for (int v : g[u]) if (v != l[0][u]) d[v] = d[u] + 1, l[0][v] = u, dfs(v);
}
int lca(int u, int v)
{
    if (d[u] < d[v]) swap(u, v);
    for (int i = logn - 1; i >= 0; i--) if (d[l[i][u]] >= d[v]) u = l[i][u];
    for (int i = logn - 1; i >= 0; i--) if (l[i][u] != l[i][v]) u = l[i][u], v = l[i][v];
    return u == v ? u : l[0][u];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    g.assign(n, {}), l.assign(logn, vector<int>(n, 0)), d.assign(n, 0), col.assign(k, {}), c.assign(n, -1);
    for (int i = 0, a, b; i < n - 1; i++) cin >> a >> b, g[--a].push_back(--b), g[b].push_back(a);
    for (int i = 0; i < n; i++)
    {
        cin >> c[i];
        col[--c[i]].push_back(i);
    }
    dfs(0);
    for (int i = 0; i < k; i++)
    {
        int lc = col[i][0];
        for (int j : col[i]) lc = lca(lc, j);
        for (int j : col[i]) if (j != lc)
        {
            add(i, c[l[0][j]]);
        }
    }
    cout << solve() << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 10076 KB Output is correct
3 Correct 2 ms 10076 KB Output is correct
4 Correct 2 ms 10076 KB Output is correct
5 Correct 2 ms 10076 KB Output is correct
6 Correct 2 ms 10076 KB Output is correct
7 Correct 2 ms 10068 KB Output is correct
8 Correct 2 ms 10076 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 3 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 10076 KB Output is correct
3 Correct 2 ms 10076 KB Output is correct
4 Correct 2 ms 10076 KB Output is correct
5 Correct 2 ms 10076 KB Output is correct
6 Correct 2 ms 10076 KB Output is correct
7 Correct 2 ms 10068 KB Output is correct
8 Correct 2 ms 10076 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 3 ms 10076 KB Output is correct
11 Correct 3 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10292 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 3 ms 10384 KB Output is correct
18 Correct 3 ms 10328 KB Output is correct
19 Correct 3 ms 10340 KB Output is correct
20 Correct 3 ms 10332 KB Output is correct
21 Correct 3 ms 10328 KB Output is correct
22 Correct 3 ms 10340 KB Output is correct
23 Correct 4 ms 10332 KB Output is correct
24 Correct 3 ms 10340 KB Output is correct
25 Correct 3 ms 10332 KB Output is correct
26 Correct 5 ms 10340 KB Output is correct
27 Correct 3 ms 10332 KB Output is correct
28 Correct 3 ms 10500 KB Output is correct
29 Correct 3 ms 10332 KB Output is correct
30 Correct 3 ms 10332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 408 ms 60384 KB Output is correct
2 Correct 192 ms 62428 KB Output is correct
3 Correct 458 ms 61812 KB Output is correct
4 Correct 183 ms 62684 KB Output is correct
5 Correct 386 ms 59508 KB Output is correct
6 Correct 182 ms 62276 KB Output is correct
7 Correct 372 ms 59200 KB Output is correct
8 Correct 184 ms 60740 KB Output is correct
9 Correct 301 ms 54716 KB Output is correct
10 Correct 297 ms 53160 KB Output is correct
11 Correct 301 ms 54988 KB Output is correct
12 Correct 314 ms 56532 KB Output is correct
13 Correct 281 ms 52852 KB Output is correct
14 Correct 301 ms 56644 KB Output is correct
15 Correct 271 ms 56640 KB Output is correct
16 Correct 291 ms 53512 KB Output is correct
17 Correct 269 ms 53884 KB Output is correct
18 Correct 283 ms 54076 KB Output is correct
19 Correct 273 ms 56244 KB Output is correct
20 Correct 277 ms 57152 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 10076 KB Output is correct
3 Correct 2 ms 10076 KB Output is correct
4 Correct 2 ms 10076 KB Output is correct
5 Correct 2 ms 10076 KB Output is correct
6 Correct 2 ms 10076 KB Output is correct
7 Correct 2 ms 10068 KB Output is correct
8 Correct 2 ms 10076 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 3 ms 10076 KB Output is correct
11 Correct 3 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 3 ms 10292 KB Output is correct
14 Correct 3 ms 10332 KB Output is correct
15 Correct 3 ms 10332 KB Output is correct
16 Correct 3 ms 10332 KB Output is correct
17 Correct 3 ms 10384 KB Output is correct
18 Correct 3 ms 10328 KB Output is correct
19 Correct 3 ms 10340 KB Output is correct
20 Correct 3 ms 10332 KB Output is correct
21 Correct 3 ms 10328 KB Output is correct
22 Correct 3 ms 10340 KB Output is correct
23 Correct 4 ms 10332 KB Output is correct
24 Correct 3 ms 10340 KB Output is correct
25 Correct 3 ms 10332 KB Output is correct
26 Correct 5 ms 10340 KB Output is correct
27 Correct 3 ms 10332 KB Output is correct
28 Correct 3 ms 10500 KB Output is correct
29 Correct 3 ms 10332 KB Output is correct
30 Correct 3 ms 10332 KB Output is correct
31 Correct 408 ms 60384 KB Output is correct
32 Correct 192 ms 62428 KB Output is correct
33 Correct 458 ms 61812 KB Output is correct
34 Correct 183 ms 62684 KB Output is correct
35 Correct 386 ms 59508 KB Output is correct
36 Correct 182 ms 62276 KB Output is correct
37 Correct 372 ms 59200 KB Output is correct
38 Correct 184 ms 60740 KB Output is correct
39 Correct 301 ms 54716 KB Output is correct
40 Correct 297 ms 53160 KB Output is correct
41 Correct 301 ms 54988 KB Output is correct
42 Correct 314 ms 56532 KB Output is correct
43 Correct 281 ms 52852 KB Output is correct
44 Correct 301 ms 56644 KB Output is correct
45 Correct 271 ms 56640 KB Output is correct
46 Correct 291 ms 53512 KB Output is correct
47 Correct 269 ms 53884 KB Output is correct
48 Correct 283 ms 54076 KB Output is correct
49 Correct 273 ms 56244 KB Output is correct
50 Correct 277 ms 57152 KB Output is correct
51 Correct 2 ms 9820 KB Output is correct
52 Correct 190 ms 45992 KB Output is correct
53 Correct 192 ms 45924 KB Output is correct
54 Correct 200 ms 46148 KB Output is correct
55 Correct 211 ms 46144 KB Output is correct
56 Correct 199 ms 46144 KB Output is correct
57 Correct 196 ms 46148 KB Output is correct
58 Correct 276 ms 51524 KB Output is correct
59 Correct 293 ms 52040 KB Output is correct
60 Correct 333 ms 51680 KB Output is correct
61 Correct 366 ms 51508 KB Output is correct
62 Correct 179 ms 62360 KB Output is correct
63 Correct 173 ms 62528 KB Output is correct
64 Correct 168 ms 61220 KB Output is correct
65 Correct 167 ms 62168 KB Output is correct
66 Correct 208 ms 52036 KB Output is correct
67 Correct 187 ms 52060 KB Output is correct
68 Correct 216 ms 52032 KB Output is correct
69 Correct 220 ms 51780 KB Output is correct
70 Correct 222 ms 51668 KB Output is correct
71 Correct 235 ms 51780 KB Output is correct
72 Correct 214 ms 52036 KB Output is correct
73 Correct 186 ms 51276 KB Output is correct
74 Correct 228 ms 51780 KB Output is correct
75 Correct 215 ms 52008 KB Output is correct
76 Correct 320 ms 56388 KB Output is correct
77 Correct 291 ms 55360 KB Output is correct
78 Correct 266 ms 54080 KB Output is correct
79 Correct 269 ms 52608 KB Output is correct
80 Correct 279 ms 56900 KB Output is correct
81 Correct 264 ms 54852 KB Output is correct
82 Correct 260 ms 54848 KB Output is correct
83 Correct 272 ms 52812 KB Output is correct
84 Correct 279 ms 56724 KB Output is correct
85 Correct 290 ms 55360 KB Output is correct
86 Correct 285 ms 52800 KB Output is correct
87 Correct 275 ms 53824 KB Output is correct
88 Correct 270 ms 53912 KB Output is correct
89 Correct 267 ms 51520 KB Output is correct
90 Correct 273 ms 51264 KB Output is correct
91 Correct 258 ms 52800 KB Output is correct
92 Correct 273 ms 52288 KB Output is correct
93 Correct 270 ms 51708 KB Output is correct
94 Correct 248 ms 51264 KB Output is correct
95 Correct 264 ms 52300 KB Output is correct
96 Correct 264 ms 51524 KB Output is correct
97 Correct 308 ms 52644 KB Output is correct