結果

問題 No.1103 Directed Length Sum
ユーザー hitonanode
提出日時 2020-07-11 10:42:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,021 ms / 3,000 ms
コード長 940 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 198,024 KB
最終ジャッジ日時 2025-01-11 19:42:07
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

int main()
{
    int N;
    cin >> N;
    vector<vector<int>> to(N), from(N);
    REP(_, N - 1)
    {
        int a, b;
        cin >> a >> b;
        to[a - 1].emplace_back(b - 1), from[b - 1].emplace_back(a - 1);
    }
    int r = 0;
    while (from[r].size()) r++;

    lint ret = 0;
    auto dfs = [&](auto &&rec, int now, lint d) -> void {
        for (int nxt : to[now])
        {
            rec(rec, nxt, d + 1);
        }
        ret += d * (d + 1) / 2;
        ret %= 1000000007;
    };
    dfs(dfs, r, 0);
    cout << ret << '\n';
}
0