結果

問題 No.1103 Directed Length Sum
ユーザー hs0319boyhs0319boy
提出日時 2020-07-07 16:42:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,432 ms / 3,000 ms
コード長 1,838 bytes
コンパイル時間 1,859 ms
コンパイル使用メモリ 180,848 KB
実行使用メモリ 183,000 KB
最終ジャッジ日時 2024-04-08 21:10:11
合計ジャッジ時間 22,658 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
34,628 KB
testcase_01 AC 18 ms
34,628 KB
testcase_02 AC 1,156 ms
183,000 KB
testcase_03 AC 840 ms
97,336 KB
testcase_04 AC 1,204 ms
74,820 KB
testcase_05 AC 2,432 ms
104,108 KB
testcase_06 AC 723 ms
60,740 KB
testcase_07 AC 119 ms
40,900 KB
testcase_08 AC 197 ms
44,228 KB
testcase_09 AC 78 ms
38,596 KB
testcase_10 AC 315 ms
47,812 KB
testcase_11 AC 1,331 ms
78,404 KB
testcase_12 AC 761 ms
60,996 KB
testcase_13 AC 341 ms
48,196 KB
testcase_14 AC 66 ms
37,572 KB
testcase_15 AC 563 ms
55,108 KB
testcase_16 AC 1,549 ms
83,748 KB
testcase_17 AC 1,629 ms
85,888 KB
testcase_18 AC 304 ms
47,684 KB
testcase_19 AC 1,429 ms
79,556 KB
testcase_20 AC 95 ms
39,364 KB
testcase_21 AC 196 ms
43,332 KB
testcase_22 AC 1,142 ms
71,236 KB
testcase_23 AC 591 ms
56,644 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:70:14: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   70 |     babycount(root,-1);
      |     ~~~~~~~~~^~~~~~~~~
main.cpp:62:9: note: 'root' was declared here
   62 |     int root;
      |         ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const ll inf=1e9+7;
const ll INF=1e18;

int MAX_N=1000020;

vvin baby(MAX_N);
vin d(MAX_N);

void deep(int root){
    queue<int> q;q.push(root);
    fill(all(d),0);
    while(q.size()){
        int p=q.front();q.pop();
        if(baby[p].size()==0)continue;
        for(auto b:baby[p]){
            d[b]=d[p]+1;
            q.push(b);
        }
    }
}

vin babynum(MAX_N,1);

void babycount(int v,int p){//自身を含む子孫の数
    if(baby[v].size()==0)return;
    for(auto b:baby[v]){
        if(b==p)continue;
        babycount(b,v);
        babynum[v]+=babynum[b];
    }
}

int main(){
    int n;cin>>n;
    vin a(n-1),b(n-1);
    set<int> notroot;
    rep(i,n-1){
        cin>>a[i]>>b[i];
        baby[a[i]].push_back(b[i]);
        notroot.insert(b[i]);
    }
    int root;
    rep(i,n){
        if(!notroot.count(i+1)){
            root=i+1;
            break;
        }
    }
    deep(root);
    babycount(root,-1);
    ll ans=(ll)0;
    rep(i,n-1){
        ans+=((ll)(d[a[i]]+1)*(ll)babynum[b[i]])%inf;
        ans%=inf;
    }
    cout<<ans<<endl;
    //cout<<root<<endl;
    //rep(i,n)cout<<d[i+1]<<" ";cout<<endl;
    //rep(i,n)cout<<babynum[i+1]<<" ";cout<<endl;
}
0