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>
using namespace std;
#define x first
#define y second
const int DIM = 1e3 + 5;
const int P = (1 << 10) - 1;
struct drum {
int x, y, c;
};
int n, m;
int h[DIM], id[DIM];
int dp[DIM][P];
pair <int, int> tt[DIM];
bool viz[DIM];
int mark[DIM];
vector <int> arb[DIM];
vector <drum> v[DIM];
vector <int> l;
int lca(int x, int y) {
while (x != y) {
if (h[x] > h[y]) x = tt[x].first;
else y = tt[y].first;
}
return x;
}
void dfs(int nod = 1, int papa = 0) {
id[nod] = 1;
for (auto it : arb[nod]) {
if (it == papa) continue ;
tt[it] = {nod, id[nod]};
id[nod] *= 2;
h[it] = h[nod] + 1;
dfs(it, nod);
}
}
void get_dp(int now, int nod, int &ad, int &lant) {
int wh = 0;
while (now != nod) {
ad = ad + dp[now][wh];
wh = tt[now].second;
now = tt[now].first;
}
lant |= wh;
}
void solve(int nod = 1, int papa = 0) {
int p = id[nod] * 2 - 1;
for (auto it : arb[nod]) {
if (it == papa) continue ;
solve(it, nod);
for (int mask = 0; mask <= p ; ++mask) {
if (mask & tt[it].second)
dp[nod][mask ^ tt[it].second] = max(dp[nod][mask ^ tt[it].second], dp[nod][mask] + dp[it][0]);
}
}
for (auto it : v[nod]) {
int ad = it.c, lant = 0;
get_dp(it.x, nod, ad, lant);
get_dp(it.y, nod, ad, lant);
for (int mask = 0; mask <= p ; ++mask) {
if ((mask & lant) == lant)
dp[nod][mask ^ lant] = max(dp[nod][mask ^ lant], dp[nod][mask] + ad);
}
}
}
int main() {
scanf("%d%d", &n, &m);
int x, y, c;
int sum = 0;
vector <tuple<int, int, int>> edges;
for (int i = 1; i <= m ; ++i) {
scanf("%d%d%d", &x, &y, &c);
sum += c;
if (c == 0) {
arb[x].push_back(y);
arb[y].push_back(x);
} else {
edges.push_back({x, y, c});
}
}
dfs();
for (auto it : edges) {
x = get<0>(it);
y = get<1>(it);
c = get<2>(it);
int z = lca(x, y);
if ((h[x] - h[z] + h[y] - h[z] + 1) % 2 == 0) continue ;
v[z].push_back({x, y, c});
}
solve();
printf("%d", sum - dp[1][0]);
return 0;
}
Compilation message (stderr)
training.cpp: In function 'int main()':
training.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
82 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
training.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
90 | scanf("%d%d%d", &x, &y, &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... |