結果
問題 |
No.3200 Sinking Islands
|
ユーザー |
|
提出日時 | 2025-07-11 21:50:05 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 86 ms / 2,000 ms |
コード長 | 1,722 bytes |
コンパイル時間 | 2,206 ms |
コンパイル使用メモリ | 204,872 KB |
実行使用メモリ | 9,296 KB |
最終ジャッジ日時 | 2025-07-11 21:50:14 |
合計ジャッジ時間 | 5,947 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; class UnionFind{ private: vector<int> par,siz; public: UnionFind(int N){ par.resize(N,-1); siz.resize(N,1); } int root(int x){ //連結成分の代表頂点を返す. if(par.at(x) == -1) return x; else return par.at(x) = root(par.at(x)); } long long unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す. u = root(u),v = root(v); if(u == v) return 0; long long ret = 1LL*siz.at(u)*siz.at(v); if(siz.at(u) < siz.at(v)) swap(u,v); //Union by size. par.at(v) = u; siz.at(u) += siz.at(v); return ret; } bool issame(int u, int v){ //同じ連結成分ならtrue. if(root(u) == root(v)) return true; else return false; } int size(int pos){return siz.at(root(pos));} //posの連結成分の大きさを返す. }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; vector<pair<int,int>> UV(M); for(auto &[u,v] : UV) cin >> u >> v,u--,v--; int Q; cin >> Q; vector<bool> alive(M,true); vector<int> Qs(Q); for(auto &b : Qs) cin >> b,b--,alive.at(b) = false; long long answer = 0; UnionFind Z(N); for(int i=0; i<M; i++) if(alive.at(i)){ auto [u,v] = UV.at(i); answer += Z.unite(u,v); } vector<long long> Ans; Ans.push_back(answer); for(int i=Q-1; i>0; i--){ int b = Qs.at(i); auto [u,v] = UV.at(b); answer += Z.unite(u,v); Ans.push_back(answer); } reverse(Ans.begin(),Ans.end()); for(auto &a : Ans) cout << 1LL*N*(N-1)/2-a << "\n"; }