結果
問題 | No.1817 Reversed Edges |
ユーザー |
|
提出日時 | 2022-01-21 22:08:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 391 ms / 2,000 ms |
コード長 | 2,028 bytes |
コンパイル時間 | 1,582 ms |
コンパイル使用メモリ | 175,256 KB |
実行使用メモリ | 258,176 KB |
最終ジャッジ日時 | 2024-11-26 00:30:32 |
合計ジャッジ時間 | 11,127 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 |
ソースコード
#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;}