結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-11-26 13:31:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 3,000 ms
コード長 2,303 bytes
コンパイル時間 2,265 ms
コンパイル使用メモリ 211,232 KB
実行使用メモリ 38,864 KB
最終ジャッジ日時 2023-09-12 05:02:08
合計ジャッジ時間 8,293 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 164 ms
15,300 KB
testcase_09 AC 119 ms
11,820 KB
testcase_10 AC 174 ms
14,752 KB
testcase_11 AC 114 ms
12,040 KB
testcase_12 AC 192 ms
17,932 KB
testcase_13 AC 192 ms
18,156 KB
testcase_14 AC 200 ms
18,216 KB
testcase_15 AC 204 ms
18,296 KB
testcase_16 AC 224 ms
18,176 KB
testcase_17 AC 232 ms
18,212 KB
testcase_18 AC 246 ms
18,220 KB
testcase_19 AC 250 ms
18,260 KB
testcase_20 AC 242 ms
18,176 KB
testcase_21 AC 242 ms
18,260 KB
testcase_22 AC 253 ms
18,208 KB
testcase_23 AC 187 ms
18,300 KB
testcase_24 AC 197 ms
18,272 KB
testcase_25 AC 74 ms
18,836 KB
testcase_26 AC 126 ms
19,000 KB
testcase_27 AC 109 ms
38,800 KB
testcase_28 AC 155 ms
38,864 KB
testcase_29 AC 146 ms
29,740 KB
testcase_30 AC 156 ms
29,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using V = vector<T>;
using VI = V<int>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define all(a) a.begin(),a.end()
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& is, V<T>& v) { for (auto& a : v)is >> a; return is; }

template<class W> class weighted_graph {
    bool directed, zero_indexed_input;
    int n, m;
    V<V<pair<int, W>>> g;
public:
    weighted_graph(int n, int m, bool directed = false, bool zero_indexed_input = false) 
        : n(n), m(m), directed(directed), zero_indexed_input(zero_indexed_input), g(n) {};
    friend istream& operator>>(istream& is, weighted_graph<W>& me) {
        FOR(i, 0, me.m) {
            int a, b, c; is >> a >> b >> c;
            if (!me.zero_indexed_input)--a, --b;
            me.g[a].emplace_back(b, c);
            if (!me.directed)me.g[b].emplace_back(a, c);
        }
        return is;
    }
    const V<pair<int, W>>& operator[](int i) { return g[i]; }
    int size() { return n; }
};

void compress(VI& x) {
    VI y = x;
    sort(all(y));
    y.erase(unique(all(y)), y.end());
    for (int& a : x)a = lower_bound(all(y), a) - y.begin();
}

int main() {
    init();

    int n; cin >> n;
    weighted_graph<int> g(n, n - 1); cin >> g;

    VI x(n), sz(n, 1);
    auto dfs1 = [&](auto&& f, int cur, int par)->int {
        for (auto [to, weight] : g[cur]) {
            if (to == par)continue;
            x[to] = x[cur] ^ weight;
            sz[cur] += f(f, to, cur);
        }
        return sz[cur];
    };
    dfs1(dfs1, 0, -1);
    compress(x);

    ll ans = n * ll(n - 1);
    VI y(n), cnt(n);
    auto dfs2 = [&](auto&& f, int cur, int par)->void {
        ans -= y[x[cur]];
        ans -= ll(sz[cur]) * cnt[x[cur]];
        for (auto [to, weight] : g[cur]) {
            if (to == par)continue;
            int y_tmp = y[x[cur]], cnt_tmp = cnt[x[cur]];
            y[x[cur]] = n - sz[to], cnt[x[cur]] = 1;
            f(f, to, cur);
            y[x[cur]] = y_tmp, cnt[x[cur]] = cnt_tmp;
        }
        y[x[cur]] += sz[cur];
        ++cnt[x[cur]];
    };
    dfs2(dfs2, 0, -1);
    cout << ans << "\n";

    return 0;
}
0