結果

問題 No.1103 Directed Length Sum
ユーザー ysystem7
提出日時 2020-09-02 15:58:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 744 ms / 3,000 ms
コード長 1,852 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 221,108 KB
最終ジャッジ日時 2025-01-14 03:35:17
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
In function ‘void dfs(ll, ll, ll)’,
    inlined from ‘int main()’ at main.cpp:72:8:
main.cpp:39:12: warning: ‘root’ may be used uninitialized [-Wmaybe-uninitialized]
   39 |     depth[v]=d;
      |            ^
main.cpp: In function ‘int main()’:
main.cpp:56:9: note: ‘root’ was declared here
   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