#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int MAX = 5005;
vector<int> G[MAX], T[MAX];
pii E[MAX];
int P[MAX], D[MAX], C[MAX], dp[MAX], temp[1 << 10];
int in[MAX], out[MAX], dn;
void DFS(int cur, int prev) {
if (prev) G[cur].erase(find(G[cur].begin(), G[cur].end(), prev));
in[cur] = ++dn;
D[cur] = D[prev] + 1;
P[cur] = prev;
for (int next : G[cur]) {
if (next == prev) continue;
DFS(next, cur);
}
out[cur] = dn;
}
int LCA(int a, int b) {
if (D[a] > D[b]) swap(a, b);
while (D[a] != D[b]) b = P[b];
while (a != b) a = P[a], b = P[b];
return a;
}
int Find(int cur, int child) {
for (int i = 0; i < G[cur].size(); ++i) {
if (in[cur] <= in[child] && in[child] <= out[cur]) return (1 << i);
}
}
void DFS2(int cur) {
for (int next : G[cur]) DFS2(next);
vector<pii> cost;
for (int i = 0; i < G[cur].size(); ++i) {
cost.emplace_back(1 << i, dp[G[cur][i]]);
}
for (int en : T[cur]) {
auto [a, b] = E[en];
int bit = 0, v = C[en];
if (a != cur) bit |= Find(cur, a), v += dp[a];
if (b != cur) bit |= Find(cur, b), v += dp[b];
cost.emplace_back(bit, v);
}
int Z = (1 << G[cur].size());
memset(temp, 0, sizeof(temp));
for (int i = 0; i < Z; ++i) {
for (auto [b, n] : cost) {
if (i & b) continue;
temp[i | b] = max(temp[i | b], temp[i] + n);
}
}
dp[cur] = *max_element(temp, temp + 1024);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M, ans = 0, remain = 1;
cin >> N >> M;
for (int i = 1; i <= M; ++i) {
int a, b, c;
cin >> a >> b >> c;
if (!c) {
G[a].push_back(b);
G[b].push_back(a);
}
else {
E[remain] = {a, b};
C[remain++] = c;
ans += c;
}
}
DFS(1, 0);
for (int i = 1; i <= M - N + 1; ++i) {
auto [a, b] = E[i];
if (abs(D[a] - D[b]) & 1) continue;
int c = LCA(a, b);
T[c].push_back(i);
}
DFS2(1);
ans -= dp[1];
cout << ans;
return 0;
}
Compilation message
training.cpp: In function 'int Find(int, int)':
training.cpp:31:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < G[cur].size(); ++i) {
| ~~^~~~~~~~~~~~~~~
training.cpp: In function 'void DFS2(int)':
training.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < G[cur].size(); ++i) {
| ~~^~~~~~~~~~~~~~~
training.cpp: In function 'int Find(int, int)':
training.cpp:34:1: warning: control reaches end of non-void function [-Wreturn-type]
34 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |