結果
問題 | No.2888 Mamehinata |
ユーザー |
![]() |
提出日時 | 2024-09-26 08:52:43 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 131 ms / 2,000 ms |
コード長 | 3,012 bytes |
コンパイル時間 | 3,831 ms |
コンパイル使用メモリ | 289,116 KB |
実行使用メモリ | 21,748 KB |
最終ジャッジ日時 | 2024-09-26 08:52:59 |
合計ジャッジ時間 | 9,515 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 52 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, a, b) for(ll i = a; i < b; ++i) #define rrep(i, a, b) for(ll i = a; i >= b; --i) constexpr ll inf = 4e18; struct SetupIO { SetupIO() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(30); } } setup_io; template <typename T> struct Edge { int from, to; T cost; int idx; Edge() : from(-1), to(-1), cost(-1), idx(-1) {} Edge(int from, int to, const T& cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template <typename T> struct Graph { Graph(int N) : n(N), es(0), g(N) {} int size() const { return n; } int edge_size() const { return es; } void add_edge(int from, int to, const T& cost = 1) { assert(0 <= from and from < n); assert(0 <= to and to < n); g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void add_directed_edge(int from, int to, const T& cost = 1) { assert(0 <= from and from < n); assert(0 <= to and to < n); g[from].emplace_back(from, to, cost, es++); } inline vector<Edge<T>>& operator[](const int& k) { assert(0 <= k and k < n); return g[k]; } inline const vector<Edge<T>>& operator[](const int& k) const { assert(0 <= k and k < n); return g[k]; } private: int n, es; vector<vector<Edge<T>>> g; }; template <typename T> using Edges = vector<Edge<T>>; template <typename T> vector<pair<T, int>> dijkstra(const Graph<T>& g, const int s = 0) { const int n = g.size(); assert(0 <= s and s < n); vector<pair<T, int>> d(n, {numeric_limits<T>::max(), -1}); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq; d[s] = {0, -1}; pq.emplace(0, s); while(!pq.empty()) { const auto [dist, cur] = pq.top(); pq.pop(); if(d[cur].first < dist) continue; for(const Edge<T>& e : g[cur]) { if(d[e.to].first > d[cur].first + e.cost) { d[e.to] = {d[cur].first + e.cost, cur}; pq.emplace(d[e.to].first, e.to); } } } return d; } int main(void) { int n, m; cin >> n >> m; Graph<int> g(n); rep(i, 0, m) { int u, v; cin >> u >> v; u--; v--; g.add_edge(u, v); } vector<pair<int, int>> d = dijkstra(g, 0); vector<int> ans(n + 1); int cnt = 0; rep(i, 0, n) { if(d[i].first == numeric_limits<int>::max()) continue; ans[d[i].first]++; cnt++; } if(cnt == 1) { rep(i, 1, n + 1) { cout << 0 << '\n'; } return 0; } rep(i, 1, n + 1) { if(i >= 2) { ans[i] += ans[i - 2]; } cout << ans[i] << '\n'; } }