結果
問題 | No.416 旅行会社 |
ユーザー |
|
提出日時 | 2020-04-15 18:39:49 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 178 ms / 4,000 ms |
コード長 | 2,982 bytes |
コンパイル時間 | 1,741 ms |
コンパイル使用メモリ | 135,204 KB |
実行使用メモリ | 15,872 KB |
最終ジャッジ日時 | 2024-12-14 20:45:24 |
合計ジャッジ時間 | 4,909 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) { return A; } return Parent[A] = root(Parent[A]); } long long size(int A) { return -(long long)Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) { swap(A, B); } Parent[A] += Parent[B]; Parent[B] = A; return true; } }; vector<vector<int>> g; void dfs(int cur, vector<int>& res, int idx) { if (res[cur] != 0) return; res[cur] = idx; for (auto next : g[cur]) { dfs(next, res, idx); } } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ int n, m, Q; scanf("%d %d %d", &n, &m, &Q); g.resize(n); vector<pair<int, int>> vp(m); for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; if (u > v) swap(u, v); vp[i] = { u,v }; } sort(vp.begin(), vp.end()); vector<bool> used(m, false); vector<pair<int, int>> q(Q); for (int i = 0; i < Q; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; if (u > v) swap(u, v); q[i] = make_pair(u, v); int idx = lower_bound(vp.begin(), vp.end(), q[i]) - vp.begin(); used[idx] = true; } UnionFind uni(n); for (int i = 0; i < m; i++) { if (!used[i]) { uni.connect(vp[i].first, vp[i].second); g[vp[i].first].push_back(vp[i].second); g[vp[i].second].push_back(vp[i].first); } } vector<int> res(n); for (int i = 1; i < n; i++) { if (uni.root(0) == uni.root(i)) { res[i] = -1; } } for (int i = Q - 1; i >= 0; i--) { if (uni.root(q[i].first) == uni.root(q[i].second)) continue; bool f = (uni.root(0) == uni.root(q[i].first)); bool s = (uni.root(0) == uni.root(q[i].second)); if (!f and s) { dfs(q[i].first, res, i + 1); } else if (f and !s) { dfs(q[i].second, res, i + 1); } uni.connect(q[i].first, q[i].second); g[q[i].first].push_back(q[i].second); g[q[i].second].push_back(q[i].first); } for (int i = 1; i < n; i++) { printf("%d\n",res[i]); } return 0; }