답안 #1055689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055689 2024-08-13T03:21:18 Z RiverFlow 구슬과 끈 (APIO14_beads) C++14
0 / 100
2 ms 5156 KB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

long long dp[N][2], f[N];
vector<pair<int, int>> g[N];

void dfs(int u, int p, int par_edge) {
    long long sum = 0;
    vector<ii> child;
    for(auto x : g[u]) {
        if (x.fi == p) continue ;
        dfs(x.fi, u, x.se);
        child.push_back(x);
        sum += f[x.fi];
    }
    // dp[u][0]
    FOR(i, 0, sz(child) - 1)
    FOR(j, i + 1, sz(child) - 1) {
        int v = child[i].fi,
            k = child[j].fi;
        sum -= f[v] + f[k];
        dp[u][0] = max(dp[u][0], dp[v][0] + dp[k][0]
                            + child[i].se + child[j].se
                            + sum);
        sum += f[v] + f[k];
    }
    if (par_edge > 0 and sz(child)) {
        for(auto x : child) {
            int v = x.fi;
            sum -= f[v];
            dp[u][1] = max(dp[u][1], dp[v][1] + sum + x.se + par_edge);
            sum += f[v];
        }
    }
    f[u] = max(dp[u][0], dp[u][1]);
}

void main_code() {
    int n; cin >> n;
    FOR(i, 1, n - 1) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back(_mp(v, w));
        g[v].push_back(_mp(u, w));
    }
    dfs(1, 0, 0);
    cout << dp[1][0];
}


/*     Let the river flows naturally     */

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5156 KB Output is correct
3 Incorrect 2 ms 4956 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5156 KB Output is correct
3 Incorrect 2 ms 4956 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5156 KB Output is correct
3 Incorrect 2 ms 4956 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5156 KB Output is correct
3 Incorrect 2 ms 4956 KB Output isn't correct
4 Halted 0 ms 0 KB -