結果
問題 | No.416 旅行会社 |
ユーザー |
![]() |
提出日時 | 2020-04-19 16:29:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 640 ms / 4,000 ms |
コード長 | 2,376 bytes |
コンパイル時間 | 3,998 ms |
コンパイル使用メモリ | 205,432 KB |
最終ジャッジ日時 | 2025-01-09 21:38:39 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define pb push_back #define eb emplace_back #define fs first #define sc second using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll MOD = 998244353LL; constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; template <class T> void chmin(T &a, T b) { a = min(a, b); } template <class T> void chmax(T &a, T b) { a = max(a, b); } void debug() { cerr << "ok" << endl; } template <class T> void printv(const vector<T> &v) { for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' '); } struct QuickFind { int n; vector<vector<int>> vs; vector<int> root; QuickFind(int n) : n(n), vs(n), root(n) { iota(root.begin(), root.end(), 0); for (int i = 0; i < n; i++) { vs[i].emplace_back(i); } } inline int size(int u) { return vs[root[u]].size(); } bool unite(int u, int v) { u = root[u], v = root[v]; if (u == v) return false; if (size(u) < size(v)) swap(u, v); for (auto &e : vs[v]) { vs[u].emplace_back(e); root[e] = u; } return true; } inline int same(int u, int v) { return root[u] == root[v]; } vector<int> &elements(int u) { return vs[root[u]]; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; set<P> st; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; --a; --b; st.insert(P(a, b)); } V<int> c(q), d(q), res(n); for (int i = 0; i < q; i++) { cin >> c[i] >> d[i]; --c[i]; --d[i]; st.erase(P(c[i], d[i])); } QuickFind uf(n); for (auto &e : st) { uf.unite(e.first, e.second); } for (auto &e : uf.elements(0)) { res[e] = -1; } for (int i = q - 1; i >= 0; i--) { if (uf.same(c[i], d[i])) continue; if (!uf.same(c[i], 0)) swap(c[i], d[i]); if (uf.same(c[i], 0)) { for (auto &e : uf.elements(d[i])) { res[e] = i + 1; } } uf.unite(c[i], d[i]); } for (int i = 1; i < n; i++) { cout << res[i] << '\n'; } }