답안 #1036540

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1036540 2024-07-27T13:42:46 Z sleepntsheep Training (IOI07_training) C
37 / 100
5 ms 10844 KB
#include <stdio.h>
#include <stdlib.h>

#define M 5005
#define N 1005

long long max(long long a, long long b) { return a > b ? a : b; }
void cmax(long long*a, long long b) { *a = max(*a, b); }


int n, m, m_, *eh[N], eo[N], dep[N];
int *fh[N], fo[N];
int id[N][N];

void pus_(int **eh, int *eo, int j) {
    int o = eo[0]++;
    if (0 == o) eh[0] = (int*)malloc(2 * sizeof **eh);
    else if (0 == (o & o - 1)) eh[0] = (int*)realloc(eh[0], 2 * o * sizeof**eh);
    eh[0][o] = j;
}
#define pus(eh, eo, j) pus_(&(eh), &(eo), j)

int rc[M], ru[M][2], par[N], temp;

void dfs(int u, int p) {
    par[u] = p;
    for (int v, j = 0; j < eo[u]; ++j) if ((v = eh[u][j]) != p) {
        dep[v] = dep[u] + 1;
        dfs(v, u);
    }
}

int lca(int u, int v) {
    while (dep[u] != dep[v])
        if (dep[u] > dep[v]) u = par[u];
        else v = par[v];
    while (u != v) u = par[u], v = par[v];
    return u;
}

long long dp[N][1024];

void efs(int u, int p) {
    for (int i = 0; i < eo[u]; ++i) if (eh[u][i] - p) {
        efs(eh[u][i], u);
        dp[u][1 << i] = dp[eh[u][i]][(1 << eo[eh[u][i]]) - 1];
    }

    for (int j = 0; j < fo[u]; ++j) {
        int e = fh[u][j];

        long long cost = rc[e];
        int mm = 0;
        for (int k = 0; k < 2; ++k) {
            int cur = ru[e][k], prev = -1;
            while (cur != u) {
                int mask = (1 << eo[cur]) - 1;
                if (prev != -1)
                    mask &= ~(1 << id[cur][prev]);
                cost += dp[cur][mask];

                mm |= 1 << id[u][cur];

                prev=cur;
                cur = par[cur];
            }
        }

        cmax(&dp[u][mm], cost);
    }

    for (int i = 0; i < (1 << eo[u]); ++i) {
        //forr (int j = 1; j < i; ++j)if((i|j)==i)
        for (int j = i; j > 0; j = (j - 1) & i)
            cmax(&dp[u][i], dp[u][j]+ dp[u][i ^ j]);
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int u, v, c, i = 0; i < m; ++i) {
        scanf("%d%d%d", &u, &v, &c);
        if (!c)
            pus(eh[u], eo[u], v), pus(eh[v], eo[v], u);
        else
            ru[m_][0] = u, ru[m_][1] = v, rc[m_] = c, ++m_;
    }

    par[1] = 1;
    dfs(1, 1);

    for (int i = 2; i <= n; ++i) {
        for (int j = 0; j < eo[i]; ++j) {
            if (eh[i][j] == par[i])
                eh[i][j] = eh[i][--eo[i]];
            break;
        }
    }

    for (int i = 1; i <= n; ++i)
        for (int k = 0; k < eo[i]; ++k)
            id[i][eh[i][k]] = k;

    long long answer = 0;
    for (int i = 0; i < m_; ++i) {
        int a = lca(ru[i][0], ru[i][1]);
        answer += rc[i];
        if ((dep[ru[i][0]] + dep[ru[i][1]] - 2 * dep[a]) % 2) {
        } else {
            pus(fh[a], fo[a], i);
        }
    }

    efs(1, 1);
    printf("%lld\n", answer - dp[1][(1 << eo[1]) - 1]);
}

Compilation message

training.c: In function 'pus_':
training.c:18:26: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   18 |     else if (0 == (o & o - 1)) eh[0] = (int*)realloc(eh[0], 2 * o * sizeof**eh);
      |                        ~~^~~
training.c: In function 'main':
training.c:80:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     scanf("%d%d", &n, &m);
      |     ^~~~~~~~~~~~~~~~~~~~~
training.c:82:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         scanf("%d%d%d", &u, &v, &c);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 0 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10840 KB Output is correct
2 Correct 5 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 7004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 8028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5088 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9564 KB Output isn't correct
2 Halted 0 ms 0 KB -