結果

問題 No.1103 Directed Length Sum
ユーザー mikianaaamikianaaa
提出日時 2020-08-20 15:43:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,733 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 177,152 KB
実行使用メモリ 69,632 KB
最終ジャッジ日時 2024-10-13 11:44:01
合計ジャッジ時間 9,729 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 189 ms
46,388 KB
testcase_04 AC 299 ms
32,256 KB
testcase_05 AC 641 ms
53,632 KB
testcase_06 AC 166 ms
22,396 KB
testcase_07 AC 28 ms
7,808 KB
testcase_08 AC 47 ms
10,240 KB
testcase_09 AC 19 ms
6,016 KB
testcase_10 AC 70 ms
12,800 KB
testcase_11 AC 337 ms
34,980 KB
testcase_12 AC 150 ms
22,352 KB
testcase_13 AC 69 ms
13,044 KB
testcase_14 AC 15 ms
6,820 KB
testcase_15 AC 116 ms
18,240 KB
testcase_16 AC 386 ms
38,936 KB
testcase_17 AC 408 ms
40,356 KB
testcase_18 AC 67 ms
12,764 KB
testcase_19 AC 345 ms
35,824 KB
testcase_20 AC 21 ms
6,912 KB
testcase_21 AC 41 ms
9,600 KB
testcase_22 AC 270 ms
29,904 KB
testcase_23 AC 131 ms
19,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/istream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/sstream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/complex:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ccomplex:39,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:2:
In member function 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = int; _Args = {const int&}; _Tp = int]',
    inlined from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = int; _Args = {const int&}; _Tp = int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/alloc_traits.h:516:17,
    inlined from 'void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_deque.h:1543:30,
    inlined from 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/

ソースコード

diff #

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

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

    int N;
    cin >> N;
    ll MOD = pow(10, 9) + 7;
    vector<vector<int>> G(N, vector<int>());
    vector<int> root(N, -1);
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        root[b] = a;
    }

    int s;
    rep(i, N) {
        if (root[i] == -1) {
            s = i;
            break;
        }
    }

    vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[s] = 0;
    que.push(s); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1)
                continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            dist[nv] %= MOD;
            que.push(nv);
        }
    }

    ll ans = 0;
    rep(i, N) {
        ll tmp = dist[i];
        if (dist[i] >= 2) {
            tmp = (1 + dist[i]) * dist[i] % MOD / 2;
        }

        ans += tmp;
        ans %= MOD;
    }

    cout << ans << endl;
}
0