//Challenge: Accepted
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary (T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#define ll long long
#define maxn 1005
#define maxc 10
#define maxm 5005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn], pnt[maxn], path[maxn];
pii ed[maxm], chi[maxm];
int w[maxm], pa[maxn], dep[maxn];
void dfs(int n, int par, int d) {
dep[n] = d;
pa[n] = par;
for (int v:adj[n]) {
if (v != par) {
dfs(v, n, d + 1);
}
}
}
int ans = 0;
int tot, dp[maxn][maxm], id[maxn];
void solve(int n, int par) {
int d = 0;
for (int v:adj[n]) {
if (v != par) {
solve(v, n);
id[v] = d++;
}
}
for (int i:pnt[n]) chi[i] = {0, 0};
for (int v:adj[n]) {
for (int j:path[v]) {
if (!chi[j].ff) chi[j].ff = v;
else chi[j].ss = v;
}
}
vector<int> bit(1<<d, 0);
for (int i:pnt[n]) {
int se = (chi[i].ff ? (1<<id[chi[i].ff]) : 0) + (chi[i].ss ? 1<<id[chi[i].ss] : 0);
//debug(i, chi[i].ff, chi[i].ss, se);
int val = (chi[i].ff ? dp[chi[i].ff][i] : 0) + (chi[i].ss ? dp[chi[i].ss][i] : 0);
for (int j = 0;j < (1<<d);j++) {
if ((j & se) == 0) {
bit[j | se] = max(bit[j | se], bit[j] + w[i] + val);
}
}
}
for (int i = 1;i < (1<<d);i++) {
for (int j = 0;j < d;j++) {
if (i & (1<<j)) bit[i] = max(bit[i], bit[i ^ (1<<j)]);
}
}
ans = max(ans, bit[(1<<d) - 1]);
for (int v:adj[n]) {
for (int i:path[v]) {
dp[n][i] = bit[(1<<d) - 1 - (1<<id[v])];
}
}
//debug(n);
//pary(bit.begin(), bit.end());
//debug(n, dp[n][7]);
}
int main() {
io
int n, m;
cin >> n >> m;
tot = m;
int tc = 0;
for (int i = 1;i <= m;i++) {
int u, v;
cin >> u >> v >> w[i];
tc += w[i];
if (w[i] == 0) {
adj[u].push_back(v);
adj[v].push_back(u);
} else {
ed[i] = {u, v};
}
}
dfs(1, 0, 0);
int even = 0;
for (int i = 1;i <= m;i++) {
if (w[i]) {
auto [u, v] = ed[i];
if (dep[u] < dep[v]) swap(u, v);
int len = 0;
while (dep[u] > dep[v]) len++, path[u].push_back(i), u = pa[u];
while (u != v) {
path[u].push_back(i), path[v].push_back(i);
len += 2;
u = pa[u], v = pa[v];
}
if (len % 2 == 0) pnt[u].push_back(i);
else even += w[i], tc -= w[i];
}
}
/*
for (int i = 1;i <= n;i++) {
debug("path", i);
pary(path[i].begin(), path[i].end());
debug("pnt");
pary(pnt[i].begin(), pnt[i].end());
}
*/
solve(1, 0);
//debug(tc, even, ans);
cout << tc - ans + even << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
19852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
20480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
6144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
22532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |