結果
| 問題 | No.1293 2種類の道路 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-11-21 03:15:52 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 63 ms / 2,000 ms | 
| コード長 | 2,661 bytes | 
| コンパイル時間 | 1,712 ms | 
| コンパイル使用メモリ | 136,400 KB | 
| 最終ジャッジ日時 | 2025-01-16 03:47:21 | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>
using namespace std;
typedef long long ll;
using ull = unsigned long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 2100000;
class UnionFindUndo {
public:
	vector<int> parent;
	vector<pair<int, int>> history;
	UnionFindUndo(int n) {
		parent.assign(n, -1);
	}
	int root(int x) {
		if (parent[x] < 0) {
			return x;
		}
		return root(parent[x]);
	}
	long long size(int x) {
		return -(long long)parent[root(x)];
	}
	bool connect(int x, int y) {
		x = root(x);
		y = root(y);
		history.emplace_back(x, parent[x]);
		history.emplace_back(y, parent[y]);
		if (x == y) {
			return false;
		}
		if (size(x) < size(y)) {
			swap(x, y);
		}
		parent[x] += parent[y];
		parent[y] = x;
		return true;
	}
	void undo() {
		parent[history.back().first] = history.back().second;
		history.pop_back();
		parent[history.back().first] = history.back().second;
		history.pop_back();
	}
	void snapshot() {
		history.clear();
	}
	void rollback() {
		while (!history.empty()) undo();
	}
};
int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n, D, W; cin >> n >> D >> W;
	UnionFindUndo uf(n * 2);
	for (int i = 0; i < D; i++) {
		int u, v; cin >> u >> v;
		u--; v--;
		uf.connect(u, v);
	}
	for (int i = 0; i < W; i++) {
		int u, v; cin >> u >> v;
		u--; v--;
		uf.connect(u + n, v + n);
	}
	uf.snapshot();
	vector<vector<int>> g(n);
	
	for (int i = 0; i < n; i++) {
		g[uf.root(i)].emplace_back(i);
	}
	ll res = 0;
	for (auto& v : g) {
		if (v.size() == 0) continue;
		for (auto e : v) {
			uf.connect(e, e + n);
		}
		res += 1LL * v.size() * (((uf.size(v[0]) - v.size() * 2) + v.size()) - 1);
		uf.rollback();
	}
	cout << res << endl;
}
            
            
            
        