답안 #967122

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967122 2024-04-21T07:21:55 Z sleepntsheep 수도 (JOI20_capital_city) C
1 / 100
304 ms 28328 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], freq[K], 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;
    freq[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;

    dfs3(u, u);
    int col = color[u];
    if (dfreq[col] == cfreq[col])
    {
        int dcolor = 0;
        for (int at, jj = head_[col]; jj; jj = nxt_[jj])
        {
            at = vv_[jj];
            for (;; at = par[at])
            {
                visit[at] = 1;
                if (1 == ++freq[color[at]])
                    ++dcolor;
                if (at == u || visit[par[at]] == 1)
                    break;
            }
        }
        if (freq[col])
            --dcolor;
        if (dcolor < answer)
            answer = dcolor;
    }
    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 'main':
capital_city.c:111:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |     scanf("%d%d", &n, &k);
      |     ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:115:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |         scanf("%d%d", &u, &v); --u, --v;
      |         ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:121:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         scanf("%d", color+i);
      |         ^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Incorrect 2 ms 8540 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 262 ms 24388 KB Output is correct
2 Correct 151 ms 28244 KB Output is correct
3 Correct 234 ms 28060 KB Output is correct
4 Correct 152 ms 28328 KB Output is correct
5 Correct 241 ms 25680 KB Output is correct
6 Correct 154 ms 28244 KB Output is correct
7 Correct 237 ms 25936 KB Output is correct
8 Correct 163 ms 27988 KB Output is correct
9 Incorrect 304 ms 24960 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Incorrect 2 ms 8540 KB Output isn't correct
12 Halted 0 ms 0 KB -