This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <assert.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) {
for (int i = 0; i < eo[u]; ++i) if (eh[u][i] != par[u]) {
efs(eh[u][i]);
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];
if (par[cur] == u)
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] - 1];
--eo[i];
break;
}
}
for (int i = 1; i <= n; ++i)
for (int k = 0; k < eo[i]; ++k) {
assert(par[i] != eh[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);
printf("%lld\n", answer - dp[1][(1 << eo[1]) - 1]);
}
Compilation message (stderr)
training.c: In function 'pus_':
training.c:19:26: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | else if (0 == (o & o - 1)) eh[0] = (int*)realloc(eh[0], 2 * o * sizeof**eh);
| ~~^~~
training.c: In function 'main':
training.c:82:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
training.c:84:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d%d%d", &u, &v, &c);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |