Submission #967123

# Submission time Handle Problem Language Result Execution time Memory
967123 2024-04-21T07:34:22 Z sleepntsheep Capital City (JOI20_capital_city) C
100 / 100
335 ms 29656 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;
/* graph */
int head[N], nxt[M_], vv[M_];
int cfreq[K];

/* node with color */
int 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[N], dfreq[K];

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

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

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] = 86;
        //printf(" Got centroid %d\n",u);
        //
    dfs3(u, u);
    //for(int j=0;j<k;++j) printf(" : %d %d\n",cfreq[j],dfreq[j]);
    int col = color[u];
    if (dfreq[col] == cfreq[col])
    {
        static int qu[1<<20], head, tail;
        head = tail = 0;

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

        while (head-tail)
        {
            int uu = qu[tail++];
            //printf(" Exploring %d\n",uu);
            if (uu == u || visit[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[vv_[jj]] = 1;
        }

        //printf(" Ans in centroid %d %d\n",u,dcolor);
        if (dcolor < answer)
            answer = dcolor;
NOTOK:;
    }
    dfs4(u, u);

    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:89:18: warning: unused variable 'at' [-Wunused-variable]
   89 |         for (int at, jj = head_[col]; jj; jj = nxt_[jj])
      |                  ^~
capital_city.c: In function 'main':
capital_city.c:120:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |     scanf("%d%d", &n, &k);
      |     ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:124:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         scanf("%d%d", &u, &v); --u, --v;
      |         ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:130:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         scanf("%d", color+i);
      |         ^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12732 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 3 ms 12780 KB Output is correct
15 Correct 3 ms 12636 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 2 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 2 ms 12892 KB Output is correct
23 Correct 3 ms 12764 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 3 ms 12636 KB Output is correct
26 Correct 3 ms 12912 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12636 KB Output is correct
29 Correct 3 ms 12636 KB Output is correct
30 Correct 3 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 25584 KB Output is correct
2 Correct 131 ms 25784 KB Output is correct
3 Correct 232 ms 25084 KB Output is correct
4 Correct 138 ms 25936 KB Output is correct
5 Correct 234 ms 23068 KB Output is correct
6 Correct 137 ms 25776 KB Output is correct
7 Correct 232 ms 23428 KB Output is correct
8 Correct 142 ms 25772 KB Output is correct
9 Correct 306 ms 22448 KB Output is correct
10 Correct 312 ms 24068 KB Output is correct
11 Correct 315 ms 26456 KB Output is correct
12 Correct 308 ms 28500 KB Output is correct
13 Correct 316 ms 23892 KB Output is correct
14 Correct 316 ms 28752 KB Output is correct
15 Correct 316 ms 28752 KB Output is correct
16 Correct 330 ms 24560 KB Output is correct
17 Correct 302 ms 24976 KB Output is correct
18 Correct 326 ms 25000 KB Output is correct
19 Correct 304 ms 27472 KB Output is correct
20 Correct 320 ms 29460 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12732 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 3 ms 12780 KB Output is correct
15 Correct 3 ms 12636 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 2 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 2 ms 12892 KB Output is correct
23 Correct 3 ms 12764 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 3 ms 12636 KB Output is correct
26 Correct 3 ms 12912 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12636 KB Output is correct
29 Correct 3 ms 12636 KB Output is correct
30 Correct 3 ms 12636 KB Output is correct
31 Correct 240 ms 25584 KB Output is correct
32 Correct 131 ms 25784 KB Output is correct
33 Correct 232 ms 25084 KB Output is correct
34 Correct 138 ms 25936 KB Output is correct
35 Correct 234 ms 23068 KB Output is correct
36 Correct 137 ms 25776 KB Output is correct
37 Correct 232 ms 23428 KB Output is correct
38 Correct 142 ms 25772 KB Output is correct
39 Correct 306 ms 22448 KB Output is correct
40 Correct 312 ms 24068 KB Output is correct
41 Correct 315 ms 26456 KB Output is correct
42 Correct 308 ms 28500 KB Output is correct
43 Correct 316 ms 23892 KB Output is correct
44 Correct 316 ms 28752 KB Output is correct
45 Correct 316 ms 28752 KB Output is correct
46 Correct 330 ms 24560 KB Output is correct
47 Correct 302 ms 24976 KB Output is correct
48 Correct 326 ms 25000 KB Output is correct
49 Correct 304 ms 27472 KB Output is correct
50 Correct 320 ms 29460 KB Output is correct
51 Correct 2 ms 10588 KB Output is correct
52 Correct 182 ms 16612 KB Output is correct
53 Correct 199 ms 16536 KB Output is correct
54 Correct 209 ms 16464 KB Output is correct
55 Correct 203 ms 16616 KB Output is correct
56 Correct 191 ms 16612 KB Output is correct
57 Correct 192 ms 16612 KB Output is correct
58 Correct 203 ms 19356 KB Output is correct
59 Correct 232 ms 19664 KB Output is correct
60 Correct 246 ms 18604 KB Output is correct
61 Correct 250 ms 18212 KB Output is correct
62 Correct 139 ms 29652 KB Output is correct
63 Correct 135 ms 29656 KB Output is correct
64 Correct 140 ms 29608 KB Output is correct
65 Correct 139 ms 29652 KB Output is correct
66 Correct 155 ms 23260 KB Output is correct
67 Correct 145 ms 23120 KB Output is correct
68 Correct 141 ms 23120 KB Output is correct
69 Correct 159 ms 23256 KB Output is correct
70 Correct 144 ms 23164 KB Output is correct
71 Correct 144 ms 22964 KB Output is correct
72 Correct 152 ms 23160 KB Output is correct
73 Correct 144 ms 22508 KB Output is correct
74 Correct 142 ms 23256 KB Output is correct
75 Correct 145 ms 23160 KB Output is correct
76 Correct 217 ms 21784 KB Output is correct
77 Correct 207 ms 20052 KB Output is correct
78 Correct 312 ms 24916 KB Output is correct
79 Correct 302 ms 23328 KB Output is correct
80 Correct 305 ms 29008 KB Output is correct
81 Correct 303 ms 26256 KB Output is correct
82 Correct 305 ms 26192 KB Output is correct
83 Correct 306 ms 23652 KB Output is correct
84 Correct 335 ms 28576 KB Output is correct
85 Correct 304 ms 27112 KB Output is correct
86 Correct 293 ms 23188 KB Output is correct
87 Correct 297 ms 24640 KB Output is correct
88 Correct 283 ms 25632 KB Output is correct
89 Correct 312 ms 22432 KB Output is correct
90 Correct 279 ms 22324 KB Output is correct
91 Correct 285 ms 24208 KB Output is correct
92 Correct 290 ms 22984 KB Output is correct
93 Correct 269 ms 22676 KB Output is correct
94 Correct 275 ms 22476 KB Output is correct
95 Correct 285 ms 23552 KB Output is correct
96 Correct 274 ms 22292 KB Output is correct
97 Correct 274 ms 24092 KB Output is correct