結果

問題 No.1103 Directed Length Sum
ユーザー freecss00freecss00
提出日時 2020-07-03 22:27:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 570 ms / 3,000 ms
コード長 1,993 bytes
コンパイル時間 1,766 ms
コンパイル使用メモリ 172,156 KB
実行使用メモリ 96,596 KB
最終ジャッジ日時 2023-10-17 05:18:30
合計ジャッジ時間 8,546 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
26,964 KB
testcase_01 AC 10 ms
26,964 KB
testcase_02 AC 326 ms
96,596 KB
testcase_03 AC 191 ms
35,180 KB
testcase_04 AC 289 ms
37,792 KB
testcase_05 AC 570 ms
46,172 KB
testcase_06 AC 174 ms
34,096 KB
testcase_07 AC 39 ms
28,392 KB
testcase_08 AC 56 ms
29,344 KB
testcase_09 AC 27 ms
27,868 KB
testcase_10 AC 76 ms
30,400 KB
testcase_11 AC 304 ms
38,848 KB
testcase_12 AC 161 ms
34,096 KB
testcase_13 AC 80 ms
30,400 KB
testcase_14 AC 22 ms
27,644 KB
testcase_15 AC 128 ms
32,512 KB
testcase_16 AC 365 ms
40,432 KB
testcase_17 AC 364 ms
40,960 KB
testcase_18 AC 76 ms
30,400 KB
testcase_19 AC 307 ms
39,376 KB
testcase_20 AC 31 ms
28,056 KB
testcase_21 AC 49 ms
29,080 KB
testcase_22 AC 248 ms
37,000 KB
testcase_23 AC 131 ms
32,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)(n-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

vi g[1000010];

ll dfs(int v, int x, ll cnt){
    ll res = cnt;
    for(auto u : g[v]){
        res += dfs(u, x+1, (cnt + x) % mod);
        res %= mod;
    }
    return res;
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    int n; cin >> n;
    vi cnt(n);
    rep(i,n-1){
        int a,b; cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        cnt[b]++;
    }
    int root = -1;
    rep(i,n) if(cnt[i] == 0) root = i;
    ll ans = dfs(root, 1, 0);
    cout << ans << endl;
    return 0;
}
0