結果

問題 No.1103 Directed Length Sum
ユーザー hs0319boyhs0319boy
提出日時 2020-07-03 23:14:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,505 ms / 3,000 ms
コード長 1,895 bytes
コンパイル時間 1,822 ms
コンパイル使用メモリ 182,896 KB
実行使用メモリ 77,804 KB
最終ジャッジ日時 2023-10-17 06:15:39
合計ジャッジ時間 14,753 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 725 ms
77,804 KB
testcase_03 AC 721 ms
65,508 KB
testcase_04 AC 800 ms
41,416 KB
testcase_05 AC 1,505 ms
67,252 KB
testcase_06 AC 491 ms
27,260 KB
testcase_07 AC 91 ms
9,260 KB
testcase_08 AC 145 ms
12,416 KB
testcase_09 AC 56 ms
7,052 KB
testcase_10 AC 205 ms
15,420 KB
testcase_11 AC 893 ms
44,460 KB
testcase_12 AC 503 ms
27,544 KB
testcase_13 AC 208 ms
15,724 KB
testcase_14 AC 40 ms
6,216 KB
testcase_15 AC 354 ms
22,620 KB
testcase_16 AC 1,000 ms
50,288 KB
testcase_17 AC 1,038 ms
51,312 KB
testcase_18 AC 199 ms
15,320 KB
testcase_19 AC 928 ms
46,188 KB
testcase_20 AC 67 ms
8,016 KB
testcase_21 AC 129 ms
11,288 KB
testcase_22 AC 705 ms
36,132 KB
testcase_23 AC 397 ms
23,864 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:40:22: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   40 |     vin d(n+1);d[root]=1;
      |                      ^
main.cpp:37:9: note: 'root' was declared here
   37 |     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 main(){
    int n;cin>>n;
    vin a(n-1),b(n-1);
    vin par(n+1,-1);
    vvin baby(n+1);
    vbo r(n+1,false);
    rep(i,n-1){
        cin>>a[i]>>b[i];
        baby[a[i]].push_back(b[i]);
        par[b[i]]=a[i];
        r[b[i]]=true;
    }
    int root;
    rep(i,n)if(!r[i+1])root=i+1;
    queue<int> qq;qq.push(root);
    vin d(n+1);d[root]=1;
    while(qq.size()){
        auto p=qq.front();qq.pop();
        if(baby[p].size()==0)continue;
        for(auto tmp:baby[p]){
            d[tmp]=d[p]+1;
            qq.push(tmp);
        }
    }
    vin child(n+1,1);//the number of children
    vbo arrived(n+1,false);
    priority_queue<pair<int,int>> q;
    rep(i,n)if(baby[i+1].size()==0)q.push(mp(d[i+1],i+1));
    while(q.size()){
        auto p=q.top().second;q.pop();
        //cout<<p<<" ";
        if(arrived[p]||par[p]==-1)continue;
        child[par[p]]+=child[p];
        q.push(mp(d[par[p]],par[p]));
        arrived[p]=true;
    }
    //cout<<endl;
    ll ans=(ll)0;
    rep(i,n-1){
        ans+=((ll)d[a[i]]*(ll)child[b[i]])%inf;
        ans%=inf;
    }
    cout<<ans<<endl;
    /*rep(i,n)cout<<par[i+1]<<" ";
    cout<<endl;
    rep(i,n)cout<<child[i+1]<<" ";
    cout<<endl;*/
}
0