#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
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 |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4608 KB |
Output is correct |
2 |
Correct |
8 ms |
4608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2176 KB |
Output is correct |
2 |
Correct |
8 ms |
1408 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
896 KB |
Output is correct |
2 |
Correct |
3 ms |
1664 KB |
Output is correct |
3 |
Correct |
16 ms |
4480 KB |
Output is correct |
4 |
Correct |
4 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2176 KB |
Output is correct |
2 |
Correct |
14 ms |
4480 KB |
Output is correct |
3 |
Correct |
8 ms |
1792 KB |
Output is correct |
4 |
Correct |
8 ms |
1536 KB |
Output is correct |