結果

問題 No.1817 Reversed Edges
ユーザー ktr216ktr216
提出日時 2022-01-21 22:08:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 2,028 bytes
コンパイル時間 1,671 ms
コンパイル使用メモリ 173,516 KB
実行使用メモリ 258,108 KB
最終ジャッジ日時 2023-08-17 06:11:53
合計ジャッジ時間 9,151 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
245,208 KB
testcase_01 AC 96 ms
245,472 KB
testcase_02 AC 94 ms
245,404 KB
testcase_03 AC 94 ms
245,320 KB
testcase_04 AC 95 ms
245,568 KB
testcase_05 AC 94 ms
245,388 KB
testcase_06 AC 95 ms
245,268 KB
testcase_07 AC 279 ms
249,792 KB
testcase_08 AC 187 ms
247,568 KB
testcase_09 AC 276 ms
249,464 KB
testcase_10 AC 198 ms
247,824 KB
testcase_11 AC 229 ms
248,544 KB
testcase_12 AC 306 ms
250,324 KB
testcase_13 AC 306 ms
250,452 KB
testcase_14 AC 302 ms
250,396 KB
testcase_15 AC 309 ms
250,200 KB
testcase_16 AC 314 ms
250,256 KB
testcase_17 AC 310 ms
250,392 KB
testcase_18 AC 307 ms
250,392 KB
testcase_19 AC 313 ms
250,392 KB
testcase_20 AC 313 ms
250,276 KB
testcase_21 AC 309 ms
250,256 KB
testcase_22 AC 280 ms
250,572 KB
testcase_23 AC 280 ms
250,536 KB
testcase_24 AC 297 ms
258,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
using VI = vector<int>;
using P = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define ALL(a) (a).begin(),(a).end()

constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};

template< typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true); }
template< typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true); }

const ll MOD = 1000000007;

int N, MAX_N = 2000001, idx = 0, MAX_LOG_V = 20;
vector<vector<int>> edge(MAX_N), parent(MAX_LOG_V, vector<int>(MAX_N, -1));
vector<int> tp(MAX_N), l(MAX_N), r(MAX_N), par(MAX_N, -1), depth(MAX_N, 0);
vector<bool> done(MAX_N, false);

void tps(int u) {
    done[u] = true;
    l[u] = idx;
    tp[idx] = u;
    idx++;
    REP(i, edge[u].size()) {
        int v = edge[u][i];
        if (done[v]) continue;
        par[v] = u;
        depth[v] = depth[u] + 1;
        tps(v);
    }
    r[u] = idx;
}

int main() {
    int N;
    cin >> N;
    vector<int> A(N - 1), B(N - 1);
    REP(i, N - 1) cin >> A[i] >> B[i];
    REP(i, N - 1) {
        edge[A[i] - 1].push_back(B[i] - 1);
        edge[B[i] - 1].push_back(A[i] - 1);
    }
    tps(0);
    vector<int> C(N, 0), D(N, 0);
    for (int i = N - 1; i > 0; i--) {
        int u = tp[i], v = par[u];
        C[v] += C[u];
        if (u < v) C[v]++;
    }
    FOR(i, 1, N) {
        int u = tp[i], v = par[u];
        D[u] = D[v];
        if (u > v) D[u]++;
        else D[u]--;
    }
    /*
    REP(i, N) cout << tp[i] << " "; cout << "\n";
    REP(i, N) cout << par[i] << " "; cout << "\n";
    REP(i, N) cout << l[i] << " "; cout << "\n";
    REP(i, N) cout << r[i] << " "; cout << "\n";
    REP(i, N) cout << C[i] << " "; cout << endl;
    REP(i, N) cout << D[i] << " "; cout << endl;
    */
    REP(i, N) cout << C[0] + D[i] << endl;
}
0