結果

問題 No.1103 Directed Length Sum
ユーザー mikianaaamikianaaa
提出日時 2020-08-20 15:57:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 861 ms / 3,000 ms
コード長 3,936 bytes
コンパイル時間 1,756 ms
コンパイル使用メモリ 174,028 KB
実行使用メモリ 89,612 KB
最終ジャッジ日時 2024-04-21 13:32:27
合計ジャッジ時間 8,929 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 310 ms
73,576 KB
testcase_03 AC 248 ms
89,612 KB
testcase_04 AC 449 ms
46,800 KB
testcase_05 AC 861 ms
78,584 KB
testcase_06 AC 186 ms
31,448 KB
testcase_07 AC 34 ms
10,112 KB
testcase_08 AC 63 ms
13,696 KB
testcase_09 AC 25 ms
7,552 KB
testcase_10 AC 93 ms
17,504 KB
testcase_11 AC 465 ms
50,720 KB
testcase_12 AC 224 ms
31,804 KB
testcase_13 AC 86 ms
17,920 KB
testcase_14 AC 16 ms
6,656 KB
testcase_15 AC 149 ms
25,600 KB
testcase_16 AC 523 ms
56,696 KB
testcase_17 AC 516 ms
58,880 KB
testcase_18 AC 96 ms
17,408 KB
testcase_19 AC 486 ms
51,936 KB
testcase_20 AC 27 ms
8,448 KB
testcase_21 AC 49 ms
12,672 KB
testcase_22 AC 318 ms
42,884 KB
testcase_23 AC 157 ms
27,008 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/string:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bitset:52,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/x86_64-pc-linux-gnu/bits/stdc++.h:52,
                 from main.cpp:2:
In member function 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = long long int; _Args = {const long long int&}; _Tp = long long int]',
    inlined from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = long long int; _Args = {const long long int&}; _Tp = long long int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/alloc_traits.h:537:17,
    inlined from 'void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = long long int; _Alloc = std::allocator<long long int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/stl_deque.h:1543:30,
    inlined from 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = long long int; _Sequence = std::deque<long long int, std::allocator<long long int> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/stl_queue.h:286:20,
    inlined from 'int main()' at main.cpp:119:13:
/home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/new_allocator.h:187:11: warning: 's' may be used uninitialized [-Wmaybe-uninitialized]
  187 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:106:8: note: 's' was declared here
  106 |     ll s;
      |        ^

ソースコード

diff #

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

// modint (1000000007 で割ったあまりを扱う構造体)
template <int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0)
            v += 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 istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
        return is >> 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>;

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

    int N;
    cin >> N;
    vector<vector<ll>> G(N, vector<ll>());
    vector<ll> root(N, -1);
    rep(i, N - 1) {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
        root[b] = a;
    }

    ll s;
    rep(i, N) {
        if (root[i] == -1) {
            s = i;
            break;
        }
    }

    vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<ll> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[s] = 0;
    que.push(s); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1)
                continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    mint ans = 0;
    rep(i, N) {
        mint tmp = dist[i];
        if (dist[i] >= 2) {
            tmp = (1 + dist[i]) * dist[i] / 2;
        }

        ans += tmp;
    }

    cout << ans << endl;
}
0