This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// from duckindog wth depression
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 10 + 10;
int n;
pair<int, int> ed[N];
vector<pii> ad[N];
bool mk[N], dd[N];
pair<int, int> dfs(int u, int pre = 0) {
dd[u] = 1;
int ret = 0, cnt = 0;
for (auto duck : ad[u]) {
int v, w; tie(v, w) = duck;
if (v == pre) continue;
if (!mk[v]) continue;
pii nret = dfs(v, u);
ret += nret.first + w;
cnt += nret.second + 1;
}
return {ret, cnt};
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
cin >> n;
int answer = 0;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
ad[u].push_back({v, w});
ad[v].push_back({u, w});
ed[i] = {u, v};
}
for (int mask = 1; mask < (1 << n - 1) - 1; ++mask) {
memset(mk, 0, sizeof mk);
memset(dd, 0, sizeof dd);
int cnt = 0;
for (int i = 1; i < n; ++i) {
if (mask >> i - 1 & 1) {
mk[ed[i].first] = mk[ed[i].second] = 1;
cnt += 1;
}
}
if (cnt & 1) continue;
int value = 0;
for (int i = 1; i <= n; ++i) {
if (mk[i] && !dd[i]) {
pii val = dfs(i);
if (val.second & 1) {
value = -1;
break;
}
value += val.first;
}
}
answer = max(answer, value);
}
cout << answer;
}
Compilation message (stderr)
beads.cpp: In function 'int32_t main()':
beads.cpp:47:37: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
47 | for (int mask = 1; mask < (1 << n - 1) - 1; ++mask) {
| ~~^~~
beads.cpp:52:21: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
52 | if (mask >> i - 1 & 1) {
| ~~^~~
beads.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("duck.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen("duck.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |