結果
問題 |
No.812 Change of Class
|
ユーザー |
![]() |
提出日時 | 2019-04-12 23:14:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 398 ms / 4,000 ms |
コード長 | 2,889 bytes |
コンパイル時間 | 1,036 ms |
コンパイル使用メモリ | 89,660 KB |
実行使用メモリ | 12,172 KB |
最終ジャッジ日時 | 2024-06-12 19:54:49 |
合計ジャッジ時間 | 10,738 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 60 |
ソースコード
#include <iostream> #include <numeric> #include <vector> #include <queue> #include <tuple> #include <limits> template <class Cost = int> struct Edge { int from, to; Cost cost; Edge(int from = -1, int to = -1, Cost cost = 1) : from(from), to(to), 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> class Graph { public: int size; std::vector<std::vector<Edge<Cost>>> path; explicit Graph(int N = 0) : size(N), path(size) {} void span(int from, int to, Cost cost = 1) { path[from].push_back(Edge<Cost>(from, to, cost)); } std::vector<Edge<Cost>>& operator[](int v) { return path[v]; } }; const int INF = std::numeric_limits<int>::max() / 3; class UnionFind { private: std::vector<int> par, num; int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); } public: explicit UnionFind(int N) : par(N), num(N, 1) { std::iota(par.begin(), par.end(), 0); } void unite(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (num[u] < num[v]) std::swap(u, v); num[u] += num[v]; par[v] = u; } bool same(int u, int v) { return find(u) == find(v); } bool ispar(int v) { return v == find(v); } int size(int v) { return num[find(v)]; } }; template <class Cost> std::vector<Cost> dijkstra(Graph<Cost>& graph, std::vector<int> ss) { std::vector<Cost> dist(graph.size, INF); std::priority_queue<std::pair<int, Cost>, std::vector<std::pair<int, Cost>>, std::greater<std::pair<int, Cost>>> que; for (auto s : ss) { dist[s] = 0; que.emplace(0, s); } while (!que.empty()) { int v; Cost d; std::tie(d, v) = que.top(); que.pop(); if (d > dist[v]) continue; for (auto e : graph[v]) { if (dist[e.to] <= dist[v] + e.cost) continue; dist[e.to] = dist[v] + e.cost; que.emplace(dist[e.to], e.to); } } return dist; } int main() { int N, M; std::cin >> N >> M; Graph<> graph(N); UnionFind uf(N); for (int i = 0; i < M; ++i) { int p, q; std::cin >> p >> q; --p, --q; graph.span(p, q); graph.span(q, p); uf.unite(p, q); } int Q; std::cin >> Q; for (int q = 0; q < Q; ++q) { int a; std::cin >> a; --a; std::cout << uf.size(a) - 1 << " "; auto dist = dijkstra<>(graph, {a}); int max = 1; for (auto d : dist) { if (0 < d && d < INF) max = std::max(max, d); } int day; for (day = 0; (1 << day) < max; ++day) {} std::cout << day << std::endl; } return 0; }