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 int long long
#define pb push_back
#define ld double
#define ll __int128
mt19937 rnd(51);
struct edge {
int a, b, c;
};
const int N = 2e5 + 10, INF = 1e18;
vector<pair<int,int>> g[N];
int res[N][2];
bool bad = 0;
void dfs(int v, int p) {
vector<int> dp(3, -INF);
dp[0] = 0;
for (auto [to, c] : g[v]) {
if (to != p) {
dfs(to, v);
vector<int> dpp(3);
for (int j = 0; j < 2; j++) {
dpp[j + 1] = max(dpp[j + 1], dp[j] + res[to][0] + c);
}
for (int j = 0; j < 3; j++) {
dpp[j] = max(dpp[j], dp[j] + max(res[to][1] + c, res[to][0]));
}
swap(dp, dpp);
}
}
res[v][0] = max(dp[0], dp[2]);
res[v][1] = dp[1];
}
int solve(int n, vector<edge> u) {
for (int i = 0; i < n; i++) {
g[i].clear();
res[i][0] = res[i][1] = 0;
}
for (auto to : u) {
g[to.a].pb({to.b, to.c});
g[to.b].pb({to.a, to.c});
}
dfs(0, -1);
return res[0][0];
}
int zhfs(int v, int p) {
int cnt = 0;
for (auto to : g[v]) {
if (to.first != p) {
int val = zhfs(to.first, v);
if (val == 1) {
if (to.second == 0) {
bad = 1;
}
} else {
cnt += to.second;
}
}
}
if (cnt > 3) bad = 1;
return cnt % 2;
}
int solvee(int n, vector<edge> u) {
n--;
int ans = 0;
for (int i = 0; i < (1 << n); i++) {
int res = 0;
for (int j = 0; j < n + 1; j++) g[j].clear();
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
res += u[j].c;
g[u[j].a].pb({u[j].b, 1});
g[u[j].b].pb({u[j].a, 1});
} else {
g[u[j].a].pb({u[j].b, 0});
g[u[j].b].pb({u[j].a, 0});
}
}
bad = 0;
int val = zhfs(0, -1);
if (val == 1) bad = 1;
if (!bad) {
ans = max(ans, res);
}
}
return ans;
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<edge> u(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> u[i].a >> u[i].b >> u[i].c;
u[i].a--, u[i].b--;
}
cout << solvee(n, u) << endl;
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:24:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
24 | for (auto [to, c] : g[v]) {
| ^
# | 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... |