# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
886291 | DrAymeinstein | Training (IOI07_training) | C++17 | 0 ms | 0 KiB |
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 <utility>
#include <iostream>
#include <vector>
#include <string>
#define N 1005
#define BIT(x,i) ((x>>i)&1)
using namespace std;
vector <int> a[N];
class edge {
public:
int u, v, k;
};
vector <edge> c[N], add;
long long n, m, i, u, v, sum, f[N], g[N], p[N], h[N], k, num[N][N], w[11], w2[11][11], dp[N][11][1024], nchild[N];
void dfs(int u)
{
for (int i = 0; i < a[u].size(); i++)
{
int v = a[u][i];
if (v != p[u]) h[v] = h[u] + 1, p[v] = u, dfs(v);
}
}
void opt(long long& a, long long b) {
a = max(a, b);
}
int LCA(int u, int v) {
while (h[u] > h[v]) {
u = p[u];
}
while (h[v] > h[u]) {
v = p[v];
}
while (u != v) {
u = p[u], v = p[v];
}
return u;
}
int nearest(int u, int v) {
while (p[u] != v) {
u = p[u];
}
return u;
}
long long climb(int u, int v) {
long long res = 0, last;
res += f[u]; last = u; u = p[u];
while (u != v) {
res += dp[u][nchild[u] - 1][((1 << nchild[u]) - 1) ^ (1 << num[u][last])];
last = u; u = p[u];
}
return res;
}
void DFS(int u) {
int i, j, tt;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i];
if (v != p[u]) {
DFS(v);
}
}
for (int v : a[u]) {
if (v != p[u]) {
g[nchild[u]] = f[v], num[u][v] = nchild[u]++;
}
}
memset(w, 0, sizeof(w));
memset(w2, 0, sizeof(w2));
for (int i = 0; i < c[u].size(); i++) {
edge x = c[u][i];
int v1 = x.u, v2 = x.v, k = x.k, tg1, tg2;
long long sum, sum1, sum2;
if (v2 == u) {
tg1 = num[u][nearest(v1, u)];
opt(w[tg1], climb(v1, u) + k);
} else {
tg1 = num[u][nearest(v1, u)]; sum1 = climb(v1, u);
tg2 = num[u][nearest(v2, u)]; sum2 = climb(v2, u);
opt(w2[tg1][tg2], sum1 + sum2 + k);
opt(w2[tg2][tg1], sum1 + sum2 + k);
}
}
if (nchild[u] == 0) return;
dp[u][0][0] = 0;
dp[u][0][1] = max(w[0], g[0]);
for (i = 1; i < nchild[u]; i++) {
for (tt = 0; tt < (1 << i); tt++) {
opt(dp[u][i][tt], dp[u][i - 1][tt]);
opt(dp[u][i][tt | (1 << i)], dp[u][i - 1][tt] + max(g[i], w[i]));
for (j = 0; j < i; j++)
if (BIT(tt, j) == 0) {
opt(dp[u][i][(tt | (1 << i)) | (1 << j)], dp[u][i - 1][tt] + max(w2[i][j], g[i] + g[j]));
}
}
}
f[u] = dp[u][nchild[u] - 1][(1 << nchild[u]) - 1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = m; i >= 1; i--) {
cin >> u >> v >> k;
edge tg = { u,v,k };
if (k == 0) a[u].push_back(v), a[v].push_back(u);
else add.push_back(tg), sum += k;
}
dfs(1);
for (i = 0; i < add.size(); i++) {
edge x = add[i];
int u = x.u, v = x.v, k = x.k, p = LCA(u, v);
if (h[u] < h[v]) swap(u, v);
if ((h[u] + h[v] - 2 * h[p]) & 1) continue;
edge tg = {
u,v,k
};
c[p].push_back(tg);
}
DFS(1);
cout << sum - f[1];
}