結果

問題 No.872 All Tree Path
ユーザー first_vilfirst_vil
提出日時 2019-11-20 23:25:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 199 ms / 3,000 ms
コード長 2,140 bytes
コンパイル時間 2,030 ms
コンパイル使用メモリ 178,964 KB
実行使用メモリ 24,320 KB
最終ジャッジ日時 2024-04-16 20:12:08
合計ジャッジ時間 5,217 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 189 ms
16,000 KB
testcase_01 AC 192 ms
16,000 KB
testcase_02 AC 188 ms
16,000 KB
testcase_03 AC 90 ms
24,320 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 196 ms
16,000 KB
testcase_06 AC 199 ms
16,000 KB
testcase_07 AC 199 ms
16,000 KB
testcase_08 AC 12 ms
5,376 KB
testcase_09 AC 12 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 12 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i))
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
#define out(y,x) (y) < 0 || h <= (y) || (x) < 0 || w <= (x)
#define line cout << "------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
const long double pi = acos(-1);
const long double eps = 1e-10;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T> inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

int n;
vector<vector<pair<int, int>>> g;
vector<bool> ever;
VI cnt;

int dfs1(int now) {
    ever[now] = true;
    for (auto i : g[now]) {
        int to = i.first;
        if (ever[to])continue;
        cnt[now] += dfs1(to);
    }
    return cnt[now];
}

ll dfs2(int now) {
    ll ret = 0;
    ever[now] = true;
    for (auto i : g[now]) {
        int to; ll cost;
        tie(to, cost) = i;
        if (ever[to])continue;
        ret += cnt[to] * (n - (ll)cnt[to]) * 2 * cost;
        ret += dfs2(to);
    }
    return ret;
}

int main() {
    init();

    cin >> n;
    g.resize(n);
    FOR(i, 0, n - 1) {
        int s, t, w; cin >> s >> t >> w;
        --s, --t;
        g[s].emplace_back(t, w);
        g[t].emplace_back(s, w);
    }

    ever.resize(n);
    cnt.resize(n, 1);
    dfs1(0);

    ever.clear();
    ever.resize(n);

    cout << dfs2(0) << "\n";
}
0