Submission #8508150


Source Code Expand

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <functional>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

VI Centroid(VVI &graph){
    int n = graph.size();
    VI ret, sz(n);
    function<void (int, int)> dfs = [&](int now, int past){
        sz[now] = 1;
        bool is_centroid = true;
        for (int next : graph[now]){
            if (next == past) continue;
            dfs(next, now);
            sz[now] += sz[next];
            if (sz[next] > n / 2) is_centroid = false;
        }
        if (n - sz[now] > n / 2) is_centroid = false;
        if (is_centroid) ret.push_back(now);
    };
    dfs(0, -1);
    return ret;
}

const int N = 100010;
VI e[N];
VL c[N];
ll sz[N];

ll dfs(int now, int past){
    ll ret = 0;
    sz[now] = 1;
    REP(i,e[now].size()){
        int next = e[now][i];
        if (next == past) continue;
        ret += dfs(next, now);
        sz[now] += sz[next];
        ret += 2 * sz[next] * c[now][i];
        // cout << next << " " << sz[next] << " " << c[now][i] << endl;
    }
    return ret;
}

int main() {
    int n;
    cin >> n;
    VVI graph(n);
    REP(i,n-1){
        int x = in() - 1, y = in() - 1, z = in();
        e[x].push_back(y);
        c[x].push_back(z);
        e[y].push_back(x);
        c[y].push_back(z);
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    VI cs = Centroid(graph);
    ll ans = 0;
    if (cs.size() == 2){
        int c1 = cs[0], c2 = cs[1];
        ans = dfs(c1, c2) + dfs(c2, c1);
        REP(i,e[c1].size()){
            if (e[c1][i] == c2){
                ans += (n - 1) * c[c1][i];
            }
        }
    }else{
        int c1 = cs[0];
        ans = dfs(c1, -1) - *min_element(ALL(c[c1]));
    }
    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task D - Tree and Hamilton Path
User TangentDay
Language C++14 (GCC 5.4.1)
Score 1100
Code Size 2672 Byte
Status AC
Exec Time 134 ms
Memory 24184 KB

Compile Error

./Main.cpp: In function ‘int in()’:
./Main.cpp:37:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 int in() { int x; scanf("%d", &x); return x; }
                                  ^
./Main.cpp: In function ‘ll lin()’:
./Main.cpp:38:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 ll lin() { ll x; scanf("%lld", &x); return x; }
                                   ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1100 / 1100
Status
AC × 2
AC × 38
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All sample_01.txt, sample_02.txt, sample_01.txt, sample_02.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt
Case Name Status Exec Time Memory
sample_01.txt AC 3 ms 4992 KB
sample_02.txt AC 3 ms 4992 KB
subtask_1_01.txt AC 3 ms 4992 KB
subtask_1_02.txt AC 87 ms 16676 KB
subtask_1_03.txt AC 44 ms 11204 KB
subtask_1_04.txt AC 96 ms 17672 KB
subtask_1_05.txt AC 52 ms 13232 KB
subtask_1_06.txt AC 37 ms 10836 KB
subtask_1_07.txt AC 39 ms 10456 KB
subtask_1_08.txt AC 47 ms 11452 KB
subtask_1_09.txt AC 29 ms 9216 KB
subtask_1_10.txt AC 12 ms 7168 KB
subtask_1_11.txt AC 27 ms 9088 KB
subtask_1_12.txt AC 107 ms 18040 KB
subtask_1_13.txt AC 102 ms 18040 KB
subtask_1_14.txt AC 101 ms 18040 KB
subtask_1_15.txt AC 97 ms 18040 KB
subtask_1_16.txt AC 97 ms 18040 KB
subtask_1_17.txt AC 96 ms 18040 KB
subtask_1_18.txt AC 102 ms 18040 KB
subtask_1_19.txt AC 94 ms 18040 KB
subtask_1_20.txt AC 102 ms 18040 KB
subtask_1_21.txt AC 134 ms 18040 KB
subtask_1_22.txt AC 119 ms 18168 KB
subtask_1_23.txt AC 125 ms 22136 KB
subtask_1_24.txt AC 110 ms 23672 KB
subtask_1_25.txt AC 111 ms 18040 KB
subtask_1_26.txt AC 100 ms 19704 KB
subtask_1_27.txt AC 108 ms 23160 KB
subtask_1_28.txt AC 98 ms 18168 KB
subtask_1_29.txt AC 105 ms 24184 KB
subtask_1_30.txt AC 103 ms 22392 KB
subtask_1_31.txt AC 101 ms 18412 KB
subtask_1_32.txt AC 90 ms 18412 KB
subtask_1_33.txt AC 50 ms 15344 KB
subtask_1_34.txt AC 71 ms 19440 KB