Submission #235831

#TimeUsernameProblemLanguageResultExecution timeMemory
235831balbitBeads and wires (APIO14_beads)C++14
0 / 100
37 ms5120 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define int ll #define pii pair<int, int> #define f first #define s second #define REP(i,n) for (int i = 0; i<n; ++i) #ifdef BALBIT #define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__) template<typename T> void _do(T && x){cerr<<x<<endl;} template<typename T, typename ...S> void _do(T && x, S&&...y){cerr<<x<<", "; _do(y...);} #define IOS() #else #define IOS() ios::sync_with_stdio(0), cin.tie(0) #define endl '\n' #define bug(...) #endif // BALBIT #define SZ(x) (int)(x.size()) #define ALL(x) (x).begin(), (x).end() #define pb push_back const int maxn = 2e5+5; vector<pii> g[maxn]; ll dp[maxn][2]; void dfs(int v, int p, ll w) { vector<ll> mo; ll sg = 0; for (pii u : g[v]) { if (u.f != p) { dfs(u.f,v,u.s); mo.pb(dp[u.f][0] - dp[u.f][1] + u.s); sg += dp[u.f][1]; } } sort(ALL(mo), greater<ll>()); dp[v][0] = sg; if (SZ(mo) >= 2) { dp[v][0] = max(dp[v][0], sg + mo[0] + mo[1]); } dp[v][1] = dp[v][0]; if (SZ(mo) >= 1) { dp[v][1] = max(dp[v][1], sg + mo[0] + w); } } signed main(){ IOS(); int n; cin>>n; if (n <= 2) assert(0); vector<pair<pii, int> > v; for (int i = 0; i<n-1; ++i) { int a,b,w; cin>>a>>b>>w; v.pb({{a,b},w}); --a;--b;g[a].pb({b,w});g[b].pb({a,w}); } sort(ALL(v)); ll re = 0; do{ vector<int> done(n); bool ok = 1; ll s = 0; for (int i = 0; i<SZ(v); i+=2){ if (v[i].f.f == v[i+1].f.f || v[i].f.f == v[i+1].f.s) { if (!done[v[i].f.f]) s += v[i].s + v[i+1].s; done[v[i].f.f] = 1; }else{ if (v[i].f.s == v[i+1].f.f || v[i].f.s == v[i+1].f.s) { if (!done[v[i].f.s]) s += v[i].s + v[i+1].s; done[v[i].f.s] = 1; } } } re = max(re, s); }while (next_permutation(ALL(v))); cout<<re<<endl; // dfs(0,-1,0); // cout<<dp[0][0]<<endl; } /* 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 */

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:68:14: warning: unused variable 'ok' [-Wunused-variable]
         bool ok = 1;
              ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...