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 <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
struct R {
int a, b, c, lca;
};
vector<int> t[5001];
vector<R> r;
pair<int, int> p[5001];
bool e[5001];
int dp[5001][1 << 10], ti = 0, tin[5001], tout[5001], dg[5001];
bool operator<(R A, R B) {
return tout[A.lca] < tout[B.lca];
}
void d(int u = 1, int parent = 0) {
e[u] = !e[parent];
tin[u] = ++ti;
for (int i : t[u]) {
if (i != parent) {
p[i] = {u, 1 << dg[u]++};
d(i, u);
}
}
tout[u] = ++ti;
}
bool par(int A, int B) {
return tin[A] <= tin[B] && tout[A] >= tout[B];
}
int lca(int A, int B) {
while (!par(A, B)) A = p[A].first;
return A;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, cost = 0;
cin >> n >> m;
FOR(i, 0, m) {
int a, b, c;
cin >> a >> b >> c;
cost += c;
if (!c) {
t[a].push_back(b);
t[b].push_back(a);
}
r.push_back({a, b, c});
}
d();
FOR(i, 0, m) r[i].lca = lca(r[i].a, r[i].b);
sort(r.begin(), r.end());
for (R i : r) {
if (i.c && e[i.a] ^ e[i.b]) continue;
int sm = i.c;
pair<int, int> A, B;
for (A = {i.a, 0}; A.first != i.lca; A = p[A.first]) sm += dp[A.first][A.second];
for (B = {i.b, 0}; B.first != i.lca; B = p[B.first]) sm += dp[B.first][B.second];
for (int mask = (1 << dg[i.lca]) - 1; ~mask; mask--) {
if (!(mask & A.second || mask & B.second)) {
dp[i.lca][mask] = max(dp[i.lca][mask], sm + dp[i.lca][mask | A.second | B.second]);
}
}
}
cout << cost - dp[1][0];
return 0;
}
# | 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... |