#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
const int MOD = 1e9 + 7;
const int MX = 1010;
const ll INF = 1e18;
int N, M; vi adj[MX]; vector<array<int, 3>> edges, LCA[MX];
int depth[MX]; pi par[MX]; int DP[MX][(1 << 12)]; int ans = 0;
/* DFS and LCA - *We can use non-optimal LCA because of low constraints* */
void DFS(int X, int P) {
int curDeg = 0; EACH(Y, adj[X]) if (Y != P) {
depth[Y] = depth[X] + 1; par[Y] = {X, (1 << (curDeg++))}; DFS(Y, X);
}
}
int getLCA(int A, int B) {
if (depth[A] < depth[B]) swap(A, B);
while (depth[A] > depth[B]) A = par[A].f; while (A != B) A = par[A].f, B = par[B].f; return A;
}
/* DP[i][msk] = Maximum cost in subtree i such that no even cycles are induced * 0's are available * */
void solve(int X) {
EACH(Y, adj[X]) if (Y != par[X].f) { solve(Y); DP[X][0] += DP[Y][0]; }
EACH(E, LCA[X]) {
if ((depth[E[0]] + depth[E[1]]) % 2 == 1) continue;
pi A = {E[0], 0}; pi B = {E[1], 0}; int tot = E[2];
while (A.f != X) { tot += DP[A.f][A.s]; A = par[A.f]; }
while (B.f != X) { tot += DP[B.f][B.s]; B = par[B.f]; }
F0R(msk, (1 << 12)) if (!((msk & A.s) || (msk & B.s))) {
ckmax(DP[X][msk], DP[X][msk | A.s | B.s] + tot);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M; F0R(i, M) {
int A, B, C; cin >> A >> B >> C; ans += C;
if (!C) adj[A].pb(B), adj[B].pb(A); else edges.pb({A, B, C});
}
DFS(1, 0); EACH(E, edges) LCA[getLCA(E[0], E[1])].pb(E); solve(1); cout << ans - DP[1][0];
}
Compilation message
training.cpp: In function 'int getLCA(int, int)':
training.cpp:73:5: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
73 | while (depth[A] > depth[B]) A = par[A].f; while (A != B) A = par[A].f, B = par[B].f; return A;
| ^~~~~
training.cpp:73:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
73 | while (depth[A] > depth[B]) A = par[A].f; while (A != B) A = par[A].f, B = par[B].f; return A;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
972 KB |
Output is correct |
2 |
Correct |
2 ms |
1008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
9220 KB |
Output is correct |
2 |
Correct |
17 ms |
11212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
2500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
4812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
2732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |