結果

問題 No.1103 Directed Length Sum
コンテスト
ユーザー hs0319boy
提出日時 2020-07-03 22:52:27
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,699 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,464 ms
コンパイル使用メモリ 196,460 KB
実行使用メモリ 77,696 KB
最終ジャッジ日時 2026-04-06 02:49:37
合計ジャッジ時間 14,196 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 1 WA * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/string:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bitset:54,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
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/15.2.0_1/include/c++/15/bits/alloc_traits.h:674: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/15.2.0_1/include/c++/15/bits/stl_deque.h:1610: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/15.2.0_1/include/c++/15/bits/stl_queue.h:313:20,
    inlined from 'int main()' at main.cpp:39:26:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/new_allocator.h:191:11: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:37:9: note: 'root' was declared here
   37 |     int root;
      |         ^~~~

ソースコード

diff #
raw source code

#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
    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();q.pop();
        if(par[p.second]==-1)continue;
        child[par[p.second]]+=child[p.second];
        q.push(mp(d[p.second],par[p.second]));
    }
    ll ans=(ll)0;
    rep(i,n-1){
        ans+=(d[a[i]]*child[b[i]])%inf;
        ans%=inf;
    }
    cout<<ans<<endl;
}
0