답안 #967126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967126 2024-04-21T07:37:48 Z sleepntsheep 수도 (JOI20_capital_city) C
100 / 100
332 ms 24404 KB
#include <stdio.h>

#define N 200000
#define M (2*N-2)
#define N_ N+1
#define M_ M+1
#define K N

int n, k, color[N], answer, head[N], nxt[M_], vv[M_], head_[K], nxt_[N_], vv_[N_];

void link(int*head, int*nxt, int*vv, int*i, int u, int v)
{
    nxt[i[0]] = head[u];
    vv[i[0]] = v;
    head[u] = i[0]++;
}

int dead[N], sz[N];

void dfs2(int u, int p)
{
    sz[u] = 1;
    for (int j = head[u]; j; j = nxt[j])
    {
        if (vv[j] == p || dead[vv[j]])
            continue;
        dfs2(vv[j], u);
        sz[u] += sz[vv[j]];
    }
}

int par[N], visit[K], dfreq[K], cfreq[K];

void dfs3(int u, int p, int delta)
{
    visit[color[u]] = -1;
    dfreq[color[u]] += delta;
    par[u] = p;
    for (int j = head[u]; j; j = nxt[j])
    {
        if (vv[j] == p || dead[vv[j]])
            continue;
        dfs3(vv[j], u, delta);
    }
}

int get_centroid(int u, int p, int tsize)
{
    for (int j = head[u]; j; j = nxt[j])
        if (!dead[vv[j]] && vv[j] != p && sz[vv[j]] * 2 >= tsize)
            return get_centroid(vv[j], u, tsize);
    return u;
}

void centroid_decomposition(int u)
{
    dfs2(u, u);
    u = get_centroid(u, u, sz[u]);
    dead[u] = 1;

    dfs3(u, u, 1);

    int col = color[u];
    if (dfreq[col] == cfreq[col])
    {
        static int qu[N], head, tail;
        head = tail = 0;

        int dcolor = 0;
        for (int at, jj = head_[col]; jj; jj = nxt_[jj])
            qu[head++] = vv_[jj];
        visit[col] = 1;

        while (head-tail)
        {
            int uu = qu[tail++];
            if (uu == u || visit[color[par[uu]]] == 1)
                continue;
            int nextcol = color[par[uu]];
            if (cfreq[nextcol] != dfreq[nextcol])
                goto NOTOK;
            ++dcolor;
            for (int jj = head_[nextcol]; jj; jj = nxt_[jj])
                qu[head++] = vv_[jj];
            visit[nextcol] = 1;
        }

        if (dcolor < answer)
            answer = dcolor;
NOTOK:;
    }
    dfs3(u, u, -1);

    for (int j = head[u]; j; j = nxt[j])
        if (!dead[vv[j]])
            centroid_decomposition(vv[j]);
}

int main()
{
    scanf("%d%d", &n, &k);
    answer = n;
    for (int i = 1, u, v, eo[1] = { 1 }; i < n; ++i)
    {
        scanf("%d%d", &u, &v); --u, --v;
        link(head, nxt, vv, eo, u, v);
        link(head, nxt, vv, eo, v, u);
    }
    for (int eo[1] = { 1 }, i = 0; i < n; ++i)
    {
        scanf("%d", color+i);
        ++cfreq[--color[i]];
        link(head_, nxt_, vv_, eo, color[i], i);
    }

    centroid_decomposition(0);

    printf("%d", answer);
}

Compilation message

capital_city.c: In function 'centroid_decomposition':
capital_city.c:70:18: warning: unused variable 'at' [-Wunused-variable]
   70 |         for (int at, jj = head_[col]; jj; jj = nxt_[jj])
      |                  ^~
capital_city.c: In function 'main':
capital_city.c:101:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |     scanf("%d%d", &n, &k);
      |     ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:105:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%d%d", &u, &v); --u, --v;
      |         ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:111:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         scanf("%d", color+i);
      |         ^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10584 KB Output is correct
9 Correct 2 ms 10672 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10584 KB Output is correct
9 Correct 2 ms 10672 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 2 ms 10584 KB Output is correct
12 Correct 2 ms 10584 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10684 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 2 ms 10588 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10744 KB Output is correct
30 Correct 2 ms 10584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 213 ms 23864 KB Output is correct
2 Correct 134 ms 24268 KB Output is correct
3 Correct 208 ms 23376 KB Output is correct
4 Correct 137 ms 24264 KB Output is correct
5 Correct 219 ms 21332 KB Output is correct
6 Correct 142 ms 24324 KB Output is correct
7 Correct 233 ms 21676 KB Output is correct
8 Correct 129 ms 24244 KB Output is correct
9 Correct 309 ms 20968 KB Output is correct
10 Correct 288 ms 19032 KB Output is correct
11 Correct 290 ms 21168 KB Output is correct
12 Correct 317 ms 23376 KB Output is correct
13 Correct 300 ms 18516 KB Output is correct
14 Correct 313 ms 23660 KB Output is correct
15 Correct 290 ms 23380 KB Output is correct
16 Correct 311 ms 19540 KB Output is correct
17 Correct 288 ms 19848 KB Output is correct
18 Correct 311 ms 19948 KB Output is correct
19 Correct 293 ms 22448 KB Output is correct
20 Correct 290 ms 24148 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 1 ms 10584 KB Output is correct
9 Correct 2 ms 10672 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 2 ms 10584 KB Output is correct
12 Correct 2 ms 10584 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10684 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 2 ms 10588 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10744 KB Output is correct
30 Correct 2 ms 10584 KB Output is correct
31 Correct 213 ms 23864 KB Output is correct
32 Correct 134 ms 24268 KB Output is correct
33 Correct 208 ms 23376 KB Output is correct
34 Correct 137 ms 24264 KB Output is correct
35 Correct 219 ms 21332 KB Output is correct
36 Correct 142 ms 24324 KB Output is correct
37 Correct 233 ms 21676 KB Output is correct
38 Correct 129 ms 24244 KB Output is correct
39 Correct 309 ms 20968 KB Output is correct
40 Correct 288 ms 19032 KB Output is correct
41 Correct 290 ms 21168 KB Output is correct
42 Correct 317 ms 23376 KB Output is correct
43 Correct 300 ms 18516 KB Output is correct
44 Correct 313 ms 23660 KB Output is correct
45 Correct 290 ms 23380 KB Output is correct
46 Correct 311 ms 19540 KB Output is correct
47 Correct 288 ms 19848 KB Output is correct
48 Correct 311 ms 19948 KB Output is correct
49 Correct 293 ms 22448 KB Output is correct
50 Correct 290 ms 24148 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 184 ms 11952 KB Output is correct
53 Correct 220 ms 11920 KB Output is correct
54 Correct 186 ms 11924 KB Output is correct
55 Correct 186 ms 11856 KB Output is correct
56 Correct 190 ms 11864 KB Output is correct
57 Correct 184 ms 11692 KB Output is correct
58 Correct 176 ms 13908 KB Output is correct
59 Correct 196 ms 14400 KB Output is correct
60 Correct 234 ms 13136 KB Output is correct
61 Correct 256 ms 12948 KB Output is correct
62 Correct 137 ms 24096 KB Output is correct
63 Correct 137 ms 24272 KB Output is correct
64 Correct 133 ms 24404 KB Output is correct
65 Correct 131 ms 24284 KB Output is correct
66 Correct 146 ms 18044 KB Output is correct
67 Correct 142 ms 17748 KB Output is correct
68 Correct 140 ms 18004 KB Output is correct
69 Correct 142 ms 18004 KB Output is correct
70 Correct 149 ms 18004 KB Output is correct
71 Correct 170 ms 18044 KB Output is correct
72 Correct 140 ms 17832 KB Output is correct
73 Correct 143 ms 17312 KB Output is correct
74 Correct 143 ms 17824 KB Output is correct
75 Correct 153 ms 17828 KB Output is correct
76 Correct 209 ms 16300 KB Output is correct
77 Correct 208 ms 14932 KB Output is correct
78 Correct 302 ms 20048 KB Output is correct
79 Correct 292 ms 18260 KB Output is correct
80 Correct 307 ms 23960 KB Output is correct
81 Correct 306 ms 21072 KB Output is correct
82 Correct 332 ms 21244 KB Output is correct
83 Correct 296 ms 18516 KB Output is correct
84 Correct 296 ms 23232 KB Output is correct
85 Correct 292 ms 21920 KB Output is correct
86 Correct 293 ms 18004 KB Output is correct
87 Correct 295 ms 19672 KB Output is correct
88 Correct 263 ms 20308 KB Output is correct
89 Correct 258 ms 16976 KB Output is correct
90 Correct 257 ms 16976 KB Output is correct
91 Correct 262 ms 18972 KB Output is correct
92 Correct 273 ms 17756 KB Output is correct
93 Correct 265 ms 17492 KB Output is correct
94 Correct 271 ms 17268 KB Output is correct
95 Correct 273 ms 18340 KB Output is correct
96 Correct 263 ms 17264 KB Output is correct
97 Correct 276 ms 18876 KB Output is correct