結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2020-09-10 11:19:07 |
言語 | C++17 (gcc 12.2.0 + boost 1.81.0) |
結果 |
AC
|
実行時間 | 64 ms / 3,000 ms |
コード長 | 2,178 bytes |
コンパイル時間 | 963 ms |
コンパイル使用メモリ | 87,060 KB |
実行使用メモリ | 12,744 KB |
最終ジャッジ日時 | 2023-08-24 14:48:35 |
合計ジャッジ時間 | 3,268 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,376 KB |
testcase_01 | AC | 2 ms
4,380 KB |
testcase_02 | AC | 2 ms
4,380 KB |
testcase_03 | AC | 1 ms
4,380 KB |
testcase_04 | AC | 2 ms
4,376 KB |
testcase_05 | AC | 2 ms
4,384 KB |
testcase_06 | AC | 1 ms
4,376 KB |
testcase_07 | AC | 2 ms
4,504 KB |
testcase_08 | AC | 1 ms
4,376 KB |
testcase_09 | AC | 56 ms
9,556 KB |
testcase_10 | AC | 48 ms
12,744 KB |
testcase_11 | AC | 60 ms
12,348 KB |
testcase_12 | AC | 58 ms
11,484 KB |
testcase_13 | AC | 64 ms
11,208 KB |
testcase_14 | AC | 58 ms
11,432 KB |
testcase_15 | AC | 61 ms
10,940 KB |
testcase_16 | AC | 59 ms
11,144 KB |
testcase_17 | AC | 56 ms
10,936 KB |
testcase_18 | AC | 60 ms
10,956 KB |
testcase_19 | AC | 58 ms
10,916 KB |
ソースコード
#include <iostream> #include <vector> #include <queue> template <class Cost = int> struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; } bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; } }; template <class Cost = int> struct Graph { std::vector<std::vector<Edge<Cost>>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; } std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; } }; template <class Cost> std::vector<int> bfs(const Graph<Cost>& graph, std::vector<int> ss) { std::vector<Cost> dist(graph.size(), -1); std::queue<int> que; for (auto s : ss) { dist[s] = 0; que.push(s); } while (!que.empty()) { int v = que.front(); que.pop(); for (const auto& e : graph[v]) { if (dist[e.dst] != -1) continue; dist[e.dst] = dist[v] + e.cost; que.push(e.dst); } } return dist; } void solve() { int n; std::cin >> n; Graph<> graph(n); for (int i = 0; i < n - 1; ++i) { int u, v; std::cin >> u >> v; graph.span(false, --u, --v); } auto rs = bfs(graph, {0}); std::vector<int> leaves; for (int v = 0; v < n; ++v) { bool isleaf = true; for (auto e : graph[v]) { if (rs[v] < rs[e.dst]) isleaf = false; } if (isleaf) leaves.push_back(v); } auto ls = bfs(graph, leaves); for (int v = 0; v < n; ++v) { std::cout << std::min(rs[v], ls[v]) << "\n"; } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }