Submission #774620

# Submission time Handle Problem Language Result Execution time Memory
774620 2023-07-05T23:19:10 Z TAMREF Beads and wires (APIO14_beads) C++17
0 / 100
3 ms 4948 KB
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define va first
#define vb second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define pp push_back
#define ep emplace_back
#define all(v) (v).begin(),(v).end()
#define szz(v) ((int)(v).size())
#define bi_pc __builtin_popcount
#define bi_pcll __builtin_popcountll
#define bi_tz __builtin_ctz
#define bi_tzll __builtin_ctzll
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef TAMREF
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#endif
using namespace std;
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cerr << vars << " = ";
    string delim = "";
    (..., (cerr << delim << values, delim = ", "));
    cerr << '\n';
}
#ifdef TAMREF
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...) 42
#endif
using ll = long long; using lf = long double; 
using pii = pair<int,int>; using ppi = pair<int,pii>;
using pll = pair<ll,ll>; using pff = pair<lf,lf>;
using ti = tuple<int,int,int>;
using base = complex<double>;
const lf PI = 3.14159265358979323846264338L;
template <typename T>
inline T umax(T& u, T v){return u = max(u, v);}
template <typename T>
inline T umin(T& u, T v){return u = min(u, v);}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<pll> G[200005];
ll dp[200005][2];
int vis[200005];
int n;
ll ans;

void dfs(int x) {
    vis[x] = 1;
    vector<ll> P(2);
    P[0] = LLONG_MIN;

    vector<pll> subdp[2];
    subdp[0].clear();
    subdp[1].clear();

    for(auto &[u, w] : G[x]) {
        if(vis[u]) continue;
        dfs(u);
        for(int b = 0; b < 2; b++) {
            for(int c = 0; c < 2; c++) {
                ll cand = dp[u][b] + P[c];
                if(b || dp[u][b]) cand += w;
                umax(dp[x][b ^ c], cand);
            }
        }
        if(dp[u][0]) {
            umax(P[1], dp[u][0] + w);
            subdp[0].ep(dp[u][0] + w, u);
        } 
        umax(P[0], dp[u][1] + w);
        subdp[1].ep(dp[u][1] + w, u);
    }
    debug("dp[%d][%d] = %lld, dp[%d][%d] = %lld\n", x, 0, dp[x][0], x, 1, dp[x][1]);
    
    if(szz(G[x]) >= 3) {
        sort(all(subdp[0]), greater<pll>());
        sort(all(subdp[1]), greater<pll>());

        // 0 0 0
        if(szz(subdp[0]) >= 3) {
            umax(ans, subdp[0][0].va + subdp[0][1].va + subdp[0][2].va);
        }

        // 1 1 0
        if(szz(subdp[1]) >= 2) {
            int i1 = subdp[1][0].vb, i2 = subdp[1][1].vb;
            auto it = find_if(all(subdp[0]), [&](pll u) {
                return u.vb != i1 && u.vb != i2;
            });
            if(it != subdp[0].end()) umax(ans, subdp[1][0].va + subdp[1][0].va + it -> va);
        }
        // 0 1 1
        if(szz(subdp[0])) {
            ll cc = subdp[0][0].va + subdp[1][0].va;
            int i1 = subdp[0][0].vb, i2 = subdp[1][0].vb;
            if(i1 == i2) {
                i2 = subdp[1][1].vb;
                cc += subdp[1][1].va - subdp[1][0].va;
            }
            auto it = find_if(all(subdp[1]), [&](pll u) {
                return u.vb != i1 && u.vb != i2;
            });
            umax(ans, cc + it -> va);
        }

        if(szz(subdp[0])) {
            // 1 0 1
            ll cc = subdp[1][0].va + subdp[0][0].va;
            int i1 = subdp[1][0].vb, i2 = subdp[0][0].vb;
            if(i1 == i2 && szz(subdp[0]) >= 2) {
                i2 = subdp[0][1].vb;
                cc += subdp[0][1].va - subdp[0][0].va;
            }
            if(i1 != i2) {
                auto it = find_if(all(subdp[1]), [&](pll u) {
                    return u.vb != i1 && u.vb != i2;
                });
                umax(ans, cc + it -> va);
            }
        }
    }
}

int main(){
    fio;
    cin >> n;
    for(int i = 1, a, b, c; i < n; i++) {
        cin >> a >> b >> c;
        G[a].ep(b, c);
        G[b].ep(a, c);
    }
    dfs(1);

    for(int i = 1; i <= n; i++) umax(ans, dp[i][1]);

    cout << ans << '\n';
}

Compilation message

beads.cpp: In function 'void dfs(int)':
beads.cpp:104:20: warning: statement has no effect [-Wunused-value]
  104 | #define debug(...) 42
      |                    ^~
beads.cpp:163:5: note: in expansion of macro 'debug'
  163 |     debug("dp[%d][%d] = %lld, dp[%d][%d] = %lld\n", x, 0, dp[x][0], x, 1, dp[x][1]);
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 2 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 2 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 2 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 2 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -