結果

問題 No.1103 Directed Length Sum
ユーザー tokusakuraitokusakurai
提出日時 2020-07-03 21:52:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,389 bytes
コンパイル時間 6,453 ms
コンパイル使用メモリ 169,912 KB
実行使用メモリ 198,912 KB
最終ジャッジ日時 2023-10-23 14:47:56
合計ジャッジ時間 15,485 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
51,536 KB
testcase_01 AC 22 ms
51,532 KB
testcase_02 AC 995 ms
198,912 KB
testcase_03 AC 614 ms
93,088 KB
testcase_04 AC 818 ms
82,988 KB
testcase_05 AC 1,473 ms
104,560 KB
testcase_06 AC 500 ms
73,660 KB
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

const int MAX_V = 1e6;
vector<int> es[MAX_V], rs[MAX_V];
ll subtree_size[MAX_V];
ll ans = 0;

ll dfs(int now){
    ll ret = 0, s = 1;
    for(auto &e: es[now]){
        ret += dfs(e)+subtree_size[e];
        s += subtree_size[e];
    }
    subtree_size[now] = s;
    ans += ret, ans %= MOD;
    return ret;
}

int main(){
    int N;
    cin >> N;
    rep(i, N-1){
        int A, B;
        cin >> A >> B;
        A--, B--;
        es[A].pb(B), rs[B].pb(A);
    }
    int root = -1;
    rep(i, N) if(rs[i].empty()) root = i;
    dfs(root);
    cout << ans << endl;
}
0