#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(abs(a), abs(b));}
ll lcm(ll a, ll b){return abs(a) / gcd(a, b) * abs(b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1e3 + 69;
int n, m;
int h[N], parent[N];
vector<int> graph[N];
vector<int> child[N];
void dfs(int u, int p){
h[u] = h[p] + 1;
parent[u] = p;
for(int v: graph[u]) if (v != p){
child[u].push_back(v);
dfs(v, u);
}
remove_dup(child[u]);
}
vector<array<int, 3>> bruh[N];
int LCA(int u, int v){
if (h[u] < h[v]) swap(u, v);
while(h[u] > h[v]) u = parent[u];
if (u == v) return u;
while(u != v){
u = parent[u];
v = parent[v];
}
return u;
}
vector<int> dp[N];
void walk_up(int u, int &x, int &ans){
while(parent[x] != u){
int _x = parent[x];
int idx = lower_bound(ALL(child[_x]), x) - child[_x].begin();
ans += dp[_x][idx];
x = parent[x];
}
}
void go(int u){
for(int v: child[u]) {
go(v);
}
int deg = child[u].size();
if (deg == 0){
dp[u].push_back(0);
return;
}
int ma[deg][deg]; memset(ma, 0, sizeof ma);
for(auto i: bruh[u]){
int x = i[0], y = i[1], w = i[2];
if (h[x] > h[y]) swap(x, y);
if (x == u){
w += dp[y].back();
walk_up(u, y, w);
int idx = lower_bound(ALL(child[u]), y) - child[u].begin();
maximize(ma[idx][idx], w);
}
else{
w += dp[x].back() + dp[y].back();
walk_up(u, x, w);
walk_up(u, y, w);
int idx1 = lower_bound(ALL(child[u]), x) - child[u].begin();
int idx2 = lower_bound(ALL(child[u]), y) - child[u].begin();
if (idx1 > idx2) swap(idx1, idx2);
maximize(ma[idx1][idx2], w);
}
}
for(int i = 0; i < deg; ++i){
maximize(ma[i][i], dp[child[u][i]].back());
}
dp[u].resize(deg + 1);
int sos[MASK(deg)]; memset(sos, 0, sizeof sos);
for(int i = 0; i < deg; ++i) for(int j = i; j < deg; ++j) if (ma[i][j] > 0){
int cur_mask = MASK(i) | MASK(j);
for(int mask = 0; mask < (int)MASK(deg); ++mask) if ((mask & cur_mask) == 0){
maximize(sos[mask + cur_mask], sos[mask] + ma[i][j]);
}
}
for(int mask = 0; mask < (int)MASK(deg); ++mask){
maximize(dp[u].back(), sos[mask]);
for(int i = 0; i < deg; ++i) if (!GETBIT(mask, i))
maximize(dp[u][i], sos[mask]);
}
}
void solve(){
cin >> n >> m;
vector<array<int, 3>> arr;
ll sum = 0;
for(int i= 0; i < m; ++i){
int u, v, w; cin >> u >> v >> w;
sum += w;
if (w == 0) {
graph[u].push_back(v);
graph[v].push_back(u);
}
else arr.push_back({{u, v, w}});
}
dfs(1, 0);
for(auto i: arr) if ((h[i[0]] + h[i[1]]) % 2 == 0){
int lck = LCA(i[0], i[1]);
bruh[lck].push_back(i);
}
go(1);
cout << sum - dp[1].back() << "\n";
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 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... |