結果
| 問題 |
No.1293 2種類の道路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-11 15:48:10 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 60 ms / 2,000 ms |
| コード長 | 2,281 bytes |
| コンパイル時間 | 3,219 ms |
| コンパイル使用メモリ | 257,108 KB |
| 実行使用メモリ | 12,032 KB |
| 最終ジャッジ日時 | 2024-09-13 11:26:23 |
| 合計ジャッジ時間 | 6,080 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
#include<bits/stdc++.h>
// #include<atcoder/modint>
using namespace std;
// using namespace atcoder;
// using mint = modint998244353;
///// https://nyaannyaan.github.io/library/data-structure/rollback-union-find.hpp.html
#line 2 "data-structure/rollback-union-find.hpp"
struct RollbackUnionFind {
vector<int> data;
stack<pair<int, int> > history;
int inner_snap;
RollbackUnionFind(int sz) : inner_snap(0) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
history.emplace(x, data[x]);
history.emplace(y, data[y]);
if (x == y) return false;
if (data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
int find(int k) {
if (data[k] < 0) return k;
return find(data[k]);
}
int same(int x, int y) { return find(x) == find(y); }
int size(int k) { return (-data[find(k)]); }
void undo() {
data[history.top().first] = history.top().second;
history.pop();
data[history.top().first] = history.top().second;
history.pop();
}
void snapshot() { inner_snap = int(history.size() >> 1); }
int get_state() { return int(history.size() >> 1); }
void rollback(int state = -1) {
if (state == -1) state = inner_snap;
state <<= 1;
assert(state <= (int)history.size());
while (state < (int)history.size()) undo();
}
};
/**
* @brief RollbackつきUnion Find
* @docs docs/data-structure/rollback-union-find.md
*/
void solve(){
int n, d, w;
cin >> n >> d >> w;
RollbackUnionFind UF1(n);
RollbackUnionFind UF2(n);
int a, b;
for(int i = 0; i < d; i++){
cin >> a >> b;
UF1.unite(a - 1, b - 1);
}
for(int i = 0; i < w; i++){
cin >> a >> b;
UF2.unite(a - 1, b - 1);
}
vector<vector<int>> group(n, vector<int>());
for(int i = 0; i < n; i++){
group[UF1.find(i)].push_back(i);
}
long long ans = -n;
for(int i = 0; i < n; i++){
long long l1 = group[i].size();
if(l1 == 0) continue;
int ver = UF2.get_state();
int u = group[i][0];
for(int j = 1; j < l1; j++) UF2.unite(u, group[i][j]);
long long l2 = UF2.size(i);
ans += l1 * l2;
UF2.rollback(ver);
}
cout << ans << "\n";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t;
t = 1;
// cin >> t;
while(t--) solve();
}