#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
struct Road {
int a, b, c, lca;
};
vector<int> tree[5001];
vector<Road> roads;
pair<int, int> par[5001];
bool even_to_root[5001];
int dp[5001][1 << 10], timer = 0, tin[5001], tout[5001], deg[5001];
bool operator<(Road A, Road B) {
return tout[A.lca] < tout[B.lca];
}
void get_ancestors(int node = 1, int parent = 0) {
even_to_root[node] = !even_to_root[parent];
tin[node] = ++timer;
for (int i : tree[node]) {
if (i != parent) {
par[i] = {node, 1 << deg[node]++};
get_ancestors(i, node);
}
}
tout[node] = ++timer;
}
bool is_parent(int A, int B) {
return tin[A] <= tin[B] && tout[A] >= tout[B];
}
int lca(int A, int B) {
while (!is_parent(A, B)) A = par[A].first;
return A;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, cost = 0;
cin >> n >> m;
FOR(i, 0, m) {
int a, b, c;
cin >> a >> b >> c;
cost += c;
if (!c) {
tree[a].push_back(b);
tree[b].push_back(a);
}
roads.push_back({a, b, c});
}
get_ancestors();
FOR(i, 0, m) roads[i].lca = lca(roads[i].a, roads[i].b);
sort(roads.begin(), roads.end());
for (Road i : roads) {
if (i.c && even_to_root[i.a] ^ even_to_root[i.b]) continue;
int sm = i.c;
pair<int, int> A, B;
for (A = {i.a, 0}; A.first != i.lca; A = par[A.first]) sm += dp[A.first][A.second];
for (B = {i.b, 0}; B.first != i.lca; B = par[B.first]) sm += dp[B.first][B.second];
for (int mask = (1 << deg[i.lca]) - 1; ~mask; mask--) {
if (!(mask & A.second || mask & B.second)) {
dp[i.lca][mask] = max(dp[i.lca][mask], sm + dp[i.lca][mask | A.second | B.second]);
}
}
}
cout << cost - dp[1][0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
4728 KB |
Output is correct |
2 |
Correct |
10 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1144 KB |
Output is correct |
2 |
Correct |
4 ms |
1016 KB |
Output is correct |
3 |
Correct |
6 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2296 KB |
Output is correct |
2 |
Correct |
8 ms |
1272 KB |
Output is correct |
3 |
Correct |
6 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
760 KB |
Output is correct |
2 |
Correct |
5 ms |
1784 KB |
Output is correct |
3 |
Correct |
17 ms |
4700 KB |
Output is correct |
4 |
Correct |
6 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
16 ms |
4648 KB |
Output is correct |
3 |
Correct |
9 ms |
1780 KB |
Output is correct |
4 |
Correct |
8 ms |
1528 KB |
Output is correct |