結果
問題 |
No.416 旅行会社
|
ユーザー |
|
提出日時 | 2021-04-25 17:00:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 285 ms / 4,000 ms |
コード長 | 2,263 bytes |
コンパイル時間 | 1,718 ms |
コンパイル使用メモリ | 136,732 KB |
最終ジャッジ日時 | 2025-01-21 00:58:21 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <cstdlib> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <functional> #include <iomanip> #include <unordered_map> #include <memory.h> #include <unordered_set> #include <fstream> #include <random> using namespace std; const int MAXM = 200200; pair<int,int> edge[MAXM], del[MAXM]; int ans[MAXM]; int e2g[MAXM]; vector<int> g2e[MAXM]; bool isSame(int x, int y) { return e2g[x] == e2g[y]; } void unite(int x, int y) { if (isSame(x, y)) return; int gx = e2g[x], gy = e2g[y]; if (g2e[gx].size() < g2e[gy].size()) { swap(x, y); swap(gx, gy); } for (int el : g2e[gy]) e2g[el] = gx; g2e[gx].insert(g2e[gx].end(), g2e[gy].begin(), g2e[gy].end()); g2e[gy].clear(); } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, Q; cin >> N >> M >> Q; for (int i = 0; i < M; i++) { cin >> edge[i].first >> edge[i].second; edge[i].first--; edge[i].second--; } set<pair<int,int>> S; for (int i = 0; i < Q; i++) { cin >> del[i].first >> del[i].second; del[i].first--; del[i].second--; S.insert(del[i]); } for (int i = 0; i < N; i++) { e2g[i] = i; g2e[i].push_back(i); } for (int i = 0; i < M; i++) { if (S.find(edge[i]) == S.end()) { unite(edge[i].first, edge[i].second); } } { int g = e2g[0]; for (int el : g2e[g]) ans[el] = -1; } for (int i = Q - 1; i >= 0; i--) { int a = del[i].first, b = del[i].second; if (!isSame(a, b)) { if (!isSame(0, a) && !isSame(0, b)) { unite(a, b); } else { if (isSame(0, b)) { swap(a, b); } int gb = e2g[b]; for (int el : g2e[gb]) ans[el] = i + 1; unite(a, b); } } } for (int i = 1; i < N; i++) cout << ans[i] << '\n'; return 0; }