This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) (int)a.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pii> rid = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<pii> dir = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
const int N = 1e6 + 1, mod = 1e9 + 7;
const ll inf = 2e9;
double eps = 1e-15;
bool flg = 0;
int was[N], ok = 1;
vector<int> g[N];
int dfs(int u, int p) {
int cnt = (u != p);
was[u] = 1;
for (int to: g[u]) {
if (to == p) continue;
cnt += dfs(to, u);
}
if (cnt > 3) ok = 0;
return (cnt & 1);
}
void slv() {
int n;
cin >> n;
int u[n], v[n], w[n];
for (int i = 0; i < n - 1; i++) {
cin >> u[i] >> v[i] >> w[i];
}
int mx = 0;
for (int msk = 0; msk < (1 << (n - 1)); msk++) {
int sum = 0;
for (int i = 0; i < n - 1; i++) {
if (msk >> i & 1) {
g[u[i]].push_back(v[i]);
g[v[i]].push_back(u[i]);
sum += w[i];
}
}
ok = 1;
for (int i = 1; i <= n; i++) {
if (!was[i] && dfs(i, i)) ok = 0;
}
for (int i = 1; i <= n; i++) {
g[i].clear();
was[i] = 0;
}
if (ok) mx = max(mx, sum);
}
cout << mx;
}
main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int tp = 1;
if (flg) cin >> tp;
while (tp--) {
slv();
}
}
//wenomechainsama
Compilation message (stderr)
beads.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
75 | main() {
| ^~~~
# | 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... |