結果
| 問題 |
No.1817 Reversed Edges
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-21 21:41:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 209 ms / 2,000 ms |
| コード長 | 1,014 bytes |
| コンパイル時間 | 2,616 ms |
| コンパイル使用メモリ | 202,224 KB |
| 最終ジャッジ日時 | 2025-01-27 13:32:54 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 23 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);
struct Edge {
int to;
Edge(const int to) : to(to) {}
};
using Graph = vector<vector<Edge>>;
void solve() {
int N;
cin >> N;
Graph G(N);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
vector<int> ans(N);
auto dfs1 = [&](auto dfs1, int u, int p) -> int {
int res = 0;
for (auto &[v] : G[u]) {
if (v == p) continue;
res += dfs1(dfs1, v, u) + (int)(u > v);
}
return res;
};
ans[0] = dfs1(dfs1, 0, -1);
auto dfs2 = [&](auto dfs2, int u, int p) -> void {
for (auto &[v] : G[u]) {
if (v == p) continue;
if (v > u) {
ans[v] = ans[u] + 1;
} else {
ans[v] = ans[u] - 1;
}
dfs2(dfs2, v, u);
}
};
dfs2(dfs2, 0, -1);
for (auto &a : ans) cout << a << endl;
}
int main() {
bokusunny;
solve();
return 0;
}