Submission #23348

# Submission time Handle Problem Language Result Execution time Memory
23348 2017-05-06T12:22:19 Z Nurlykhan Beads and wires (APIO14_beads) C++14
0 / 100
6 ms 4992 KB
#include <bits/stdc++.h>
 
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long 
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
 
using namespace std;
 
const int N = (int) 2e5 + 7;
const int M = (int) 2e6 + 7;
const ll LINF = (ll) 1e16;
const int INF = (int) 1e9 + 7;
const double EPS = (double) 1e-9;
int n;
struct edge {
    int a, b, c;

    edge() {}
};
edge a[N];
vector<int> v[N];
ll dp[N];

void calc(int x, int p = -1) {
    vector<pii> vv;
    for (auto ii : v[x]) {
        int it = a[ii].a ^ a[ii].b ^ x;
        if (it == p) continue;
        calc(it, x);
        vv.pb(mp(it, a[ii].c));
    }
    for (int i = 0; i < sz(vv); i++) {
        for (int j = i + 1; j < sz(vv); j++) {
            ll sum = dp[vv[i].f] + dp[vv[j].f] + vv[i].s + vv[j].s;
            for (int k = 0; k < sz(vv); k++) {
                if (i == k || j == k) continue;
                sum += dp[vv[k].f];
            }
            dp[x] = max(dp[x], sum);
        }
    }
}

int main() {
    #define fn "balls"
    #ifdef witch
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
//        freopen(fn".in", "r", stdin);
//        freopen(fn".out", "w", stdout);
    #endif
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        cin >> a[i].a >> a[i].b >> a[i].c;
        v[a[i].a].pb(i);
        v[a[i].b].pb(i);
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dp[j] = 0;
        }
        calc(i);
        for (int j = 1; j <= n; j++) {
            ans = max(ans, dp[j]);
        }
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Incorrect 6 ms 4992 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Incorrect 6 ms 4992 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Incorrect 6 ms 4992 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Incorrect 6 ms 4992 KB Output isn't correct
4 Halted 0 ms 0 KB -