結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
startcpp
|
| 提出日時 | 2016-08-27 00:23:56 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 701 ms / 4,000 ms |
| コード長 | 2,062 bytes |
| コンパイル時間 | 1,356 ms |
| コンパイル使用メモリ | 77,524 KB |
| 実行使用メモリ | 30,976 KB |
| 最終ジャッジ日時 | 2024-12-14 19:55:56 |
| 合計ジャッジ時間 | 9,050 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
//でばっぐ出力を消し忘れ奴
//全部橋を壊す→クエリを逆から処理→橋を復元して連結判定→UnionFindでできそう
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
class UF {
public:
int pare[200001];
void init() { for (int i = 0; i < 200001; i++) pare[i] = i; }
int root(int x) { if (pare[x] == x) return x; return (pare[x] = root(pare[x])); }
void marge(int x, int y) { x = root(x); y = root(y); pare[x] = y; }
bool isSame(int x, int y) { return root(x) == root(y); }
};
int n, m, q;
set<P> edges;
P query[200000];
UF uf;
int ans[200001]; //ans[i] = 島iについての答え (-1の場合は、n+1として保存)
vector<int> et[200001];
void dfs(int u, int q) {
if (ans[u] > 0) return;
ans[u] = max(ans[u], q);
for (int i = 0; i < et[u].size(); i++) {
if (uf.isSame(1, et[u][i])) {
dfs(et[u][i], q);
}
}
}
int main() {
int i;
cin >> n >> m >> q;
//橋を構築
for (i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
edges.insert(P(u, v));
et[u].push_back(v);
et[v].push_back(u);
}
//橋を壊す&クエリを保存
for (i = 0; i < q; i++) {
cin >> query[i].first >> query[i].second;
set<P>::iterator it = edges.find(query[i]);
if (it != edges.end()) { edges.erase(it); }
}
//今の連結状態をUnionFindに記録
uf.init();
for (set<P>::iterator it = edges.begin(); it != edges.end(); it++) {
int u = (*it).first;
int v = (*it).second;
//cout << "edge ; " << u << " " << v << endl;
uf.marge(u, v);
}
//最終状態でも連結している島を求める
for (i = 2; i <= n; i++) {
if (uf.isSame(1, i)) { ans[i] = q + 1; }
}
//クエリを逆から処理する
for (i = q - 1; i >= 0; i--) {
int u = query[i].first;
int v = query[i].second;
uf.marge(u, v);
if (uf.isSame(1, u)) { dfs(u, i + 1); }
if (uf.isSame(1, v)) { dfs(v, i + 1); }
}
//答えを出力する
for (i = 2; i <= n; i++) {
if (ans[i] > q) cout << -1 << endl;
else cout << ans[i] << endl;
}
return 0;
}
startcpp