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 all(a) a.begin(),a.end()
#define mp make_pair
#define fi first
#define se second
const int maxn = (int)1e4+10;
int n, num=0;
int a[maxn], b[maxn], c[maxn], deg[maxn];
vector<int> adj[maxn];
bool vis[maxn];
void dfs(int s){
vis[s]=1; num++;
for(auto u : adj[s])
if(!vis[u]) dfs(u);
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; int ans = 0;
for(int i = 0; i < n-1; i++) cin >> a[i] >> b[i] >> c[i];
for(int mask = 0; mask < (1<<(n-1)); mask++){
int sum = 0, ok=1;
for(int i = 1; i <= n; i++) adj[i].clear(), vis[i]=false, deg[i]=0;
for(int i = 0; i < n-1; i++) sum+=!!(mask&(1<<i))*c[i];
for(int i = 0; i < n-1; i++)
if(mask&(1<<i)) deg[a[i]]++, deg[b[i]]++, adj[a[i]].pb(b[i]);
for(int i = 1; i <= n; i++){
if(deg[i]>3) ok=0; num=0;
if(!vis[i]) dfs(i), ok&=(num%2==1);
}
if(ok) ans = max(ans, sum);
}
cout << ans;
}
Compilation message (stderr)
beads.cpp: In function 'int32_t main()':
beads.cpp:34:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
34 | if(deg[i]>3) ok=0; num=0;
| ^~
beads.cpp:34:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
34 | if(deg[i]>3) ok=0; num=0;
| ^~~
# | 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... |