結果

問題 No.1103 Directed Length Sum
ユーザー hs0319boyhs0319boy
提出日時 2020-07-07 17:55:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,010 ms / 3,000 ms
コード長 1,844 bytes
コンパイル時間 1,974 ms
コンパイル使用メモリ 173,048 KB
実行使用メモリ 136,136 KB
最終ジャッジ日時 2024-04-09 02:21:48
合計ジャッジ時間 13,268 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
34,408 KB
testcase_01 AC 14 ms
34,484 KB
testcase_02 AC 790 ms
136,136 KB
testcase_03 AC 511 ms
50,540 KB
testcase_04 AC 540 ms
48,364 KB
testcase_05 AC 1,010 ms
58,380 KB
testcase_06 AC 342 ms
43,556 KB
testcase_07 AC 80 ms
36,584 KB
testcase_08 AC 121 ms
37,736 KB
testcase_09 AC 57 ms
35,664 KB
testcase_10 AC 165 ms
38,992 KB
testcase_11 AC 604 ms
49,496 KB
testcase_12 AC 343 ms
43,548 KB
testcase_13 AC 173 ms
39,116 KB
testcase_14 AC 45 ms
35,492 KB
testcase_15 AC 271 ms
41,556 KB
testcase_16 AC 687 ms
51,440 KB
testcase_17 AC 719 ms
52,232 KB
testcase_18 AC 167 ms
38,996 KB
testcase_19 AC 623 ms
50,072 KB
testcase_20 AC 67 ms
36,072 KB
testcase_21 AC 111 ms
37,476 KB
testcase_22 AC 499 ms
46,932 KB
testcase_23 AC 281 ms
42,016 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:69:9: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   69 |     deep(root);
      |     ~~~~^~~~~~
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);
    vbo notroot(n+1,false);
    rep(i,n-1){
        cin>>a[i]>>b[i];
        baby[a[i]].push_back(b[i]);
        notroot[b[i]]=true;
    }
    int root;
    rep(i,n){
        if(!notroot[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