結果

問題 No.1293 2種類の道路
ユーザー 👑 rin204rin204
提出日時 2022-11-11 15:48:10
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 2,281 bytes
コンパイル時間 3,306 ms
コンパイル使用メモリ 255,272 KB
実行使用メモリ 12,140 KB
最終ジャッジ日時 2023-10-11 12:39:23
合計ジャッジ時間 6,895 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 56 ms
11,676 KB
testcase_10 AC 57 ms
11,664 KB
testcase_11 AC 57 ms
11,540 KB
testcase_12 AC 56 ms
11,856 KB
testcase_13 AC 56 ms
11,584 KB
testcase_14 AC 33 ms
9,908 KB
testcase_15 AC 33 ms
10,048 KB
testcase_16 AC 48 ms
11,824 KB
testcase_17 AC 48 ms
12,140 KB
testcase_18 AC 31 ms
10,536 KB
testcase_19 AC 35 ms
10,956 KB
testcase_20 AC 35 ms
10,952 KB
testcase_21 AC 37 ms
6,936 KB
testcase_22 AC 37 ms
6,720 KB
testcase_23 AC 34 ms
6,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
}
    
    
0