Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <string>
- #include <set>
- #include <cmath>
- #include <map>
- #include <unordered_map>
- #include <numeric>
- #include <random>
- #include <memory>
- #include <chrono>
- #include <iterator>
- #include <functional>
- #include <unordered_set>
- #include <cassert>
- #include <cstring>
- #ifdef LOCAL
- #include "debug.h"
- #else
- #define debug(x...)
- #endif
- //#pragma GCC optimize("Ofast")
- //#define int ll
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef pair < int, int > pii;
- typedef pair < ll, ll > pll;
- #define sz(x) int((x).size())
- #ifndef LOCAL
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- #else
- mt19937 rng(228);
- #endif
- const int N = 2e5 + 7;
- const int inf = INT_MAX / 2;
- const ll INF = LLONG_MAX / 3;
- const int MOD = 1e9 + 7;
- const double eps = 1e-6;
- const string cars[] = {"🚗", "🚕", "🚙"};
- struct DSU {
- int p[N], rank[N];
- char c[N];
- ll s[N], cnt, n;
- DSU(int _n) {
- cnt = n = _n;
- for (int i = 1; i <= n; i++) {
- p[i] = i;
- s[i] = 1;
- }
- }
- inline int find(int u) {
- return (p[u] == u ? u : p[u] = find(p[u]));
- }
- bool join(int u, int v) {
- u = find(u);
- v = find(v);
- if (u == v) {
- if (c[u]) {
- return false;
- }
- cnt -= s[u];
- c[u] = 1;
- return true;
- }
- else {
- char flag = (c[u] | c[v]);
- bool res = !(c[u] && c[v]);
- if (res) {
- if (flag) {
- if (!c[u]) {
- cnt -= s[u];
- }
- else {
- cnt -= s[v];
- }
- }
- if (rank[u] < rank[v]) {
- swap (u, v);
- }
- p[v] = u;
- s[u] += s[v];
- c[u] = flag;
- rank[u] += (rank[u] == rank[v]);
- return true;
- }
- return false;
- }
- }
- inline const bool ask(int u, int v) {
- return !(c[find(u)] && c[find(v)]);
- }
- inline const ll count() const {
- return cnt * (cnt - 1) + 2 * cnt * (n - cnt);
- }
- };
- signed main() {
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #endif
- cout << fixed << setprecision(4);
- ios::sync_with_stdio(false), cin.tie(), cout.tie();
- int n, m, q;
- cin >> n >> m >> q;
- DSU dsu = DSU(n);
- while (m--) {
- int x, y;
- cin >> x >> y;
- //debug(x, y);
- dsu.join(x, y);
- }
- while (q--) {
- short type;
- cin >> type;
- //debug(type);
- if (type == 1) {
- int x, y;
- cin >> x >> y;
- cout << (dsu.ask(x, y) ? "Yes\n" : "No\n");
- }
- else if (type == 2) {
- int x, y;
- cin >> x >> y;
- cout << (dsu.join(x, y) ? "Yes\n" : "No\n");
- }
- else {
- cout << dsu.count() << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement