結果

問題 No.1103 Directed Length Sum
ユーザー ysystem7ysystem7
提出日時 2020-09-02 15:58:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 659 ms / 3,000 ms
コード長 1,852 bytes
コンパイル時間 3,393 ms
コンパイル使用メモリ 217,992 KB
実行使用メモリ 118,808 KB
最終ジャッジ日時 2023-08-13 17:54:41
合計ジャッジ時間 12,520 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 314 ms
118,808 KB
testcase_03 AC 179 ms
54,676 KB
testcase_04 AC 351 ms
37,800 KB
testcase_05 AC 659 ms
63,112 KB
testcase_06 AC 209 ms
25,648 KB
testcase_07 AC 34 ms
8,524 KB
testcase_08 AC 62 ms
11,384 KB
testcase_09 AC 20 ms
6,404 KB
testcase_10 AC 94 ms
14,308 KB
testcase_11 AC 376 ms
40,904 KB
testcase_12 AC 211 ms
25,664 KB
testcase_13 AC 85 ms
14,720 KB
testcase_14 AC 15 ms
5,740 KB
testcase_15 AC 148 ms
20,792 KB
testcase_16 AC 436 ms
45,640 KB
testcase_17 AC 455 ms
47,412 KB
testcase_18 AC 89 ms
14,256 KB
testcase_19 AC 397 ms
41,700 KB
testcase_20 AC 26 ms
7,300 KB
testcase_21 AC 50 ms
10,560 KB
testcase_22 AC 313 ms
34,944 KB
testcase_23 AC 169 ms
22,116 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
関数 ‘void dfs(ll, ll, ll)’ 内,
    inlined from ‘int main()’ at main.cpp:72:8:
main.cpp:39:12: 警告: ‘root’ may be used uninitialized [-Wmaybe-uninitialized]
   39 |     depth[v]=d;
      |            ^
main.cpp: 関数 ‘int main()’ 内:
main.cpp:56:9: 備考: ‘root’ はここで定義されています
   56 |     int root;
      |         ^~~~

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;

V<V<ll>> G;
V<ll> depth;
V<ll> cnt;

void dfs(ll v,ll p,ll d){
    depth[v]=d;
    for(ll u:G[v]){
        if(u==p) continue;
        dfs(u,v,d+1);
        cnt[v]+=cnt[u];
    }
    cnt[v]++;
}

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int n;
    cin>>n;
    G.resize(n);
    cnt.resize(n);
    depth.resize(n);
    V<int> deg(n,0);
    int root;
    rep(i,n-1){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].pb(b);
        deg[b]++;
        //G[b].pb(a);
    }
    rep(i,n) cnt[i]=0;
    rep(i,n){
        if(deg[i]==0){
            root=i;
            break;
        }
    }
    dfs(root,-1,0);
    ll ans=0;
    rep(i,n) ans=(ans+(depth[i]+1)*(cnt[i]-1)%mod)%mod;
    out(ans);
}
0