Submission #924088

# Submission time Handle Problem Language Result Execution time Memory
924088 2024-02-08T12:03:11 Z boris_mihov Capital City (JOI20_capital_city) C++17
100 / 100
488 ms 45596 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF  = 1e9;

int n, k;
int col[MAXN];
std::vector <int> g[MAXN];
std::vector <int> c[MAXN];
std::vector <int> inColor[MAXN];
int sz[MAXN];
int in[MAXN];
int out[MAXN];
int par[MAXN];
bool vis[MAXN];
int timer;

void calcSize(int node, int par)
{
    sz[node] = 1;
    for (const int &u : g[node])
    {
        if (u == par || vis[u])
        {
            continue;
        }

        calcSize(u, node);
        sz[node] += sz[u];
    }
}

int findCentroid(int node, int par, int globalSz)
{
    for (const int &u : g[node])
    {
        if (u != par && !vis[u] && sz[u] > globalSz / 2)
        {
            return findCentroid(u, node, globalSz);
        }
    }

    return node;
}

int decompose(int node)
{
    calcSize(node, 0);
    int cntr = findCentroid(node, 0, sz[node]);

    vis[cntr] = true;
    for (const int &u : g[cntr])
    {
        if (vis[u])
        {
            continue;
        }

        c[cntr].push_back(decompose(u));
    }
    
    return cntr;
}

int dep[MAXN];
void buildDFS(int node)
{
    in[node] = ++timer;
    for (const int &u : c[node])
    {
        dep[u] = dep[node] + 1;
        buildDFS(u);
    }

    out[node] = ++timer;
}

bool inSubtree(int x, int y)
{
    return in[y] <= in[x] && in[x] <= out[y];
}

int ans = INF;
void prepare(int node, int p, int lvl)
{
    par[node] = p;
    vis[col[node]] = false;
    for (const int &u : g[node])
    {
        if (u == p || dep[u] <= lvl)
        {
            continue;
        }

        prepare(u, node, lvl);
    }
}   

void solveDFS(int node)
{
    prepare(node, 0, dep[node]);
    std::queue <int> waitlist;
    waitlist.push(col[node]);
    vis[col[node]] = true;

    int res = -1;
    bool isBad = false;
    while (waitlist.size())
    {
        int top = waitlist.front();
        waitlist.pop();
        res++;

        for (const int &u : inColor[top])
        {
            if (!inSubtree(u, node))
            {
                isBad = true;
                break;
            }
            
            if (par[u] != 0 && !vis[col[par[u]]])
            {
                vis[col[par[u]]] = true;
                waitlist.push(col[par[u]]);
            }
        }

        if (isBad)
        {
            break;
        }
    }

    if (!isBad)
    {
        ans = std::min(ans, res);
    }

    prepare(node, 0, dep[node]);
    for (const int &u : c[node])
    {
        solveDFS(u);
    }
}

void solve()
{
    int root = decompose(1);
    std::fill(vis + 1, vis + 1 + n, false);
    buildDFS(root);
    solveDFS(root);

    std::cout << ans << '\n';
}

void input()
{
    std::cin >> n >> k;
    for (int i = 1 ; i < n ; ++i)
    {
        int u, v;
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> col[i];
        inColor[col[i]].push_back(i);
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16828 KB Output is correct
8 Correct 3 ms 16728 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 5 ms 18780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16828 KB Output is correct
8 Correct 3 ms 16728 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 5 ms 18780 KB Output is correct
11 Correct 5 ms 16988 KB Output is correct
12 Correct 4 ms 16828 KB Output is correct
13 Correct 5 ms 16984 KB Output is correct
14 Correct 4 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 6 ms 16988 KB Output is correct
17 Correct 4 ms 16988 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 5 ms 17080 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 6 ms 16988 KB Output is correct
23 Correct 5 ms 17076 KB Output is correct
24 Correct 5 ms 16984 KB Output is correct
25 Correct 5 ms 16924 KB Output is correct
26 Correct 6 ms 16984 KB Output is correct
27 Correct 5 ms 16988 KB Output is correct
28 Correct 5 ms 16988 KB Output is correct
29 Correct 5 ms 16988 KB Output is correct
30 Correct 5 ms 16984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 44884 KB Output is correct
2 Correct 178 ms 45596 KB Output is correct
3 Correct 381 ms 44740 KB Output is correct
4 Correct 200 ms 45440 KB Output is correct
5 Correct 367 ms 42320 KB Output is correct
6 Correct 193 ms 45396 KB Output is correct
7 Correct 368 ms 42704 KB Output is correct
8 Correct 171 ms 45000 KB Output is correct
9 Correct 482 ms 41040 KB Output is correct
10 Correct 488 ms 38992 KB Output is correct
11 Correct 451 ms 41164 KB Output is correct
12 Correct 468 ms 43604 KB Output is correct
13 Correct 459 ms 38736 KB Output is correct
14 Correct 467 ms 43616 KB Output is correct
15 Correct 460 ms 43492 KB Output is correct
16 Correct 456 ms 39504 KB Output is correct
17 Correct 448 ms 40016 KB Output is correct
18 Correct 457 ms 40016 KB Output is correct
19 Correct 449 ms 42836 KB Output is correct
20 Correct 447 ms 44480 KB Output is correct
21 Correct 5 ms 18780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16828 KB Output is correct
8 Correct 3 ms 16728 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 5 ms 18780 KB Output is correct
11 Correct 5 ms 16988 KB Output is correct
12 Correct 4 ms 16828 KB Output is correct
13 Correct 5 ms 16984 KB Output is correct
14 Correct 4 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 6 ms 16988 KB Output is correct
17 Correct 4 ms 16988 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 5 ms 17080 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 6 ms 16988 KB Output is correct
23 Correct 5 ms 17076 KB Output is correct
24 Correct 5 ms 16984 KB Output is correct
25 Correct 5 ms 16924 KB Output is correct
26 Correct 6 ms 16984 KB Output is correct
27 Correct 5 ms 16988 KB Output is correct
28 Correct 5 ms 16988 KB Output is correct
29 Correct 5 ms 16988 KB Output is correct
30 Correct 5 ms 16984 KB Output is correct
31 Correct 355 ms 44884 KB Output is correct
32 Correct 178 ms 45596 KB Output is correct
33 Correct 381 ms 44740 KB Output is correct
34 Correct 200 ms 45440 KB Output is correct
35 Correct 367 ms 42320 KB Output is correct
36 Correct 193 ms 45396 KB Output is correct
37 Correct 368 ms 42704 KB Output is correct
38 Correct 171 ms 45000 KB Output is correct
39 Correct 482 ms 41040 KB Output is correct
40 Correct 488 ms 38992 KB Output is correct
41 Correct 451 ms 41164 KB Output is correct
42 Correct 468 ms 43604 KB Output is correct
43 Correct 459 ms 38736 KB Output is correct
44 Correct 467 ms 43616 KB Output is correct
45 Correct 460 ms 43492 KB Output is correct
46 Correct 456 ms 39504 KB Output is correct
47 Correct 448 ms 40016 KB Output is correct
48 Correct 457 ms 40016 KB Output is correct
49 Correct 449 ms 42836 KB Output is correct
50 Correct 447 ms 44480 KB Output is correct
51 Correct 5 ms 18780 KB Output is correct
52 Correct 284 ms 30092 KB Output is correct
53 Correct 296 ms 30104 KB Output is correct
54 Correct 307 ms 30036 KB Output is correct
55 Correct 296 ms 30200 KB Output is correct
56 Correct 328 ms 30032 KB Output is correct
57 Correct 287 ms 30036 KB Output is correct
58 Correct 294 ms 33516 KB Output is correct
59 Correct 251 ms 33796 KB Output is correct
60 Correct 323 ms 32596 KB Output is correct
61 Correct 316 ms 32336 KB Output is correct
62 Correct 169 ms 45376 KB Output is correct
63 Correct 181 ms 45236 KB Output is correct
64 Correct 176 ms 45096 KB Output is correct
65 Correct 183 ms 45396 KB Output is correct
66 Correct 235 ms 36684 KB Output is correct
67 Correct 231 ms 36684 KB Output is correct
68 Correct 242 ms 36684 KB Output is correct
69 Correct 241 ms 36908 KB Output is correct
70 Correct 235 ms 36688 KB Output is correct
71 Correct 232 ms 36852 KB Output is correct
72 Correct 231 ms 36892 KB Output is correct
73 Correct 237 ms 36172 KB Output is correct
74 Correct 227 ms 36684 KB Output is correct
75 Correct 240 ms 36680 KB Output is correct
76 Correct 334 ms 36496 KB Output is correct
77 Correct 329 ms 34784 KB Output is correct
78 Correct 445 ms 39984 KB Output is correct
79 Correct 444 ms 38116 KB Output is correct
80 Correct 451 ms 44112 KB Output is correct
81 Correct 440 ms 41044 KB Output is correct
82 Correct 454 ms 41560 KB Output is correct
83 Correct 427 ms 38484 KB Output is correct
84 Correct 473 ms 43436 KB Output is correct
85 Correct 436 ms 42028 KB Output is correct
86 Correct 432 ms 38092 KB Output is correct
87 Correct 451 ms 39764 KB Output is correct
88 Correct 376 ms 39620 KB Output is correct
89 Correct 379 ms 36444 KB Output is correct
90 Correct 367 ms 36180 KB Output is correct
91 Correct 377 ms 38228 KB Output is correct
92 Correct 371 ms 36932 KB Output is correct
93 Correct 373 ms 36640 KB Output is correct
94 Correct 383 ms 36180 KB Output is correct
95 Correct 379 ms 37312 KB Output is correct
96 Correct 376 ms 36436 KB Output is correct
97 Correct 384 ms 37968 KB Output is correct