結果

問題 No.1103 Directed Length Sum
ユーザー Im_GimletIm_Gimlet
提出日時 2020-07-19 00:52:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,494 ms / 3,000 ms
コード長 2,852 bytes
コンパイル時間 4,112 ms
コンパイル使用メモリ 176,848 KB
実行使用メモリ 112,400 KB
最終ジャッジ日時 2023-08-21 07:18:38
合計ジャッジ時間 19,518 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
50,056 KB
testcase_01 AC 23 ms
49,968 KB
testcase_02 AC 959 ms
112,400 KB
testcase_03 AC 759 ms
101,612 KB
testcase_04 AC 838 ms
77,944 KB
testcase_05 AC 1,494 ms
98,016 KB
testcase_06 AC 534 ms
68,316 KB
testcase_07 AC 127 ms
54,476 KB
testcase_08 AC 191 ms
56,492 KB
testcase_09 AC 85 ms
52,656 KB
testcase_10 AC 266 ms
59,236 KB
testcase_11 AC 934 ms
80,344 KB
testcase_12 AC 541 ms
68,260 KB
testcase_13 AC 269 ms
59,472 KB
testcase_14 AC 67 ms
52,076 KB
testcase_15 AC 417 ms
64,368 KB
testcase_16 AC 1,062 ms
84,020 KB
testcase_17 AC 1,079 ms
85,224 KB
testcase_18 AC 259 ms
58,892 KB
testcase_19 AC 939 ms
81,056 KB
testcase_20 AC 99 ms
53,276 KB
testcase_21 AC 170 ms
56,080 KB
testcase_22 AC 765 ms
75,508 KB
testcase_23 AC 448 ms
65,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

// auto mod int
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
      if ((x += a.x) >= mod) x -= mod;
      return *this;
    }
    mint& operator-=(const mint a) {
      if ((x += mod-a.x) >= mod) x -= mod;
      return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
      if (!t) return 1;
      mint a = pow(t>>1);
      a *= a;
      if (t&1) a *= *this;
      return a;
    }

    // for prime mod
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int n;
struct Data {
    int no; mint dist;
};

int main() {
    cin >> n;
    vector< vector<int> > Gto(1000100, vector<int>());
    vector< vector<int> > Gfrom(1000100, vector<int>());
    for(int i = 0; i < n-1; ++i) {
        int a, b; cin >> a >> b;
        a--; b--;
        Gto[a].emplace_back(b);
        Gfrom[b].emplace_back(a);
    }

    queue<int> que;
    que.push(0);
    int root = 0;
    while(!que.empty()) {
        int now = que.front(); que.pop();
        // cout << now << endl;
        int glen = Gfrom[now].size();
        if(glen == 0) {
            root = now; break;
        }
        for(int i = 0; i < glen; ++i) {
            que.push(Gfrom[now][i]);
        }
    }

    // cout << root << endl;

    mint ans = 0;
    // 根がどれかはわからないけど木なので正しい根を選べば全部探索できる
    queue<Data> Dque;
    Data first; first.no = root; first.dist = 0;
    Dque.push(first);
    while(!Dque.empty()) {
        Data now = Dque.front(); Dque.pop();
        ans += ((now.dist+1)*now.dist)/2;
        for(int i = 0; i < (int)Gto[now.no].size(); ++i) {
            Data next; next.no = Gto[now.no][i];
            next.dist = now.dist+1;
            Dque.push(next);
        }
    }
    cout << ans << endl;
}
0