結果

問題 No.1103 Directed Length Sum
ユーザー shell_mugshell_mug
提出日時 2020-07-03 22:33:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 671 ms / 3,000 ms
コード長 2,580 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 124,572 KB
実行使用メモリ 89,528 KB
最終ジャッジ日時 2023-10-17 05:34:11
合計ジャッジ時間 8,052 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 299 ms
89,528 KB
testcase_03 AC 198 ms
61,956 KB
testcase_04 AC 365 ms
40,780 KB
testcase_05 AC 671 ms
68,612 KB
testcase_06 AC 222 ms
27,656 KB
testcase_07 AC 37 ms
9,184 KB
testcase_08 AC 65 ms
12,088 KB
testcase_09 AC 22 ms
6,808 KB
testcase_10 AC 97 ms
15,452 KB
testcase_11 AC 411 ms
44,408 KB
testcase_12 AC 235 ms
27,920 KB
testcase_13 AC 106 ms
15,980 KB
testcase_14 AC 17 ms
6,016 KB
testcase_15 AC 172 ms
22,444 KB
testcase_16 AC 466 ms
49,288 KB
testcase_17 AC 489 ms
51,332 KB
testcase_18 AC 99 ms
15,452 KB
testcase_19 AC 420 ms
45,464 KB
testcase_20 AC 27 ms
7,600 KB
testcase_21 AC 57 ms
11,296 KB
testcase_22 AC 335 ms
37,680 KB
testcase_23 AC 182 ms
23,764 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:92:16: warning: 'top' may be used uninitialized [-Wmaybe-uninitialized]
   92 |     rep (i, n) if (i != top) ans = (ans + num[i] * depth[i]) % MOD;
      |                ^~
main.cpp:59:9: note: 'top' was declared here
   59 |     int top;
      |         ^~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
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; }
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> parent(n, -1);
    vector<P> e(n - 1);
    vector<vector<int>> graph(n);
    rep (i, n - 1) {
        cin >> e[i].first >> e[i].second;
        e[i].first--; e[i].second--;
        graph[e[i].first].push_back(e[i].second);
        parent[e[i].second] = e[i].first;
    }
    int top;
    rep (i, n) {
        if (parent[i] == -1) {
            top = i;
            break;
        }
    }

    ll MOD = (ll)1e9 + 7;
    vector<ll> num(n), depth(n);
    vector<bool> used(n);
    stack<int> st;
    st.push(top);
    while (!st.empty()) {
        int a = st.top();
        eprintf("%d ", a);
        if (!used[a] && !graph[a].empty()) {
            for (int i : graph[a]) {
                st.push(i);
                depth[i] = depth[a] + 1;
            }
            used[a] = true;
        } else {
            num[a]++;
            for (int i : graph[a]) num[a] = (num[a] + num[i]) % MOD;
            st.pop();
        }
    }
    eprintf("\n");

    ll ans = 0;
    rep (i, n) eprintf("%lld%c", num[i], " \n"[i == i_len - 1]);
    rep (i, n) eprintf("%lld%c", depth[i], " \n"[i == i_len - 1]);
    rep (i, n) if (i != top) ans = (ans + num[i] * depth[i]) % MOD;
    cout << ans << "\n";
    return 0;
}
0