답안 #924090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924090 2024-02-08T12:04:09 Z boris_mihov 수도 (JOI20_capital_city) C++17
100 / 100
400 ms 45396 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);
    }

    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 16732 KB Output is correct
2 Correct 4 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 3 ms 16728 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16728 KB Output is correct
8 Correct 4 ms 16732 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 18884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 16732 KB Output is correct
2 Correct 4 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 3 ms 16728 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16728 KB Output is correct
8 Correct 4 ms 16732 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 18884 KB Output is correct
11 Correct 4 ms 16984 KB Output is correct
12 Correct 6 ms 16988 KB Output is correct
13 Correct 5 ms 16988 KB Output is correct
14 Correct 4 ms 16844 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 5 ms 16988 KB Output is correct
17 Correct 4 ms 17092 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 4 ms 16984 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 5 ms 16988 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 5 ms 17072 KB Output is correct
26 Correct 5 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 16988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 335 ms 45180 KB Output is correct
2 Correct 156 ms 45320 KB Output is correct
3 Correct 324 ms 44616 KB Output is correct
4 Correct 145 ms 45280 KB Output is correct
5 Correct 325 ms 42192 KB Output is correct
6 Correct 147 ms 45396 KB Output is correct
7 Correct 306 ms 42580 KB Output is correct
8 Correct 154 ms 45160 KB Output is correct
9 Correct 390 ms 41064 KB Output is correct
10 Correct 397 ms 39280 KB Output is correct
11 Correct 389 ms 41296 KB Output is correct
12 Correct 396 ms 43760 KB Output is correct
13 Correct 389 ms 38736 KB Output is correct
14 Correct 384 ms 43796 KB Output is correct
15 Correct 398 ms 43384 KB Output is correct
16 Correct 398 ms 39524 KB Output is correct
17 Correct 365 ms 39704 KB Output is correct
18 Correct 376 ms 40016 KB Output is correct
19 Correct 372 ms 42576 KB Output is correct
20 Correct 385 ms 44368 KB Output is correct
21 Correct 4 ms 18776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 16732 KB Output is correct
2 Correct 4 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 3 ms 16728 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16728 KB Output is correct
8 Correct 4 ms 16732 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 18884 KB Output is correct
11 Correct 4 ms 16984 KB Output is correct
12 Correct 6 ms 16988 KB Output is correct
13 Correct 5 ms 16988 KB Output is correct
14 Correct 4 ms 16844 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 5 ms 16988 KB Output is correct
17 Correct 4 ms 17092 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
20 Correct 4 ms 16984 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 5 ms 16988 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 5 ms 17072 KB Output is correct
26 Correct 5 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 16988 KB Output is correct
31 Correct 335 ms 45180 KB Output is correct
32 Correct 156 ms 45320 KB Output is correct
33 Correct 324 ms 44616 KB Output is correct
34 Correct 145 ms 45280 KB Output is correct
35 Correct 325 ms 42192 KB Output is correct
36 Correct 147 ms 45396 KB Output is correct
37 Correct 306 ms 42580 KB Output is correct
38 Correct 154 ms 45160 KB Output is correct
39 Correct 390 ms 41064 KB Output is correct
40 Correct 397 ms 39280 KB Output is correct
41 Correct 389 ms 41296 KB Output is correct
42 Correct 396 ms 43760 KB Output is correct
43 Correct 389 ms 38736 KB Output is correct
44 Correct 384 ms 43796 KB Output is correct
45 Correct 398 ms 43384 KB Output is correct
46 Correct 398 ms 39524 KB Output is correct
47 Correct 365 ms 39704 KB Output is correct
48 Correct 376 ms 40016 KB Output is correct
49 Correct 372 ms 42576 KB Output is correct
50 Correct 385 ms 44368 KB Output is correct
51 Correct 4 ms 18776 KB Output is correct
52 Correct 255 ms 30004 KB Output is correct
53 Correct 272 ms 30032 KB Output is correct
54 Correct 278 ms 30036 KB Output is correct
55 Correct 285 ms 30396 KB Output is correct
56 Correct 293 ms 30040 KB Output is correct
57 Correct 268 ms 30032 KB Output is correct
58 Correct 254 ms 33516 KB Output is correct
59 Correct 245 ms 33712 KB Output is correct
60 Correct 304 ms 32888 KB Output is correct
61 Correct 306 ms 32336 KB Output is correct
62 Correct 143 ms 45272 KB Output is correct
63 Correct 142 ms 45392 KB Output is correct
64 Correct 145 ms 44972 KB Output is correct
65 Correct 149 ms 45308 KB Output is correct
66 Correct 231 ms 36940 KB Output is correct
67 Correct 226 ms 36688 KB Output is correct
68 Correct 221 ms 36684 KB Output is correct
69 Correct 218 ms 36836 KB Output is correct
70 Correct 231 ms 36684 KB Output is correct
71 Correct 210 ms 36692 KB Output is correct
72 Correct 217 ms 36816 KB Output is correct
73 Correct 222 ms 36180 KB Output is correct
74 Correct 220 ms 36664 KB Output is correct
75 Correct 215 ms 36680 KB Output is correct
76 Correct 315 ms 36500 KB Output is correct
77 Correct 302 ms 35324 KB Output is correct
78 Correct 391 ms 39936 KB Output is correct
79 Correct 400 ms 38364 KB Output is correct
80 Correct 397 ms 44036 KB Output is correct
81 Correct 374 ms 41156 KB Output is correct
82 Correct 397 ms 41076 KB Output is correct
83 Correct 397 ms 38520 KB Output is correct
84 Correct 395 ms 43088 KB Output is correct
85 Correct 377 ms 41812 KB Output is correct
86 Correct 394 ms 38480 KB Output is correct
87 Correct 385 ms 39712 KB Output is correct
88 Correct 326 ms 39536 KB Output is correct
89 Correct 327 ms 36440 KB Output is correct
90 Correct 329 ms 36048 KB Output is correct
91 Correct 337 ms 38188 KB Output is correct
92 Correct 324 ms 36944 KB Output is correct
93 Correct 343 ms 36868 KB Output is correct
94 Correct 327 ms 36348 KB Output is correct
95 Correct 341 ms 37712 KB Output is correct
96 Correct 323 ms 36488 KB Output is correct
97 Correct 325 ms 37968 KB Output is correct