Submission #967127

# Submission time Handle Problem Language Result Execution time Memory
967127 2024-04-21T07:38:46 Z vjudge1 Capital City (JOI20_capital_city) C
100 / 100
319 ms 24280 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);
      |         ^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 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 2 ms 10584 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 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 2 ms 10584 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10584 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 3 ms 10740 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10584 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 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 2 ms 10768 KB Output is correct
26 Correct 3 ms 10584 KB Output is correct
27 Correct 3 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 3 ms 10584 KB Output is correct
30 Correct 3 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 242 ms 23632 KB Output is correct
2 Correct 129 ms 24144 KB Output is correct
3 Correct 247 ms 23612 KB Output is correct
4 Correct 130 ms 24140 KB Output is correct
5 Correct 227 ms 21408 KB Output is correct
6 Correct 130 ms 24272 KB Output is correct
7 Correct 231 ms 21676 KB Output is correct
8 Correct 134 ms 24148 KB Output is correct
9 Correct 299 ms 21076 KB Output is correct
10 Correct 290 ms 19032 KB Output is correct
11 Correct 288 ms 21076 KB Output is correct
12 Correct 302 ms 23424 KB Output is correct
13 Correct 286 ms 18604 KB Output is correct
14 Correct 302 ms 23656 KB Output is correct
15 Correct 292 ms 23404 KB Output is correct
16 Correct 291 ms 19376 KB Output is correct
17 Correct 283 ms 19672 KB Output is correct
18 Correct 288 ms 19952 KB Output is correct
19 Correct 293 ms 22496 KB Output is correct
20 Correct 285 ms 24264 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 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 2 ms 10584 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 10584 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10584 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 3 ms 10740 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10584 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 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 2 ms 10768 KB Output is correct
26 Correct 3 ms 10584 KB Output is correct
27 Correct 3 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 3 ms 10584 KB Output is correct
30 Correct 3 ms 10588 KB Output is correct
31 Correct 242 ms 23632 KB Output is correct
32 Correct 129 ms 24144 KB Output is correct
33 Correct 247 ms 23612 KB Output is correct
34 Correct 130 ms 24140 KB Output is correct
35 Correct 227 ms 21408 KB Output is correct
36 Correct 130 ms 24272 KB Output is correct
37 Correct 231 ms 21676 KB Output is correct
38 Correct 134 ms 24148 KB Output is correct
39 Correct 299 ms 21076 KB Output is correct
40 Correct 290 ms 19032 KB Output is correct
41 Correct 288 ms 21076 KB Output is correct
42 Correct 302 ms 23424 KB Output is correct
43 Correct 286 ms 18604 KB Output is correct
44 Correct 302 ms 23656 KB Output is correct
45 Correct 292 ms 23404 KB Output is correct
46 Correct 291 ms 19376 KB Output is correct
47 Correct 283 ms 19672 KB Output is correct
48 Correct 288 ms 19952 KB Output is correct
49 Correct 293 ms 22496 KB Output is correct
50 Correct 285 ms 24264 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 186 ms 11920 KB Output is correct
53 Correct 182 ms 11860 KB Output is correct
54 Correct 193 ms 11920 KB Output is correct
55 Correct 197 ms 11860 KB Output is correct
56 Correct 191 ms 11764 KB Output is correct
57 Correct 184 ms 11768 KB Output is correct
58 Correct 183 ms 13916 KB Output is correct
59 Correct 189 ms 14260 KB Output is correct
60 Correct 239 ms 13280 KB Output is correct
61 Correct 238 ms 13136 KB Output is correct
62 Correct 132 ms 24264 KB Output is correct
63 Correct 136 ms 24148 KB Output is correct
64 Correct 132 ms 24148 KB Output is correct
65 Correct 140 ms 24280 KB Output is correct
66 Correct 143 ms 18040 KB Output is correct
67 Correct 149 ms 17744 KB Output is correct
68 Correct 147 ms 18040 KB Output is correct
69 Correct 166 ms 18004 KB Output is correct
70 Correct 141 ms 18004 KB Output is correct
71 Correct 140 ms 18040 KB Output is correct
72 Correct 155 ms 18004 KB Output is correct
73 Correct 137 ms 17268 KB Output is correct
74 Correct 142 ms 18000 KB Output is correct
75 Correct 145 ms 18004 KB Output is correct
76 Correct 224 ms 16544 KB Output is correct
77 Correct 219 ms 14936 KB Output is correct
78 Correct 288 ms 19820 KB Output is correct
79 Correct 296 ms 18260 KB Output is correct
80 Correct 296 ms 23888 KB Output is correct
81 Correct 314 ms 21160 KB Output is correct
82 Correct 303 ms 21072 KB Output is correct
83 Correct 298 ms 18516 KB Output is correct
84 Correct 299 ms 23240 KB Output is correct
85 Correct 319 ms 22152 KB Output is correct
86 Correct 303 ms 18216 KB Output is correct
87 Correct 302 ms 19580 KB Output is correct
88 Correct 276 ms 20412 KB Output is correct
89 Correct 274 ms 17184 KB Output is correct
90 Correct 245 ms 17264 KB Output is correct
91 Correct 242 ms 18772 KB Output is correct
92 Correct 246 ms 17748 KB Output is correct
93 Correct 268 ms 17644 KB Output is correct
94 Correct 250 ms 17180 KB Output is correct
95 Correct 255 ms 18288 KB Output is correct
96 Correct 264 ms 17264 KB Output is correct
97 Correct 264 ms 18860 KB Output is correct