This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define to first
#define len second
typedef long long ll;
typedef pair<ll,ll> pii;
const int MN = 11;
void chmx(ll& a, ll b) {if (a<b) a=b;}
struct Edge {
int from, to;
ll len;
Edge() {
from = to = len = 0;
}
Edge(int _from, int _to, ll _len) {
from = _from;
to = _to;
len = _len;
}
inline const bool operator<(const Edge& other) const {
if (len != other.len) {
return len < other.len;
}
if (from != other.from) {
return from < other.from;
}
return to < other.to;
}
inline const bool operator>(const Edge& other) const {
return other < *this;
}
};
int N;
set<Edge> adj[MN];
bool leaf[MN];
int dfs(int node, int par) {
int ret = 1;
for (auto it=adj[node].begin(); it!=adj[node].end(); ++it) {
if (it->to == par) continue;
ret += dfs(it->to, node);
}
return ret;
}
bool works() {
for (int i=0; i<N; ++i) {
leaf[i] = adj[i].size() == 1;
}
bool two = false;
for (int i=0; i<N; ++i) {
int leaf_ct = 0;
for (auto it=adj[i].begin(); it!=adj[i].end(); ++it ){
if (leaf[it->to]) {
++leaf_ct;
}
}
if (leaf_ct >= 3) return false;
if (leaf_ct >= 2) {
if (two) return false;
two = true;
}
if ((dfs(i, -1) & 1) == 0) return false;
}
return true;
}
Edge edge[MN];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> N;
for (int i=0; i<N-1; ++i) {
cin >> edge[i].from >> edge[i].to >> edge[i].len;
--edge[i].from;
--edge[i].to;
}
ll best = 0;
for (int i=0; i<1<<(N-1); ++i) {
for (int j=0; j<N; ++j) {
adj[j].clear();
}
int edge_ct = 0;
ll total = 0;
for (int j=0; j<N-1; ++j) {
if ((i >> j) & 1) {
ll a = edge[j].from, b = edge[j].to;
adj[a].insert(edge[j]);
adj[b].emplace(b, a, edge[j].len);
++edge_ct;
total += edge[j].len;
}
}
if (edge_ct & 1) continue;
if (works()) {
best = max(best, total);
}
}
cout << best << endl;
}
/*
5
1 2 10
1 3 40
1 4 15
1 5 20
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
# | 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... |