結果
| 問題 |
No.2427 Tree Distance Two
|
| コンテスト | |
| ユーザー |
nono00
|
| 提出日時 | 2023-08-18 21:45:27 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 671 ms / 2,000 ms |
| コード長 | 5,219 bytes |
| コンパイル時間 | 3,848 ms |
| コンパイル使用メモリ | 254,592 KB |
| 実行使用メモリ | 38,344 KB |
| 最終ジャッジ日時 | 2024-11-28 06:26:08 |
| 合計ジャッジ時間 | 14,916 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
// graph/graph-template.hpp
// graph/graph.hpp
#include <cassert>
#include <vector>
// graph/edge.hpp
namespace nono {
// brief:
// - Edge構造体
//
// tparam:
// - `T`: 重みの型
//
// note:
// - `from`, `to`, `weight` がないと、他のライブラリが壊れる
template <class T = int>
struct Edge {
using value_type = T;
int from;
int to;
T weight;
int index;
// brief:
// - Edgeコンストラクタ
Edge(int from, int to, const T& weight = 1, int index = -1)
: from(from),
to(to),
weight(weight),
index(index) {}
friend auto operator<=>(const Edge<T>& lhs, const Edge<T>& rhs) {
return lhs.weight <=> rhs.weight;
}
};
} // namespace nono
namespace nono {
// brief:
// - グラフ構造体
//
// tparam:
// - `T`: 辺の重みの型
// - `directed`: 有向かどうか
//
// note:
// - 有向グラフのaliasとして `DiGraph` が存在する
// - 以下の条件が満たさないと壊れる
// \ \ 1. `size()` が頂点数を返す
// \ \ 2. `operator[]` が `range-based for` に対応している
template <class T = int, bool directed = false>
class Graph {
using EdgeType = Edge<T>;
using EdgesType = std::vector<EdgeType>;
public:
using value_type = EdgesType;
Graph() = default;
explicit Graph(int vertex_size)
: vertex_size_(vertex_size),
edge_size_(0),
adj_list_(vertex_size) {}
Graph(int vertex_size, const std::vector<EdgeType>& edges)
: vertex_size_(vertex_size),
edge_size_(0),
adj_list_(vertex_size) {
for (const auto& e: edges) {
adj_list_[e.from].emplace_back(e);
}
}
// brief:
// - 頂点数を取得する
int size() const {
return vertex_size_;
}
// brief:
// - 頂点 `i` に隣接する頂点を取得する
const std::vector<EdgeType>& operator[](int i) const {
return adj_list_[i];
}
// brief:
// - 頂点 `i` に隣接する頂点を取得する
std::vector<EdgeType>& operator[](int i) {
return adj_list_[i];
}
// brief:
// - グラフに辺を追加する
//
// note:
// - weightのデフォルト値は1
void add_edge(int from, int to, T weight = static_cast<T>(1), int index = -1) {
adj_list_[from].emplace_back(from, to, weight, index);
if (not directed) adj_list_[to].emplace_back(to, from, weight, index);
edge_size_++;
}
// brief:
// - 頂点 `i` の次数を取得する
//
// note:
// - 有向グラフの場合、出次数を取得する
int degree(int i) const {
return adj_list_[i].size();
}
// brief:
// - 頂点の次数の配列を取得する
//
// note:
// - 有向グラフの場合、出次数を取得する
std::vector<int> degree() const {
std::vector<int> result(vertex_size_);
for (int i = 0; i < vertex_size_; i++) {
result[i] = adj_list_[i].size();
}
return result;
}
// brief:
// - 辺の配列に取得する
//
// note:
// - 単純グラフでないと壊れる
std::vector<EdgeType> to_edges() const {
std::vector<EdgeType> edges(edge_size_);
int count = 0;
for (int u = 0; u < vertex_size_; u++) {
for (const auto& e: adj_list_[u]) {
if (directed || e.from <= e.to) {
edges[count] = e;
count++;
}
}
}
return edges;
}
private:
int vertex_size_;
int edge_size_;
std::vector<std::vector<EdgeType>> adj_list_;
};
// brief:
// - 有向グラフ構造体
//
// tparam:
// - `T`: 辺の重みの型
//
// note:
// - `Graph` のalias
template <class T = int>
using DiGraph = Graph<T, true>;
} // namespace nono
namespace nono {
void solve() {
int n;
std::cin >> n;
Graph tree(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
tree.add_edge(u, v);
}
std::vector<int> parent(n);
std::vector<int> child(n);
std::vector<int> depth(n);
auto dfs = [&](const auto& dfs, const auto& graph, int u, int p) -> void {
parent[u] = p;
for (const auto& e: graph[u]) {
if (e.to == p) continue;
depth[e.to] = depth[e.from] + 1;
dfs(dfs, graph, e.to, e.from);
child[u]++;
}
};
dfs(dfs, tree, 0, -1);
for (int i = 0; i < n; i++) {
int ans = 0;
if (depth[i] >= 2) {
ans++;
}
if (parent[i] != -1) {
ans += child[parent[i]] - 1;
}
for (const auto& e: tree[i]) {
if (e.to == parent[i]) continue;
ans += child[e.to];
}
std::cout << ans << std::endl;
}
}
} // namespace nono
int main() {
std::cin.tie(0)->sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(15);
int t = 1;
// std::cin >> t;
while (t--) nono::solve();
}
nono00