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 pii pair<int, int>
#define pII pair<pii, pii>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 1010, M = 5050, K = 12;
vector<int> adj[N];
int lvl[N], deg[N];
pii prt[N];
int up[N][K];
int dp[N][(1<<K)];
vector<pII> edge; //u, v, weight, lca
bool cmp(pII a, pII b) {
return lvl[a.nd.nd] > lvl[b.nd.nd];
}
void dfs(int a, int p) {
up[a][0] = p;
prt[a] = {p, (1<<(deg[p]++))};
lvl[a] = lvl[p] + 1;
for (int i = 1; i < K; ++i) {
up[a][i] = up[up[a][i-1]][i-1];
}
for (auto b : adj[a]) {
if (b == p) continue;
dfs(b, a);
}
}
int lca(int a, int b) {
if (lvl[a] < lvl[b]) swap(a, b);
int jump = lvl[a] - lvl[b];
for (int i = 0; i < K; ++i) {
if (jump & (1<<i)) a = up[a][i];
}
if (a==b) return a;
for (int i = K-1; i >= 0; --i) {
if (up[a][i] != up[b][i]) {
a = up[a][i], b = up[b][i];
}
}
return up[a][0];
}
void solve() {
for (auto i : edge) {
int a = i.st.st, b = i.st.nd, cur = i.nd.st, l = i.nd.nd;
if (lvl[a] % 2 != lvl[b] % 2) continue;
int mask1 = 0, mask2 = 0;
for (; a != l; tie(a, mask1) = prt[a]) cur += dp[a][mask1];
for (; b != l; tie(b, mask2) = prt[b]) cur += dp[b][mask2];
int mask3 = mask1 | mask2;
for (int i = 0; i < (1<<deg[l]); ++i) {
if (i & mask3) continue;
dp[l][i] = max(dp[l][i], cur + dp[l][i|mask3]);
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int n, m, sum = 0; cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, w; cin >> a >> b >> w;
if (!w) {
adj[a].pb(b);
adj[b].pb(a);
}
else edge.pb({{a, b}, {w, 0}});
sum += w;
}
dfs(1, 0);
for (auto &i : edge) i.nd.nd = lca(i.st.st, i.st.nd);
sort(edge.begin(), edge.end(), cmp);
solve();
cout << sum - dp[1][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... |