結果
| 問題 |
No.828 全方位神童数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-11 02:32:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,121 bytes |
| コンパイル時間 | 876 ms |
| コンパイル使用メモリ | 79,840 KB |
| 最終ジャッジ日時 | 2025-01-07 01:38:17 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 43 |
ソースコード
#include <vector>
#include <iostream>
#include <cassert>
#include <numeric>
class UnionFind
{
public:
UnionFind(const std::size_t v) : V(v), parent(v), size(v, 1) { std::iota(parent.begin(), parent.end(), 0); }
std::size_t find(const std::size_t a) { return assert(a < V), parent[a] == a ? a : parent[a] = find(parent[a]); }
bool same(const std::size_t a, const std::size_t b)
{
assert(a < V), assert(b < V);
return find(a) == find(b);
}
void unite(std::size_t a, std::size_t b)
{
assert(a < V), assert(b < V);
a = find(a), b = find(b);
if (a == b) { return; }
if (size[a] < size[b]) { std::swap(a, b); }
size[a] += size[b], parent[b] = a;
}
std::size_t getSize(const std::size_t a) { return assert(a < V), size[find(a)]; }
void debugPrint() const
{
std::cerr << "[";
for (std::size_t i = 0; i < parent.size(); i++) { std::cerr << parent[i] << (i + 1 == parent.size() ? "" : ","); }
std::cerr << "]\n";
}
private:
const std::size_t V;
std::vector<std::size_t> parent, size;
};
int main()
{
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::size_t N;
std::cin >> N;
std::vector<std::vector<std::size_t>> G(N);
for (std::size_t i = 0; i < N - 1; i++) {
std::size_t u, v;
std::cin >> u >> v, u--, v--;
G[u].push_back(v), G[v].push_back(u);
}
UnionFind uf(N);
std::vector<std::size_t> max(N);
std::vector<std::vector<std::size_t>> G2(N);
for (std::size_t i = 0; i < N; i++) {
for (const std::size_t to : G[i]) {
if (to > i) { continue; }
const std::size_t c = max[uf.find(to)];
G2[i].push_back(c), uf.unite(i, c);
}
max[uf.find(i)] = i;
}
std::vector<std::size_t> ans(N, 1);
auto dfs = [&](auto&& self, const std::size_t s) -> void {
for (const std::size_t to : G2[s]) { ans[to] = ans[s] + 1, self(self, to); }
};
dfs(dfs, N - 1);
for (std::size_t i = 0; i < N; i++) { std::cout << ans[i] << "\n"; }
return 0;
}