結果

問題 No.1103 Directed Length Sum
ユーザー merom686merom686
提出日時 2020-07-04 00:39:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 3,000 ms
コード長 1,121 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 91,276 KB
実行使用メモリ 48,920 KB
最終ジャッジ日時 2023-10-17 06:43:16
合計ジャッジ時間 5,514 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 218 ms
48,920 KB
testcase_03 AC 159 ms
22,784 KB
testcase_04 AC 142 ms
14,344 KB
testcase_05 AC 252 ms
22,256 KB
testcase_06 AC 91 ms
10,256 KB
testcase_07 AC 20 ms
5,044 KB
testcase_08 AC 31 ms
5,836 KB
testcase_09 AC 13 ms
4,348 KB
testcase_10 AC 44 ms
6,824 KB
testcase_11 AC 156 ms
15,332 KB
testcase_12 AC 93 ms
10,520 KB
testcase_13 AC 45 ms
6,824 KB
testcase_14 AC 10 ms
4,348 KB
testcase_15 AC 69 ms
8,868 KB
testcase_16 AC 178 ms
16,652 KB
testcase_17 AC 186 ms
17,376 KB
testcase_18 AC 43 ms
6,824 KB
testcase_19 AC 164 ms
15,596 KB
testcase_20 AC 16 ms
4,516 KB
testcase_21 AC 29 ms
5,572 KB
testcase_22 AC 132 ms
13,288 KB
testcase_23 AC 74 ms
9,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

ll ans = 0;

struct Graph {
    struct Vertex { int n; };
    struct Edge { int i, n; };
    Graph(int n, int m) : v(n, { -1 }), e(m), n(n), m(0) {}
    void add_edge(int i, int j) {
        e[m] = { j, v[i].n };
        v[i].n = m;
        m++;
    }
    int dfs(int i, int d) {
        int k = 1;
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge& o = e[j];
            k += dfs(o.i, d + 1);
        }
        ans += (ll)k * d;
        return k;
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    Graph g(n, (n - 1) * 2);
    int x = 0;

    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;

        g.add_edge(a, b);
        x ^= b;
    }
    for (int i = n & ~3; i < n; i++) {
        x ^= i;
    }

    g.dfs(x, 0);

    cout << ans % 1000000007 << endl;

    return 0;
}
0