結果

問題 No.1103 Directed Length Sum
ユーザー finefine
提出日時 2020-07-03 21:48:19
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 657 ms / 3,000 ms
コード長 4,195 bytes
コンパイル時間 1,709 ms
コンパイル使用メモリ 174,852 KB
実行使用メモリ 155,704 KB
最終ジャッジ日時 2024-09-17 00:06:35
合計ジャッジ時間 8,579 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

using Graph = vector< vector<int> >;

bool dfs(int cur, vector<int>& memo, Graph& g, vector<int>& order) {
    memo[cur] = 1;
    for (int nex : g[cur]) {
        if (memo[nex] == 2) continue;
        if (memo[nex] == 1) return false;
        if (!dfs(nex, memo, g, order)) return false;
    }
    memo[cur] = 2;
    order.push_back(cur);
    return true;
}

bool tsort(Graph& g, vector<int>& order) {
    vector<int> memo(g.size(), 0);
    for (int i = 0; i < g.size(); i++) {
        if (memo[i] != 0) continue;
        if (!dfs(i, memo, g, order)) return false;
    }
    // reverse(order.begin(), order.end());
    return true;
}

// https://noshi91.hatenablog.com/entry/2019/03/31/174006
template <std::uint_fast64_t Modulus>
struct ModInt {
    using u64 = std::uint_fast64_t;

    static constexpr u64 MOD = Modulus;

    u64 val;

    constexpr ModInt(const u64 x = 0) noexcept : val(x % MOD) {}

    constexpr ModInt operator+() const noexcept { return ModInt(*this); }
    constexpr ModInt operator-() const noexcept {
        ModInt res(*this);
        if (res.val != 0) res.val = MOD - res.val;
        return res;
    }

    constexpr bool operator==(const ModInt& rhs) const noexcept { return val == rhs.val; }
    constexpr bool operator!=(const ModInt& rhs) const noexcept { return val != rhs.val; }

    // prefix increment/decrement
    constexpr ModInt& operator++() noexcept { return *this += ModInt(1); }
    constexpr ModInt& operator--() noexcept { return *this -= ModInt(1); }

    // postfix increment/decrement
    constexpr ModInt operator++(int) noexcept {
        ModInt tmp(*this);
        ++*this;
        return tmp;
    }
    constexpr ModInt operator--(int) noexcept {
        ModInt tmp(*this);
        --*this;
        return tmp;
    }

    constexpr ModInt operator+(const ModInt& rhs) const noexcept {
        return ModInt(*this) += rhs;
    }
    constexpr ModInt operator-(const ModInt& rhs) const noexcept {
        return ModInt(*this) -= rhs;
    }
    constexpr ModInt operator*(const ModInt& rhs) const noexcept {
        return ModInt(*this) *= rhs;
    }
    constexpr ModInt operator/(const ModInt& rhs) const noexcept {
        return ModInt(*this) /= rhs;
    }

    constexpr ModInt& operator+=(const ModInt& rhs) noexcept {
        val += rhs.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr ModInt& operator-=(const ModInt& rhs) noexcept {
        if (val < rhs.val) val += MOD;
        val -= rhs.val;
        return *this;
    }
    constexpr ModInt& operator*=(const ModInt& rhs) noexcept {
        val = val * rhs.val % MOD;
        return *this;
    }

    // prime Modulus only
    constexpr ModInt& operator/=(const ModInt& rhs) noexcept {
        return *this *= rhs.inv();
    }

    // prime Modulus only
    constexpr ModInt inv() const noexcept {
        return pow(*this, MOD - 2);
    }
};

template<std::uint_fast64_t Modulus>
constexpr ModInt<Modulus> pow(ModInt<Modulus> x, std::uint_fast64_t n) {
    ModInt<Modulus> res(1);
    while (n) {
        if (n & 1) res *= x;
        x *= x;
        n >>= 1;
    }
    return res;
}

template<std::uint_fast64_t Modulus>
istream& operator>>(istream& is, ModInt<Modulus>& x) {
    std::uint_fast64_t val;
    is >> val;
    x = ModInt<Modulus>(val);
    return is;
}

template<std::uint_fast64_t Modulus>
ostream& operator<<(ostream& os, const ModInt<Modulus>& x) {
    return os << x.val;
}

using mint = ModInt<1000000007>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    Graph g(n);
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        --a; --b;

        g[a].push_back(b);
    }

    vector<int> order;
    tsort(g, order);

    vector<mint> sum(n, 0), cnt(n, 0);
    mint ans = 0;
    for (int i : order) {
        for (int j : g[i]) {
            cnt[i] += cnt[j] + 1;
            sum[i] += sum[j] + cnt[j] + 1;
        }
        // cerr << i << ": " << sum[i] << " " << cnt[i] << newl;
        ans += sum[i];
    }
    cout << ans << newl;

    return 0;
}
0