結果

問題 No.1103 Directed Length Sum
ユーザー ShibuyapShibuyap
提出日時 2020-07-03 21:48:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 964 ms / 3,000 ms
コード長 1,001 bytes
コンパイル時間 2,150 ms
コンパイル使用メモリ 210,096 KB
実行使用メモリ 70,084 KB
最終ジャッジ日時 2023-10-17 00:34:31
合計ジャッジ時間 12,100 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
28,312 KB
testcase_01 AC 11 ms
28,312 KB
testcase_02 AC 705 ms
70,084 KB
testcase_03 AC 495 ms
46,604 KB
testcase_04 AC 569 ms
44,800 KB
testcase_05 AC 964 ms
54,872 KB
testcase_06 AC 324 ms
40,004 KB
testcase_07 AC 72 ms
30,808 KB
testcase_08 AC 113 ms
32,124 KB
testcase_09 AC 50 ms
29,884 KB
testcase_10 AC 156 ms
35,548 KB
testcase_11 AC 587 ms
46,028 KB
testcase_12 AC 331 ms
40,080 KB
testcase_13 AC 162 ms
35,672 KB
testcase_14 AC 39 ms
29,488 KB
testcase_15 AC 266 ms
38,084 KB
testcase_16 AC 652 ms
47,892 KB
testcase_17 AC 690 ms
48,620 KB
testcase_18 AC 154 ms
35,516 KB
testcase_19 AC 592 ms
46,428 KB
testcase_20 AC 57 ms
30,212 KB
testcase_21 AC 98 ms
31,776 KB
testcase_22 AC 478 ms
43,600 KB
testcase_23 AC 269 ms
38,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("YES");}else{puts("NO");}
#define MAX_N 1000005

vector<int> G[MAX_N];
int par_[MAX_N];

int main() {
    int n;
    cin >> n;
    rep(i,n)par_[i] = -1;

    ll MOD = 1000000007;
    rep(i,n-1){
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        par_[b] = a;
    }

    int r = -1;
    rep(i,n)if(par_[i] == -1)r = i;

    ll ans = 0;
    
    queue<int> que;
    que.push(r);
    ll d[n] = {};
    while(que.size()>0){
        int x = que.front();
        que.pop();
        ans += d[x] * (d[x] + 1) / 2;
        ans %= MOD;
        rep(i,G[x].size()){
            int y = G[x][i];
            d[y] = d[x] + 1;
            que.push(y);
        }
    }

    cout << ans << endl;
    return 0;
}
 
 

0