結果

問題 No.1293 2種類の道路
ユーザー 👑 jupirojupiro
提出日時 2020-11-21 03:15:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 2,661 bytes
コンパイル時間 1,360 ms
コンパイル使用メモリ 140,452 KB
実行使用メモリ 11,024 KB
最終ジャッジ日時 2023-09-30 20:33:03
合計ジャッジ時間 3,323 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 55 ms
10,488 KB
testcase_10 AC 55 ms
10,248 KB
testcase_11 AC 56 ms
10,180 KB
testcase_12 AC 56 ms
10,348 KB
testcase_13 AC 55 ms
10,644 KB
testcase_14 AC 32 ms
8,844 KB
testcase_15 AC 32 ms
8,904 KB
testcase_16 AC 46 ms
10,844 KB
testcase_17 AC 47 ms
10,576 KB
testcase_18 AC 31 ms
9,812 KB
testcase_19 AC 36 ms
10,980 KB
testcase_20 AC 36 ms
11,024 KB
testcase_21 AC 38 ms
8,240 KB
testcase_22 AC 38 ms
8,556 KB
testcase_23 AC 33 ms
8,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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