結果

問題 No.1103 Directed Length Sum
ユーザー soto800soto800
提出日時 2020-07-04 16:27:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,147 ms / 3,000 ms
コード長 3,945 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 170,388 KB
実行使用メモリ 113,996 KB
最終ジャッジ日時 2023-10-19 07:51:59
合計ジャッジ時間 13,212 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
29,120 KB
testcase_01 AC 11 ms
29,120 KB
testcase_02 AC 782 ms
113,996 KB
testcase_03 AC 527 ms
43,460 KB
testcase_04 AC 639 ms
44,392 KB
testcase_05 AC 1,147 ms
54,136 KB
testcase_06 AC 402 ms
40,724 KB
testcase_07 AC 92 ms
33,480 KB
testcase_08 AC 139 ms
34,352 KB
testcase_09 AC 59 ms
30,592 KB
testcase_10 AC 196 ms
35,284 KB
testcase_11 AC 707 ms
47,404 KB
testcase_12 AC 407 ms
40,796 KB
testcase_13 AC 204 ms
35,380 KB
testcase_14 AC 47 ms
30,224 KB
testcase_15 AC 313 ms
37,224 KB
testcase_16 AC 783 ms
48,816 KB
testcase_17 AC 825 ms
49,380 KB
testcase_18 AC 190 ms
35,256 KB
testcase_19 AC 707 ms
47,684 KB
testcase_20 AC 69 ms
30,900 KB
testcase_21 AC 124 ms
34,112 KB
testcase_22 AC 577 ms
43,480 KB
testcase_23 AC 333 ms
37,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define lli long long int
#define REP(i,s,n) for(lli i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<62)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};


const int MOD = 1000000007;
using mint = Fp<MOD>;

// a^n mod を計算する
lli modpow(lli a, lli n, lli mod) {
    lli res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^-1
lli modinv(lli a, lli mod) {
    lli b = mod, u = 1, v = 0;
    while (b) {
        lli t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= mod;
    if (u < 0) u += mod;
    return u;
}

vector<lli> G[1000100];

bool vis[1000100];
mint dp[1000100];
mint ans;

void func(lli from,lli now){
    if(from == -1)dp[now]=1;
    else dp[now] = dp[from]+1;

    mint n = dp[now];
    ans = ans + (n-1)*n*modinv(2,MOD);

    for(auto e:G[now]){
        if(e==from)continue;
        func(now,e);
    }
}


int main(){

    lli n;
    cin>>n;

    REP(i,0,n-1){
        lli a,b;
        cin>>a>>b;
        a--,b--;
        G[a].push_back(b);
        vis[b]=true;
    }

    REP(i,0,n){
        if(vis[i])continue;
        func(-1,i);
    }
    cout<<ans<<endl;

    return 0;
}
0