//全部橋を壊す→クエリを逆から処理→橋を復元して連結判定→UnionFindでできそう #include #include #include #include using namespace std; typedef pair 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

edges; P query[200000]; UF uf; int ans[200001]; //ans[i] = 島iについての答え (-1の場合は、n+1として保存) vector 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

::iterator it = edges.find(query[i]); if (it != edges.end()) { edges.erase(it); } } //今の連結状態をUnionFindに記録 uf.init(); for (set

::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; }