text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0, 1, -1, 1, -1, 0};
int dy[] = {0, 0, -1, 1, 1, -1, -1, 1, 0};
void run() {}
int fixMod(long long a, long long b) { return ((a % b) + b) % b; }
int gcd(long long a, long long b) { return (b == 0 ? abs(a) : gcd(b, a % b)); }
void fast() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<vector<int>> adj;
bool vis[int(4e5 + 10)];
void dfs(int node) {
vis[node] = 1;
for (int i = 0; i < adj[node].size(); i++) {
int ch = adj[node][i];
if (!vis[ch]) dfs(ch);
}
}
int main() {
fast();
run();
int n, m, q;
cin >> n >> m >> q;
adj.resize(max(n, m) + n);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--, y--;
y += n;
adj[x].push_back(y);
adj[y].push_back(x);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (!vis[i]) dfs(i), ans++;
}
for (int i = 0; i < m; i++) {
if (!vis[i + n]) dfs(i + n), ans++;
}
ans--;
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int n, m, q, ans;
bool v[maxn];
vector<int> g[maxn];
void dfs(int x) {
if (v[x]) return;
v[x] = true;
for (int y : g[x]) dfs(y);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> q;
int x, y;
while (q--) {
cin >> x >> y;
y += n;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n + m; i++)
if (!v[i]) {
dfs(i);
ans++;
}
cout << ans - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9;
const double eps = 1e-10;
const int MX = 1e6 + 5;
int n, m, q;
vector<vector<int> > adj(MX);
bool vis[MX];
void dfs(int node) {
vis[node] = true;
for (auto& child : adj[node])
if (!vis[child]) dfs(child);
}
int main() {
ios::sync_with_stdio(false);
cout.precision(10);
cin >> n >> m >> q;
int u, v;
for (int i = 0; i < int(q); ++i) {
cin >> u >> v;
--u;
--v;
v += n;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans = 0;
for (int i = 0; i < int(n + m); ++i)
if (!vis[i]) dfs(i), ++ans;
cout << ans - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T, typename TT>
inline ostream &operator<<(ostream &os, const pair<T, TT> &t) {
return os << t.first << " " << t.second;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &t) {
for (auto i : t) os << i << " ";
return os;
}
template <typename T>
inline istream &operator>>(istream &is, vector<T> &v) {
for (T &t : v) is >> t;
return is;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &is, pair<T1, T2> &t) {
is >> t.first >> t.second;
return is;
}
const long long mod = 1e9 + 7;
void solve() {
long long n, m, q;
cin >> n >> m >> q;
vector<vector<long long> > v(n + m);
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
v[a - 1].push_back(n + b - 1);
v[n + b - 1].push_back(a - 1);
}
vector<long long> vis(n + m);
function<void(int)> dfs = [&](int u) {
if (vis[u]) return;
vis[u] = 1;
for (auto x : v[u]) dfs(x);
};
long long ans = 0;
for (int i = 0; i < n + m; i++) {
if (!vis[i]) ans++, dfs(i);
}
cout << ans - 1 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
;
long long T = 1;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int oo = numeric_limits<int>::max();
const int MAXN = 2e5 + 10;
pair<int, int> c[MAXN];
int g[MAXN];
set<int> ys;
int n, m, q;
bool ysort(const pair<int, int> &l, const pair<int, int> &r) {
if (l.second != r.second) return l.second < r.second;
return l.first < r.first;
}
int get(int u) {
if (g[u] == u) return u;
return g[u] = get(g[u]);
}
void unite(int u, int t) {
u = get(u);
t = get(t);
if (u > t) swap(u, t);
g[t] = u;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < m; i++) g[i] = i;
for (int i = 0; i < q; i++) {
cin >> c[i].second >> c[i].first;
c[i].second--, c[i].first--;
ys.insert(c[i].second);
}
sort(c, c + q, ysort);
for (int i = 1; i < q; i++) {
if (c[i].second == c[i - 1].second) unite(c[i].first, c[i - 1].first);
}
set<int> ids;
for (int i = 0; i < m; i++) ids.insert(get(i));
cout << ids.size() - 1 + n - ys.size();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 100;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
const int block = 316;
vector<int> v[maxn];
int par[maxn];
bool mark[maxn];
int get(int u) {
if (par[u] < 0) return u;
return par[u] = get(par[u]);
}
void merge(int u, int w) {
u = get(u), w = get(w);
if (u == w) return;
if (par[u] > par[w]) swap(u, w);
par[u] += par[w];
par[w] = u;
}
bool visited[maxn];
int main() {
ios_base::sync_with_stdio(false);
memset(par, -1, sizeof par);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
v[y].push_back(x);
visited[x] = 1;
}
for (int i = 1; i <= m; i++) {
if (v[i].size() > 1)
for (int j = 0; j < v[i].size() - 1; j++) merge(v[i][j], v[i][j + 1]);
}
vector<int> ans;
for (int i = 1; i <= n; i++) {
int u = get(i);
if (visited[u] and !mark[u]) {
ans.push_back(u);
mark[u] = 1;
}
}
int cnt = ans.size() - 1;
int tot = 0;
for (int i = 1; i <= n; i++)
if (!visited[i]) tot++;
int sum = 0;
for (int i = 1; i <= m; i++)
if (v[i].empty()) sum++;
cout << tot + sum + cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
constexpr inline int SZ(const T &x) {
return x.size();
}
const char el = '\n';
static const int buf_size = 1 << 16;
static char buf[buf_size];
static int len = 0, pos = 0;
inline char getChar() {
if (__builtin_expect(pos == len, 0))
pos = 0, len = fread(buf, 1, buf_size, stdin);
return buf[pos++];
}
template <typename T = int>
inline T read() {
char s = 1, c = read<char>();
T x = 0;
if (c == '-') s = -1, c = getChar();
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
template <>
inline char read<char>() {
char c = getChar();
while (c <= 32) c = getChar();
return c;
}
template <>
inline string read<string>() {
string s;
for (char c = getChar(); c > ' '; c = getChar()) s += c;
return s;
}
struct DSU {
vector<int> e;
DSU(int n) : e(n, -1) {}
bool sameSet(int a, int b) { return find(a) == find(b); }
int size(int x) { return -e[find(x)]; }
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b];
e[b] = a;
return true;
}
};
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(13), cerr << fixed << setprecision(3);
int n = read(), m = read(), q = read();
vector<pair<int, int>> pts(q);
for (auto &[y, x] : pts) {
x = read() - 1, y = read() - 1;
}
sort(begin(pts), end(pts));
DSU d(n);
int ans = m - (q != 0);
for (int i = 1; i < q; ++i)
if (pts[i].first == pts[i - 1].first)
d.join(pts[i].second, pts[i - 1].second);
else
ans--;
bitset<(1 << 18)> bs{};
for (int i = 0; i < n; ++i) bs[d.find(i)] = 1;
ans += bs.count() - 1;
cout << ans << el;
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(4)
const int MAXN = 400000 + 10;
inline int Read() {
char ch;
while ((ch = getchar()) < '0' || '9' < ch)
;
int ans = ch - '0';
while ('0' <= (ch = getchar()) && ch <= '9')
ans = (ans << 3) + (ans << 1) + ch - '0';
return ans;
}
namespace DisjointSet {
int f[MAXN];
inline void Init(int n) {
for (int i = 0; i <= n; ++i) f[i] = i;
}
int Find(int u) { return u == f[u] ? u : f[u] = Find(f[u]); }
inline void Merge(int u, int v) { f[Find(u)] = Find(v); }
inline int Connector(int n) {
int ans = 0;
for (int i = 1; i <= n; ++i)
if (f[i] == i) ans++;
return ans;
}
} // namespace DisjointSet
int main() {
int n = Read();
int m = Read();
int k = Read();
DisjointSet::Init(n + m);
for (int i = 1; i <= k; ++i) {
int u = Read();
int v = Read() + n;
DisjointSet::Merge(u, v);
}
long long ans = DisjointSet::Connector(n + m) - 1;
std::cout << ans << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int M = 8000005;
const int N = 200005;
int n, m, k, sum, ans, Min, f[N], fa[N], t1, t2;
pair<int, int> a[N];
int find(int x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); }
void hunt(int x, int y) {
sort(a + 1, a + 1 + k);
for (int i = 1; i <= x; i++) fa[i] = i;
for (int i = 1; i < k; i++)
if (a[i].first == a[i + 1].first) {
t1 = find(a[i].second);
t2 = find(a[i + 1].second);
if (t1 != t2) fa[t2] = t1;
}
sum = 0;
for (int i = 1; i <= x; i++)
if (find(i) == i) sum++;
ans = sum > 1 ? (sum - 1) : 0;
memset((f), (0), sizeof((f)));
for (int i = 1; i <= k; i++) f[a[i].first] = 1;
for (int i = 1; i <= y; i++)
if (!f[i]) ans++;
Min = min(ans, Min);
}
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= k; i++) scanf("%d %d", &a[i].first, &a[i].second);
Min = INT_MAX;
hunt(m, n);
for (int i = 1; i <= k; i++) swap(a[i].first, a[i].second);
hunt(n, m);
printf("%d\n", Min);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, m, q;
int f[N * 2];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
iota(f + 1, f + n + m + 1, 1);
int cnt = n + m - 1;
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
b += n;
a = find(a);
b = find(b);
if (a != b) {
f[a] = b;
--cnt;
}
}
printf("%d\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long int max(long long int a, long long int b, long long int c) {
return max(max(a, b), c);
}
inline long long int min(long long int a, long long int b, long long int c) {
return min(min(a, b), c);
}
inline long long int max(long long int a, long long int b) {
return (a > b) ? a : b;
}
inline long long int min(long long int a, long long int b) {
return (a < b) ? a : b;
}
inline long long int add(long long int x, long long int y, long long int mod_) {
return (x + y >= mod_) ? x + y - mod_ : x + y;
}
inline long long int mul(long long int x, long long int y, long long int mod_) {
return ((x % mod_) * 1LL * (y % mod_)) % mod_;
}
long long int power(long long int a, long long int n) {
long long int p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long int powm(long long int a, long long int n, long long int mod_) {
long long int p = 1;
while (n) {
if (n % 2) {
p = mul(p, a, mod_);
}
n >>= 1;
a = mul(a, a, mod_);
}
return p % mod_;
}
long long int powi(long long int a, long long int mod_) {
return powm(a, mod_ - 2, mod_);
}
vector<long long int> par, sz;
void make_set(long long int v) {
par[v] = v;
sz[v] = 1;
}
long long int find_set(long long int v) {
if (par[v] == v) return v;
return par[v] = find_set(par[v]);
}
void union_sets(long long int a, long long int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
long long int mn, mx;
long long int n, m, t, k, i, j, sum = 0, flag = 0, cnt = 0;
long long int x = 0, y = 0, z, l, r, q;
int TC = 1;
while (TC--) {
cin >> n >> m >> q;
par.assign(n + m, 0);
sz.assign(n + m, 1);
for (i = 0; i < n + m; i++) make_set(i);
while (q--) {
cin >> x >> y;
--x;
--y;
y += n;
union_sets(x, y);
}
set<long long int> s;
for (i = 0; i < n + m; i++) s.insert(find_set(i));
cout << (long long int)s.size() - 1;
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 400005;
vector<int> e[maxn];
bool vis[maxn];
void dfs(int x) {
vis[x] = 1;
for (int i = 0; i < e[x].size(); i++) {
int u = e[x][i];
if (!vis[u]) dfs(u);
}
}
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(n + y);
e[n + y].push_back(x);
}
int cnt = 0;
for (int i = 1; i <= n + m; i++) {
if (!vis[i]) {
cnt++;
dfs(i);
}
}
printf("%d\n", cnt - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M, Q, ans = -1;
bool vis[400007];
vector<int> adj[400007];
void DFS(int x) {
vis[x] = 1;
for (auto i : adj[x])
if (!vis[i]) DFS(i);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M >> Q;
for (int i = 0, r, c; i < Q; i++) {
cin >> r >> c;
adj[r].push_back(c + N);
adj[c + N].push_back(r);
}
for (int i = 1; i <= N + M; i++)
if (!vis[i]) {
DFS(i);
ans++;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int Inp() {
char c = getchar();
int Neg = 1;
while (c < '0' || c > '9') {
if (c == '-') Neg = -1;
c = getchar();
}
int Sum = 0;
while (c >= '0' && c <= '9') {
Sum = ((Sum << 3) + (Sum << 1)) + c - '0';
c = getchar();
}
return Neg * Sum;
}
int Fa[400010];
int Find(int x) {
if (Fa[x] == x) return x;
return Fa[x] = Find(Fa[x]);
}
int main() {
int n = Inp();
int m = Inp();
int q = Inp();
for (int i = 1; i <= n + m; i++) Fa[i] = i;
for (int i = 1; i <= q; i++) {
int x = Inp();
int y = Inp();
Fa[Find(x)] = Find(n + y);
}
int Ans = 0;
for (int i = 1; i <= n + m; i++) {
if (Find(i) == i) Ans++;
}
printf("%d", Ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
function<void(void)> ____ = []() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
const int MAXN = 4e5 + 7;
int n, m, _, root[MAXN];
int findroot(int x) {
if (x != root[x]) root[x] = findroot(root[x]);
return root[x];
}
int main() {
____();
cin >> n >> m >> _;
for (int i = 1; i <= n + m; i++) root[i] = i;
int res = n + m - 1;
for (int i = 1; i <= _; i++) {
int x, y;
cin >> x >> y;
y += n;
int fx = findroot(x);
int fy = findroot(y);
if (fx == fy) continue;
root[fx] = fy;
res--;
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> e(n + m + 1);
for (int i = 0; i < q; ++i) {
int r, c;
cin >> r >> c;
e[r].push_back(c + n);
e[c + n].push_back(r);
}
vector<bool> vis(n + m + 1);
function<void(int)> dfs = [&](int f) {
vis[f] = true;
for (int i : e[f]) {
if (!vis[i]) {
dfs(i);
}
}
};
int ans = -1;
for (int i = 1; i <= n + m; ++i) {
if (!vis[i]) {
dfs(i);
++ans;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, fa[2000005];
int find(int x) {
if (x == fa[x]) return x;
fa[x] = find(fa[x]);
return fa[x];
}
void Union(int a, int b) {
int x = find(a), y = find(b);
if (x != y) fa[y] = x;
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) fa[i] = i;
for (int i = 1; i <= q; i++) {
int x, y;
scanf("%d%d", &x, &y);
Union(x, y + n);
}
int ans = 0;
for (int i = 1; i <= n + m; i++)
if (fa[i] == i) ans++;
printf("%d\n", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000, M = 200000;
int n, m, s;
vector<int> bel[M + 1], nei[N + 1];
bool vis[N + 1];
void dfs(int x) {
vis[x] = true;
for (int i = 0; i < nei[x].size(); i++) {
int y = nei[x][i];
if (!vis[y]) dfs(y);
}
}
int main() {
cin >> n >> m >> s;
while (s--) {
int x, y;
scanf("%d%d", &x, &y);
bel[y].push_back(x);
}
int ans = -1;
for (int i = 1; i <= m; i++) {
ans += bel[i].empty();
for (int j = 0; j + 1 < bel[i].size(); j++)
nei[bel[i][j]].push_back(bel[i][j + 1]),
nei[bel[i][j + 1]].push_back(bel[i][j]);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) ans++, dfs(i);
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool rows[200009], cols[200009];
bool uniqueParents[200009];
int parent[200009];
int depth[200009];
vector<pair<int, int>> indices;
void makeSets() {
for (int i = 0; i < 200009; i++) {
parent[i] = i;
depth[i] = 1;
}
}
int findSet(int v) {
if (v == parent[v]) return v;
return parent[v] = findSet(parent[v]);
}
void unionSet(int u, int v) {
int a = findSet(u);
int b = findSet(v);
if (a != b) {
if (depth[a] < depth[b]) swap(a, b);
parent[b] = a;
if (depth[a] == depth[b]) depth[a]++;
}
}
int main() {
makeSets();
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
indices.push_back(make_pair(l, r));
rows[l] = true;
cols[r] = true;
}
sort(indices.begin(), indices.end());
for (int i = 1; i < q; i++)
if (indices[i].first == indices[i - 1].first)
unionSet(indices[i].second, indices[i - 1].second);
int emptyRows = 0, emptyCols = 0;
for (int i = 1; i <= n; i++) emptyRows += !rows[i];
for (int i = 1; i <= m; i++) {
if (!cols[i]) {
emptyCols++;
parent[i] = -1;
}
}
int ans = max(emptyRows, emptyCols);
int sets = min(emptyRows, emptyCols);
for (int i = 1; i <= m; i++)
if (parent[i] != -1 && !uniqueParents[findSet(i)]) {
sets++;
uniqueParents[findSet(i)] = true;
}
cout << sets + ans - 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> g(n + m);
for (int i = 0; i < q; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
y += n;
g[x].push_back(y);
g[y].push_back(x);
}
vector<bool> vis(n + m);
function<void(int)> dfs = [&](int v) {
vis[v] = true;
for (auto u : g[v]) {
if (!vis[u]) {
dfs(u);
}
}
};
int ans = -1;
for (int i = 0; i < n + m; ++i) {
if (vis[i]) continue;
dfs(i);
++ans;
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int tt;
int ss = n + m;
int fa[400005];
bool vis[500005];
int ans;
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) {
fa[i] = i;
}
for (int i = 1; i <= q; i++) {
int x, y;
scanf("%d%d", &x, &y);
fa[find(x)] = find(n + y);
}
for (int i = 1; i <= n + m; i++) {
if (!vis[find(i)]) {
vis[fa[i]] = 1;
ans++;
}
}
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
long long powmod(long long a, long long l, long long md) {
long long res = 1;
while (l) {
if (l & 1) res = res * a % md;
l /= 2;
a = a * a % md;
}
return res;
}
long long binpow(long long a, long long l) {
long long res = 1;
while (l) {
if (l & 1) res = res * a;
l /= 2;
a = a * a;
}
return res;
}
long long __set(long long b, long long i) { return b | (1 << i); }
long long __unset(long long b, long long i) { return b & (~(1UL << i)); }
long long __check(long long b, long long i) { return b & (1 << i); }
long long mulmod(long long a, long long b, long long md) {
return ((a % md) * (b % md)) % md;
}
long long addmod(long long a, long long b, long long md) {
return (a + b) % md;
}
long long submod(long long a, long long b, long long md) {
return (((a - b) % md) + md) % md;
}
long long divmod(long long a, long long b, long long md) {
return mulmod(a, powmod(b, md - 2, md), md);
}
const long long inf = 0xFFFFFFFFFFFFFFFL;
priority_queue<long long, vector<long long>, greater<long long> > pq;
clock_t time_p = clock();
void time() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
class UnionFind {
private:
vector<long long> p, rank, setCount;
long long NumSets;
public:
UnionFind(long long N) {
NumSets = N;
rank.assign(N, 0);
setCount.assign(N, 1);
p.assign(N, 0);
for (long long i = 0; i < N; i++) p[i] = i;
}
long long findSet(long long i) {
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(long long i, long long j) { return findSet(i) == findSet(j); }
void unionSet(long long i, long long j) {
if (!isSameSet(i, j)) {
long long x = findSet(i), y = findSet(j);
NumSets--;
if (rank[x] > rank[y]) {
p[y] = x;
setCount[x] += setCount[y];
} else {
p[x] = y;
setCount[y] += setCount[x];
if (rank[x] == rank[y]) rank[y]++;
}
}
}
long long SizeOfSet(long long i) { return setCount[findSet(i)]; }
long long NumDisjointSets() { return NumSets; }
};
signed main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m, q;
cin >> n >> m >> q;
UnionFind uf(n + m);
for (long long i = 0; i < q; i++) {
long long r, c;
cin >> r >> c;
uf.unionSet(r - 1, n + c - 1);
}
cout << uf.NumDisjointSets() - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int f[400100];
int fa(int x) {
int y = x;
while (x != f[x]) x = f[x];
while (y != f[y]) {
int tmp = f[y];
f[y] = x;
y = tmp;
}
return x;
}
int main() {
int n, m, k, ans;
cin >> n >> m >> k;
ans = n + m - 1;
for (int i = 1; i <= n + m; ++i) f[i] = i;
for (int i = 1; i <= k; ++i) {
int x, y;
scanf("%d%d", &x, &y);
y += n;
int fx = fa(x), fy = fa(y);
if (fx != fy) f[fx] = fy, --ans;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, y, z;
int f[400010], ans, q;
int getint() {
int ret = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret;
}
int find(int x) { return f[x] == x ? f[x] : f[x] = find(f[x]); }
int main() {
cin >> n >> m >> q;
for (i = 1; i <= n + m; i++) f[i] = i;
for (i = 1; i <= q; i++) {
x = getint(), y = getint() + n;
f[find(x)] = find(y);
}
for (i = 1; i <= n + m; i++)
if (f[i] == i) ans++;
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int n, m, q, cnt;
int fa[N];
int u, v;
int find(int x) {
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int main() {
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) fa[i] = i;
for (int i = 1; i <= q; i++) {
cin >> u >> v;
int x = find(u), y = find(v + n);
if (x == y) continue;
fa[x] = y;
}
for (int i = 1; i <= n + m; i++)
if (fa[i] == i) cnt++;
cout << cnt - 1;
return 0;
}
|
#include <bits/stdc++.h>
int Fa[200010];
std::vector<int> v[200010];
int Find(int x) { return Fa[x] == x ? x : Fa[x] = Find(Fa[x]); }
bool vis[200010];
inline void merge(int a, int b) {
int f1 = Find(a), f2 = Find(b);
if (f1 != f2) {
Fa[f1] = f2;
}
}
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (; q; --q) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
}
for (int i = 1; i <= m; ++i) Fa[i] = i;
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (!v[i].size())
++ans;
else {
int a = v[i][0];
for (int j = 1, _ = v[i].size(); j < _; ++j) merge(a, v[i][j]);
}
}
for (int i = 1; i <= m; ++i)
if (!vis[Find(i)]) ++ans, vis[Find(i)] = 1;
printf("%d\n", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
constexpr int MAXN = 200005;
int n, m, q;
int uf[MAXN];
int find(int x) { return uf[x] = x == uf[x] ? x : find(uf[x]); }
int merge(int x, int y) {
int xr = find(x);
int yr = find(y);
if (xr == yr) return 0;
uf[xr] = yr;
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf(" %d %d %d", &n, &m, &q);
map<int, vector<int>> by_x;
map<int, vector<int>> by_y;
int x, y;
for (int i = 0; i < q; ++i) {
scanf(" %d %d", &x, &y);
by_x[x].push_back(i);
by_y[y].push_back(i);
uf[i] = i;
}
int blocks = q;
for (auto it : by_x) {
for (int i = 1; i < it.second.size(); ++i) {
blocks -= merge(it.second[i - 1], it.second[i]);
}
}
for (auto it : by_y) {
for (int i = 1; i < it.second.size(); ++i) {
blocks -= merge(it.second[i - 1], it.second[i]);
}
}
int ans = n + m - static_cast<int>(by_x.size()) -
static_cast<int>(by_y.size()) + blocks - 1;
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
long long a[100][100];
using namespace std;
struct DynamicDisjointSetDS {
std::unordered_map<int, int> parent, degree;
int N;
DynamicDisjointSetDS(int n) { N = n; }
int getParent(int vertex) {
if (parent.find(vertex) != parent.end()) {
if (parent[vertex] != vertex) {
parent[vertex] = getParent(parent[vertex]);
return parent[vertex];
}
} else {
parent.insert(make_pair(vertex, vertex));
degree.insert(make_pair(vertex, 1));
}
return vertex;
}
void Union(int vertexA, int vertexB) {
int x = getParent(vertexA);
int y = getParent(vertexB);
if (x == y) return;
if (degree[x] > degree[y]) {
parent[y] = x;
degree[x] = degree[x] + degree[y];
} else {
parent[x] = y;
degree[y] = degree[y] + degree[x];
}
}
int GetTotalComponent() {
unordered_set<int> total;
for (auto itr = parent.begin(); itr != parent.end(); itr++) {
total.insert(getParent(itr->first));
}
return N - parent.size() + total.size();
}
};
int main() {
using namespace std;
long long n, m, q;
cin >> n >> m >> q;
unordered_set<long long> s, s2;
for (long long i = 1; i <= n; i++) {
s.insert(i);
}
for (long long i = 1; i <= m; i++) {
s2.insert(i);
}
DynamicDisjointSetDS ds(m + 1);
vector<vector<long long>> v(m + 1);
while (q--) {
long long r, c;
cin >> r >> c;
v[c].push_back(r);
s.erase(r);
s2.erase(c);
}
for (vector<long long> v2 : v) {
for (long long i = 1; i < v2.size(); i++) {
ds.Union(v2[i], v2[i - 1]);
}
}
unordered_set<long long> sc;
for (long long i = 1; i <= n; i++) {
sc.insert(ds.getParent(i));
}
cout << (s2.size() + sc.size() - 1) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;
int n, m, q, r, c;
bool vis[400001];
vector<int> adj[400001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
cin >> r >> c;
adj[r].push_back(c + n);
adj[c + n].push_back(r);
}
int ans = 0;
for (int i = 1; i <= n + m; i++) {
if (!vis[i]) {
ans++;
queue<int> q;
q.push(i);
vis[i] = true;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int v : adj[cur]) {
if (!vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
}
}
cout << ans - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)4e5 + 10;
int n, m, q, fa[N];
int find(int x) {
if (x != fa[x]) return fa[x] = find(fa[x]);
return x;
}
void unit(int r1, int r2) {
r1 = find(r1), r2 = find(r2);
if (r1 != r2) fa[r2] = r1;
}
int main() {
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) fa[i] = i;
while (q--) {
int i, j;
scanf("%d %d", &i, &j);
unit(i, n + j);
}
int ans = 0;
for (int i = 1; i <= n + m; i++)
if (find(i) == i) ans++;
printf("%d", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5, M = 1e6, P = 1e9 + 7;
int n, m, q, vis[N], ans;
std::vector<int> g[N];
void dfs(int u) {
vis[u] = 1;
for (auto v : g[u]) {
if (!vis[v]) dfs(v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1, u, v; i <= q; i++) {
cin >> u >> v;
g[u].push_back(v + n), g[v + n].push_back(u);
}
for (int i = 1; i <= n + m; i++) {
if (!vis[i]) {
dfs(i);
ans++;
}
}
cout << ans - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int Read() {
int res = 0, f = 1;
char c;
while (c = getchar(), c < 48 || c > 57)
if (c == '-') f = 0;
do res = (res << 3) + (res << 1) + (c ^ 48);
while (c = getchar(), c >= 48 && c <= 57);
return f ? res : -res;
}
template <class T>
inline bool Min(T &a, T const &b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool Max(T &a, T const &b) {
return a < b ? a = b, 1 : 0;
}
const int N = 2e5 + 5;
int q, n, m, x, y, Ans, F[N << 1];
inline int find(int x) { return F[x] == x ? x : F[x] = find(F[x]); }
int main() {
n = Read(), m = Read(), q = Read();
for (register int i = (1), i_end_ = (n + m); i <= i_end_; ++i) F[i] = i;
for (register int i = (1), i_end_ = (q); i <= i_end_; ++i) {
int x = Read(), y = Read();
F[find(y + n)] = find(x);
}
for (register int i = (1), i_end_ = (n + m); i <= i_end_; ++i)
if (find(i) == i) Ans++;
printf("%d", Ans - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int f[N << 1];
int find(int x) {
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) f[i] = i;
int ans = n + m;
while (q--) {
int r, c;
scanf("%d%d", &r, &c);
int x = find(r), y = find(c + n);
if (x != y) {
ans--;
f[x] = y;
}
}
printf("%d\n", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> used;
vector<vector<int> > gr;
void dfs(int v) {
used[v] = 1;
for (int to : gr[v]) {
if (!used[to]) dfs(to);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
used.resize(n + m);
gr.resize(n + m);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
gr[x].push_back(y + n);
gr[y + n].push_back(x);
}
int ans = 0;
for (int i = 0; i < n + m; i++) {
if (!used[i]) {
ans++;
dfs(i);
}
}
cout << ans - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long y = 200005;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(long long &a, long long b) {
a = (a + b) % 1000000007;
if (a < 0) a += 1000000007;
}
long long gcd(long long a, long long b) {
if (a > b) swap(a, b);
if (!a) return b;
return gcd(b % a, a);
}
vector<long long int> ed[400010];
long long cx, cy = 1;
long long n, m, q;
int vis[400005];
void dfs(int i) {
if (vis[i]) return;
vis[i] = 1;
for (int j : ed[i]) dfs(j);
}
void solve() {
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
int u, v;
cin >> u >> v;
ed[u].push_back(y + v);
ed[y + v].push_back(u);
}
long long res = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
dfs(i);
cx++;
}
for (int j = 1; j <= m; j++)
if (!vis[y + j]) cy++;
cout << cx + cy - 2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
t = 1;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int n, m, k, ds[MX], rnk[MX];
struct cell {
int r, c;
bool operator<(const cell &o) const { return c != o.c ? c < o.c : r < o.r; }
} q[MX];
int find(int x) { return x == ds[x] ? x : ds[x] = find(ds[x]); }
bool merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (rnk[x] < rnk[y])
ds[x] = y;
else {
if (rnk[x] == rnk[y]) ++rnk[x];
ds[y] = x;
}
return true;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; ++i) {
scanf("%d %d", &q[i].r, &q[i].c);
}
for (int i = 1; i <= n; ++i) ds[i] = i;
sort(q, q + k);
int took = 0;
for (int i = 0; i < k;) {
int j = i + 1;
int r1 = q[i].r;
++took;
while (j < k && q[i].c == q[j].c) {
int r2 = q[j].r;
assert(r1 != r2);
if (merge(r1, r2)) {
++took;
r1 = r2;
}
++j;
}
i = j;
}
printf("%d\n", n + m - 1 - took);
}
|
#include <bits/stdc++.h>
using namespace std;
int xx[4] = {1, -1, 0, 0};
int yy[4] = {0, 0, 1, -1};
const double eps = 1e-9;
const int maxn = 4e5 + 5000;
const long long mod = 1e9 + 7;
const int INF(0x3f3f3f3f);
const long long INFL(0x3f3f3f3f3f3f3f3fll);
inline int sign(double a) { return a < -eps ? -1 : a > eps; }
long long mul(long long a, long long b, long long c) {
long long res = 1;
while (b) {
if (b & 1) res *= a, res %= c;
a *= a, a %= c, b >>= 1;
}
return res;
}
long long phi(long long x) {
long long res = x;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}
template <typename A, typename B>
inline bool chmin(A& a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B>
inline bool chmax(A& a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B>
inline long long add(A x, B y) {
if (x + y < 0) return x + y + mod;
return x + y >= mod ? x + y - mod : x + y;
}
template <typename A, typename B>
inline void add2(A& x, B y) {
if (x + y < 0)
x = x + y + mod;
else
x = (x + y >= mod ? x + y - mod : x + y);
}
template <typename A, typename B>
inline long long mul1(A x, B y) {
return 1ll * x * y % mod;
}
template <typename A, typename B>
inline void mul2(A& x, B y) {
x = (1ll * x * y % mod + mod) % mod;
}
template <typename A>
inline void debug(A a) {
cout << a << '\n';
}
template <typename A>
inline long long sqr(A x) {
return 1ll * x * x;
}
template <typename A>
A inv(A x) {
return mul(x, mod - 2);
}
template <class T>
void wt(const T& x) {
cout << x << endl;
}
template <class T>
void wt(const T& x, char c) {
cout << x << c;
}
template <class T>
void wt(const T& x, const string& s) {
cout << x << s;
}
template <class T>
void wt(const T& x, int rnd) {
cout << fixed << setprecision(rnd) << x << endl;
}
inline void debug() { cout << "###!!!" << endl; }
inline long long read() {
char c = getchar();
long long x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = 1ll * x * 10 + c - '0', c = getchar();
return x * f;
}
int fa[maxn];
int Find(int x) {
if (x != fa[x]) return fa[x] = Find(fa[x]);
return fa[x];
}
long long n, k, m, q;
vector<int> v[maxn];
int vis[maxn];
void dfs(int x) {
vis[x] = 1;
for (auto d : v[x]) {
if (!vis[d]) dfs(d);
}
return;
}
int main() {
ios::sync_with_stdio(false);
while (cin >> n >> m >> q) {
while (q--) {
int x, y;
cin >> x >> y;
v[x].push_back(y + n);
v[y + n].push_back(x);
}
long long ans = 0;
for (int i = 1; i <= n + m; i++)
if (!vis[i]) dfs(i), ans++;
cout << ans - 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename first, typename second>
ostream &operator<<(ostream &os, const pair<first, second> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
typename vector<T>::const_iterator it;
for (it = v.begin(); it != v.end(); it++) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
os << "[";
typename set<T>::const_iterator it;
for (it = v.begin(); it != v.end(); it++) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename first, typename second>
ostream &operator<<(ostream &os, const map<first, second> &v) {
os << "[";
typename map<first, second>::const_iterator it;
for (it = v.begin(); it != v.end(); it++) {
if (it != v.begin()) os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
struct DSU {
vector<long long> par;
vector<long long> sz;
vector<list<long long>> parts;
DSU(long long n) {
for (long long i = 0; i < n; i++) {
par.push_back(i);
sz.push_back(1);
parts.push_back({i});
}
}
long long find(long long a) {
return par[a] = par[a] == a ? a : find(par[a]);
}
bool same(long long a, long long b) { return find(a) == find(b); }
void unite(long long a, long long b) {
a = find(a);
b = find(b);
if (a == b) return;
if (sz[a] > sz[b]) swap(a, b);
sz[b] += sz[a];
par[a] = b;
parts[b].splice(parts[b].end(), parts[a]);
}
};
long long n, m, q;
int32_t main() {
ios::sync_with_stdio(false);
cin >> n >> m >> q;
DSU d(n + m);
while (q--) {
long long i;
long long j;
cin >> i >> j;
i--;
j--;
j = j + n;
d.unite(i, j);
}
long long co = 0;
vector<bool> vis(n + m, false);
for (long long i = 0; i < n + m; i++) {
long long par = d.find(i);
if (!vis[par]) {
co++;
vis[par] = true;
}
}
cout << co - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, m, q, x, y, ans, f[maxn << 1], vis[maxn << 1];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int merge(int x, int y) {
int fx = find(x), fy = find(y);
if (fx == fy) return 0;
return f[fy] = fx;
}
int main() {
cin >> n >> m >> q;
ans = n + m - 1;
for (int i = 1; i <= n + m; i++) f[i] = i;
for (int i = 1; i <= q; i++) {
cin >> x >> y;
if (merge(x, y + n)) --ans;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
char ch = getchar();
int s;
for (s = 0; !isdigit(ch); ch = getchar()) s |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
if (s) x = -x;
}
const int C0 = 200001;
const int N = 400001;
int pa[N];
int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); }
int solve() {
int n, m, q, ce;
int r, c, rg, cg;
int noc;
read(n);
read(m);
read(q);
ce = C0 + m;
noc = n + m;
for (int i = 1; i <= n; i++) pa[i] = i;
for (int i = 1 + C0; i <= ce; i++) pa[i] = i;
for (int i = 1; i <= q; i++) {
read(r);
rg = find(r);
read(c);
cg = find(c + C0);
if (rg != cg) {
pa[rg] = cg;
noc--;
}
}
return noc - 1;
}
int main() { printf("%d\n", solve()); }
|
#include <bits/stdc++.h>
using namespace std;
const int N = 400400;
int n, m, k;
vector<int> g[N];
bool used[N];
void dfs(int v) {
used[v] = 1;
for (int u : g[v]) {
if (used[u]) continue;
dfs(u);
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
while (k--) {
int v, u;
scanf("%d%d", &v, &u);
v--;
u += n - 1;
g[v].push_back(u);
g[u].push_back(v);
}
int ans = -1;
for (int v = 0; v < n + m; v++) {
if (used[v]) continue;
dfs(v);
ans++;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
int const N = 5e5 + 10, M = 1e6 + 1, oo = 1e9;
const int mod = oo + 7;
const long long OO = 2e18;
int n, m, q, now;
set<int> has[2][N];
set<int> adj[N];
bool vis[N];
void dfs(int u) {
vis[u] = 1;
for (auto v : has[0][u]) has[0][now].insert(v);
for (auto v : adj[u]) {
if (vis[v]) continue;
dfs(v);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
int r, c;
cin >> r >> c;
has[0][r].insert(c);
has[1][c].insert(r);
}
for (int i = 1; i <= n; i++) {
for (auto v : has[0][i]) {
auto y = has[1][v].upper_bound(i);
if (y == has[1][v].end()) continue;
adj[i].insert(*y);
adj[*y].insert(i);
}
}
int an = 0, mn = m, wh = -1;
vector<int> cmp;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
now = i;
dfs(i);
if (m - (int)has[0][i].size() < mn) mn = m - (int)has[0][i].size(), wh = i;
cmp.push_back(i);
}
an = mn;
if (mn == m)
an += n - 1;
else
for (auto i : cmp)
if (has[0][i].empty())
an++;
else if (i != wh)
an -= has[0][i].size() - 1;
cout << an;
return 0;
}
|
#include <bits/stdc++.h>
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template <class T1, class... T2>
inline void qread(T1 &IEE, T2 &...ls) {
register T1 __ = 0, ___ = 1;
register char ch;
while (!isdigit(ch = gc())) ___ = (ch == '-') ? -___ : ___;
do {
__ = (__ << 1) + (__ << 3) + (ch ^ 48);
} while (isdigit(ch = gc()));
__ *= ___;
IEE = __;
qread(ls...);
return;
}
template <class T>
inline void QreadArr(T Begin, T End) {
while (Begin != End) {
qread(*Begin);
++Begin;
}
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
return;
}
inline void putc_(char _x) {
*oS++ = _x;
if (oS == oT) flush();
}
inline void qwrite() {}
template <class T1, class... T2>
inline void qwrite(T1 IEE, T2... ls) {
if (!IEE) putc_('0');
if (IEE < 0) putc_('-'), IEE = -IEE;
while (IEE) _st[++_qr] = IEE % 10 + '0', IEE /= 10;
while (_qr) putc_(_st[_qr--]);
qwrite(ls...);
return;
}
template <class T>
inline void WriteArr(T Begin, T End) {
const char Kg = ' ', Edl = '\n';
while (Begin != End) {
qwrite(*Begin);
++Begin;
putc_(Kg);
}
putc_(Edl);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher;
} // namespace IO
using namespace IO;
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
long long My_Rand(long long Mod) { return (unsigned long long)(rnd()) % Mod; }
template <class T1, class T2>
void Min(const T1 x, const T2 y) {
return x > y ? y : x;
}
template <class T1, class T2>
void Max(const T1 x, const T2 y) {
return x > y ? x : y;
}
template <class T1, class T2>
void To_max(T1 &x, const T2 y) {
x < y ? x = y : x = x;
}
template <class T1, class T2>
void To_min(T1 &x, const T2 y) {
x > y ? x = y : x = x;
}
inline long long qpow(long long n, long long base, long long mod = 1e18) {
long long ret = 1;
while (n) {
if (n & 1) ret = ret * base % mod;
base = base * base % mod;
n >>= 1;
}
return ret % mod;
}
inline long long gcd(long long x, long long y) {
return !y ? x : gcd(y, x % y);
}
inline long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
const int N = 2e5 + 5;
int n, m, s, cnt;
int fa[N << 1];
void init() {
for (int i = 1; i <= n + m; ++i) fa[i] = i;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
qread(n, m, s);
init();
for (int i = 1; i <= s; ++i) {
int x, y;
qread(x, y), y += n;
x = find(x), y = find(y);
if (x != y) fa[x] = y;
}
for (int i = 1; i <= n + m; ++i) cnt += (fa[i] == i);
cout << cnt - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
vector<long long> sz(200005), p(200005);
long long find(long long x) { return x == p[x] ? p[x] : (p[x] = find(p[x])); }
void merge(long long x, long long y) {
x = find(x), y = find(y);
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
p[y] = x;
sz[y] = 0;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m, q;
cin >> n >> m >> q;
for (long long i = 1; i <= max(n, m); i++) {
p[i] = i;
sz[i] = 1;
}
vector<long long> g[m + 1];
for (long long i = 0; i < q; i++) {
long long x, y;
cin >> x >> y;
g[y].push_back(x);
}
long long ans = 0;
for (long long i = 1; i <= m; i++) {
if (g[i].size() == 0) ans++;
for (long long j = 1; j < g[i].size(); j++) {
merge(g[i][j - 1], g[i][j]);
}
}
for (long long i = 1; i <= n; i++) {
ans += p[i] == i;
}
cout << ans - 1 << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
namespace zyt {
template <typename T>
inline bool read(T &x) {
char c;
bool f = false;
x = 0;
do c = getchar();
while (c != EOF && c != '-' && !isdigit(c));
if (c == EOF) return false;
if (c == '-') f = true, c = getchar();
do x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f) x = -x;
return true;
}
template <typename T>
inline void write(T x) {
static char buf[20];
char *pos = buf;
if (x < 0) putchar('-'), x = -x;
do *pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf) putchar(*--pos);
}
const int N = 2e5 + 10;
int fa[N << 1], n, m, q;
int f(const int x) { return x == fa[x] ? x : fa[x] = f(fa[x]); }
int work() {
read(n), read(m), read(q);
for (int i = 1; i <= n + m; i++) fa[i] = i;
int num = 0;
while (q--) {
int a, b;
read(a), read(b);
int x = f(a), y = f(b + n);
if (x != y) {
++num;
fa[x] = y;
}
}
write(n + m - 1 - num);
return 0;
}
} // namespace zyt
int main() { return zyt::work(); }
|
#include <bits/stdc++.h>
using namespace std;
struct {
int p[200010];
void init() {
for (int i = 0; i < 200010; i++) p[i] = i;
}
int _find(int u) {
if (p[u] == u) return u;
return p[u] = _find(p[u]);
}
void _union(int a, int b) {
a = _find(a);
b = _find(b);
if (a == b) return;
p[b] = a;
}
} uf[2];
vector<int> li[200010], co[200010];
int mark_l[200010], mark_c[200010];
int cont_l, cont_c;
int main() {
int N, M, Q;
cin >> N >> M >> Q;
uf[0].init();
uf[1].init();
int i, j;
for (int l = 0; l < Q; l++) {
cin >> i >> j;
co[i].push_back(j);
li[j].push_back(i);
if (!mark_l[i]) cont_l++, mark_l[i] = 1;
if (!mark_c[j]) cont_c++, mark_c[j] = 1;
}
for (int i = 0; i < 200010; i++) {
for (int j = 1; j < li[i].size(); j++) {
uf[0]._union(li[i][j], li[i][j - 1]);
}
for (int j = 1; j < co[i].size(); j++) {
uf[1]._union(co[i][j], co[i][j - 1]);
}
}
int ma_l = 0, ma_c = 0;
for (int i = 1; i <= N; i++) ma_l += uf[0]._find(i) == i;
for (int i = 1; i <= M; i++) ma_c += uf[1]._find(i) == i;
long long ans = min((N - cont_l) + ma_c - 1, (M - cont_c) + ma_l - 1);
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 202020;
int N, M;
bool vis[2 * MAXN];
vector<int> conn[2 * MAXN];
bool dfs(int a) {
if (vis[a]) return false;
vis[a] = true;
for (auto x : conn[a]) dfs(x);
return true;
}
int main() {
int N, M, Q;
scanf("%d%d%d", &N, &M, &Q);
for (int i = 0; i < Q; ++i) {
int u, v;
scanf("%d%d", &u, &v);
u += MAXN;
conn[u].push_back(v);
conn[v].push_back(u);
}
int cnt = 0;
for (int i = 1; i <= N; ++i)
if (dfs(MAXN + i)) ++cnt;
for (int i = 1; i <= M; ++i)
if (dfs(i)) ++cnt;
printf("%d\n", cnt - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0;
char zf = 1;
char ch = getchar();
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * zf;
}
void write(long long y) {
if (y < 0) putchar('-'), y = -y;
if (y > 9) write(y / 10);
putchar(y % 10 + 48);
}
void writeln(const long long y) {
write(y);
putchar('\n');
}
int i, j, k, m, n, x, y, z, cnt;
bool a[1000010], b[1000010];
int c[1000010], d[1000010];
int fa[1000010];
int ask(int x) { return x == fa[x] ? x : fa[x] = ask(fa[x]); }
int main() {
n = read();
m = read();
k = read();
for (register int i = 1; i <= n + m; i++) fa[i] = i;
for (register int i = 1; i <= k; i++) {
x = read();
y = read() + n;
x = ask(x);
y = ask(y);
fa[x] = y;
}
long long ans = -1;
for (register int i = 1; i <= n + m; i++)
if (ask(i) == i) ans++;
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int inf = 1000000007;
const int MOD = 1000000007;
struct UnionFind {
int n;
vector<int> par;
vector<int> group;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++) par[i] = i;
n = N;
}
int root(int x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void calc() {
group.resize(n, 0);
for (int i = 0; i < n; i++) {
group[root(i)]++;
}
}
int size(int x) { return group[root(x)]; }
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry) return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
signed main() {
int n, m, q;
cin >> n >> m >> q;
UnionFind row(n), col(m), tree(n + m);
for (int i = 0; i < q; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
tree.unite(a, b + n);
}
int res = 0;
for (int i = 0; i < n + m; ++i)
if (tree.par[i] == i) ++res;
cout << res - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void dfs(int root, vector<vector<int> > &graph, vector<bool> &vis) {
vis[root] = true;
for (int i = 0; i < graph[root].size(); i++) {
if (!vis[graph[root][i]]) {
dfs(graph[root][i], graph, vis);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int> > graph(n + m + 1);
int r, c;
while (q--) {
cin >> r >> c;
graph[r].push_back(c + n);
graph[c + n].push_back(r);
}
int connectedComp = 0;
vector<bool> vis(n + m + 1, false);
for (int i = 1; i <= n + m; i++) {
if (!vis[i]) {
dfs(i, graph, vis);
connectedComp++;
}
}
cout << connectedComp - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int qs[400005];
vector<int> e[400005];
int vis[400005];
void dfs(int cn) {
vis[cn] = true;
for (auto nn : e[cn])
if (!vis[nn]) dfs(nn);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int _ = 0; _ < (signed)(q); _++) {
int r, c;
cin >> r >> c;
e[r].push_back(n + c);
e[n + c].push_back(r);
}
int cnt = 0;
for (int i = (1); i <= (signed)(n + m); i++) {
if (!vis[i]) {
++cnt;
dfs(i);
}
}
cout << cnt - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = 3.14159265;
const int maxn = 4e5 + 7;
struct UnionFind {
int pre[maxn], siz[maxn];
vector<pair<int, int> > sta;
int n;
void init(int n) {
this->n = n;
sta.clear();
for (int i = 1; i <= n; i++) {
pre[i] = i;
siz[i] = 1;
}
}
int find(int x) {
while (x != pre[x]) x = pre[x];
return x;
}
bool join(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return 0;
if (siz[x] > siz[y]) swap(x, y);
pre[x] = y;
siz[y] += siz[x];
sta.push_back({x, y});
return 1;
}
void undo() {
pair<int, int> tp = sta.back();
sta.pop_back();
int x = tp.first, y = tp.second;
pre[x] = x;
siz[y] -= siz[x];
}
} uf;
int n, m, q;
int u, v;
int main() {
scanf("%d%d%d", &n, &m, &q);
uf.init(n + m + 1);
for (int i = 0; i < q; i++) {
scanf("%d%d", &u, &v);
uf.join(u, v + n);
}
int ans = 0;
for (int i = 1; i <= n + m; i++) {
if (uf.find(i) == i) ans++;
}
printf("%d\n", min(ans - 1, n + m - 1));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e9 + 5;
const int maxn = 2e5 + 2;
int n, m, q;
int rowrep[maxn], colrep[maxn], rowds[maxn], colds[maxn];
bool seenrow[maxn] = {0}, seencol[maxn] = {0};
int find(int x, int ds[]) {
if (ds[x] == x) return x;
return ds[x] = find(ds[x], ds);
}
int count(int a[], int s) {
int seen = 0;
for (int i = 1; i <= s; i++) {
if (find(i, a) == i) ++seen;
}
return seen;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
memset(rowrep, -1, sizeof rowrep);
memset(colrep, -1, sizeof colrep);
for (int i = 1; i < maxn; i++) {
rowds[i] = colds[i] = i;
}
int totcols = 0, totrows = 0;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
if (!seenrow[a]) ++totrows;
seenrow[a] = 1;
if (!seencol[b]) ++totcols;
seencol[b] = 1;
if (rowrep[a] == -1) {
rowrep[a] = find(b, colds);
} else {
int rep = find(b, colds);
colds[rep] = find(rowrep[a], colds);
}
if (colrep[b] == -1) {
colrep[b] = find(a, rowds);
} else {
int rep = find(a, rowds);
rowds[a] = find(colrep[b], rowds);
}
}
int r1 = count(rowds, n);
int r2 = count(colds, m);
int ans = r1 + m - totcols - 1;
ans = min(ans, r2 + n - totrows - 1);
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = 1e17;
vector<vector<long long>> g;
vector<long long> used;
void dfs(long long v) {
used[v] = 1;
for (auto u : g[v]) {
if (!used[u]) {
dfs(u);
}
}
}
void solve() {
long long n, m;
cin >> n >> m;
long long q;
cin >> q;
long long N = n + m;
g.resize(N);
used.resize(N);
while (q--) {
long long x, y;
cin >> x >> y;
--x, --y;
x += m;
g[x].push_back(y);
g[y].push_back(x);
}
long long ans = -1;
for (long long i = 0; i < N; ++i) {
if (!used[i]) {
dfs(i);
ans++;
}
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tt = 1;
while (tt--) {
solve();
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
vector<int> g[2 * N];
int used[2 * N];
void dfs(int v) {
used[v] = 1;
for (int to : g[v]) {
if (!used[to]) dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; (i) < (q); ++i) {
int v, u;
cin >> v >> u;
v--;
u--;
g[v].push_back(N + u);
g[N + u].push_back(v);
}
int cnt = 0;
for (int i = 0; (i) < (n); ++i) {
if (!used[i]) {
dfs(i);
cnt++;
}
}
for (int i = 0; (i) < (m); ++i) {
if (!used[N + i]) {
dfs(N + i);
cnt++;
}
}
cout << cnt - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> vals;
vector<int> size;
int find(int a) {
if (a == vals[a]) {
return a;
}
return (vals[a] = find(vals[a]));
}
void join(int a, int b) {
if ((a = find(a)) != (b = find(b))) {
if (size[a] > size[b]) {
vals[b] = vals[a];
size[a] += size[b];
} else {
vals[a] = vals[b];
size[b] += size[a];
}
}
}
int main() {
int n, m, q;
cin >> n >> m >> q;
vals = vector<int>(n + m);
size = vector<int>(n + m);
for (int i = 0; i < n + m; i++) {
vals[i] = i;
size[i] = 1;
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
join(x - 1, n + y - 1);
}
set<int> connect;
for (int i = 0; i < n + m; i++) {
connect.insert(find(i));
}
cout << connect.size() - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
int chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
int chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
return a * a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
return a > b ? a : b;
}
template <class T>
T aabs(T a) {
return a < 0 ? -a : a;
}
int read() {
int s = 0, base = 1;
char c;
while (!isdigit(c = getchar()))
if (c == '-') base = -base;
while (isdigit(c)) {
s = s * 10 + (c ^ 48);
c = getchar();
}
return s * base;
}
int fa[404847];
int find(int first) {
return first == fa[first] ? first : fa[first] = find(fa[first]);
}
int main() {
int n, m, q, first, second;
n = read();
m = read();
q = read();
for (int i = 1; i <= n + m; ++i) fa[i] = i;
int ans = n + m - 1;
for (; q; --q) {
first = read();
second = read() + n;
if (find(first) != find(second)) {
--ans;
fa[find(first)] = find(second);
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int n_ = 2e5 + 10;
vector<int> adj[n_];
bool visited[n_], have[n_], found_in_row[n_];
vector<int> cur_cc;
void dfs(int u) {
visited[u] = 1;
cur_cc.push_back(u);
for (int v : adj[u]) {
if (!visited[v]) {
dfs(v);
}
}
}
int solve(int n, int m, int q, vector<int> x, vector<int> y) {
for (int i = 0; i < n_; i++)
adj[i] = vector<int>(), visited[i] = 0, have[i] = 0, found_in_row[i] = 0;
vector<vector<int> > rows;
rows.assign(n, vector<int>());
for (int i = 0; i < q; i++) {
rows[x[i]].push_back(y[i]);
if (!x[i]) have[y[i]] = 1;
found_in_row[x[i]] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < (int)rows[i].size() - 1; j++) {
adj[rows[i][j]].push_back(rows[i][j + 1]);
adj[rows[i][1 + j]].push_back(rows[i][j]);
}
}
int res = 0;
for (int i = 0; i < m; i++) {
if (!visited[i]) {
dfs(i);
bool f = 0;
for (int cols : cur_cc) {
f |= have[cols];
}
cur_cc = vector<int>();
if (!f) res++;
}
}
for (int i = 1; i < n; i++) {
if (!found_in_row[i]) {
res++;
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<int> x(q), y(q);
for (int i = 0; i < q; i++) {
cin >> x[i] >> y[i];
x[i]--, y[i]--;
}
cout << min(solve(n, m, q, x, y), solve(m, n, q, y, x)) << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> road[400005];
bool done[400005];
void dfs(int s) {
int size = road[s].size();
for (int i = 0; i < size; ++i) {
int temp = road[s][i];
if (!done[temp]) {
done[temp] = true;
dfs(temp);
}
}
}
int main() {
int n, m, p;
scanf("%d %d %d", &n, &m, &p);
while (p--) {
int a, b;
scanf("%d %d", &a, &b);
road[a].push_back(n + b);
road[n + b].push_back(a);
}
int soo = 0;
for (int i = 1; i <= n + m; ++i) {
if (!done[i]) {
++soo;
done[i] = true;
dfs(i);
}
}
printf("%d", soo - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
long long pw(long long b, long long r, long long md) {
long long ans = 1;
while (r) {
if (r & 1) ans = (ans * b) % md;
b = (b * b) % md;
r >>= 1;
}
return ans;
}
long long par[1000000];
long long fnd(long long u) {
if (u == par[u]) return u;
return par[u] = fnd(par[u]);
}
void uni(long long u, long long v) {
long long p1 = fnd(u);
long long p2 = fnd(v);
par[p1] = p2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) par[i] = i;
for (int i = 0; i < q; i++) {
long long u, v;
cin >> u >> v;
uni(u, n + v);
}
long long ans = -1;
for (int i = 1; i <= n + m; i++)
if (par[i] == i) ans++;
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> G[400000];
bool used[400000];
void dfs(int v) {
used[v] = true;
for (int i = 0; i < (int)G[v].size(); ++i) {
if (!used[G[v][i]]) dfs(G[v][i]);
}
}
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < q; ++i) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b + n);
G[b + n].push_back(a);
}
int ans = 0;
for (int i = 0; i < n + m; ++i) {
if (used[i] == false) {
dfs(i);
ans++;
}
}
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int fa[500500], n, m, q, x, y, ans;
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) fa[i] = i;
ans = n + m - 1;
for (int i = 1; i <= q; i++) {
scanf("%d%d", &x, &y);
if (find(x) != find(n + y)) fa[find(x)] = find(n + y), ans--;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int f[1000000];
inline int find(int x) {
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
inline int read() {
int x = 0, ch = getchar();
while (ch < '0' || ch > '9') {
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
int N, M, Q;
int ans;
int main() {
N = read(), M = read(), Q = read();
for (int i = 1; i <= N + M; ++i) {
f[i] = i;
}
for (int i = 1, x, y; i <= Q; ++i) {
x = read(), y = read() + N;
f[find(x)] = find(y);
}
for (int i = 1; i <= N + M; ++i) {
if (f[i] == i) ++ans;
}
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T> >;
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
int n, m, q;
cin >> n >> m >> q;
if (q == 0) return cout << n + m - 1 << '\n', 0;
struct E {
int i, r, c;
};
V<E> a(q);
for (int i = 0; i < q; i++)
cin >> a[i].r >> a[i].c, a[i].i = i, a[i].r--, a[i].c--;
VV<> g(q);
sort(a.begin(), a.end(),
[](E x, E y) { return x.r < y.r or x.r == y.r and x.c < y.c; });
for (int i = 0; i < q - 1; i++) {
if (a[i].r == a[i + 1].r)
g[a[i].i].push_back(a[i + 1].i), g[a[i + 1].i].push_back(a[i].i);
}
sort(a.begin(), a.end(),
[](E x, E y) { return x.c < y.c or x.c == y.c and x.r < y.r; });
for (int i = 0; i < q - 1; i++) {
if (a[i].c == a[i + 1].c)
g[a[i].i].push_back(a[i + 1].i), g[a[i + 1].i].push_back(a[i].i);
}
int ncc = 0;
V<> v(q);
queue<int> que;
for (int i = 0; i < q; i++) {
if (v[i]) continue;
ncc++;
v[i] = 1;
que.push(i);
while (not que.empty()) {
int j = que.front();
que.pop();
for (auto&& k : g[j]) {
if (v[k]) continue;
v[k] = 1;
que.push(k);
}
}
}
V<> cn(n, 1), cm(m, 1);
for (int i = 0; i < q; i++) cn[a[i].r] = 0, cm[a[i].c] = 0;
int sn = accumulate(cn.begin(), cn.end(), 0),
sm = accumulate(cm.begin(), cm.end(), 0);
cout << ncc - 1 + sn + sm << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int nax = 1e4 + 5;
const int mod = 1e9 + 7;
class dsu {
vector<long long int> parent, rank;
public:
dsu(long long int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
long long int get(long long int n) {
if (parent[n] == n) {
return n;
}
return parent[n] = get(parent[n]);
}
void union_set(long long int a, long long int b) {
a = get(a);
b = get(b);
if (a != b) {
if (rank[a] < rank[b]) {
swap(a, b);
}
parent[b] = a;
if (rank[a] == rank[b]) {
rank[a]++;
}
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, q;
cin >> n >> m >> q;
dsu g(n + m);
for (long long int i = 0; i < q; i++) {
long long int x, y;
cin >> x >> y;
x--;
y--;
g.union_set(x, n + y);
}
bitset<400000> comp;
long long int ans = -1;
for (long long int i = 0; i < n + m; i++) {
if (comp[g.get(i)] == 0) {
comp[g.get(i)] = 1;
ans++;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> e[400000];
bool used[400000];
void dfs(int now) {
if (used[now]) {
used[now] = false;
for (int i = 0; i < e[now].size(); i++) {
dfs(e[now][i]);
}
}
}
int main() {
memset(used, true, sizeof used);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
b += n;
e[a].push_back(b);
e[b].push_back(a);
}
int ans = 0;
for (int i = 0; i < n + m; i++) {
ans += used[i];
dfs(i);
}
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct subset {
int parent;
int rank;
};
int find(vector<subset>& subsets, int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(vector<subset>& subsets, int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
int main() {
int n, m, q, zrow = 0, zcol = 0, nrow = 0, ncol = 0;
cin >> n >> m >> q;
vector<vector<int>> roww(n), colw(m);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
roww[x - 1].push_back(y - 1);
colw[y - 1].push_back(x - 1);
}
vector<subset> srow(n), scol(m);
for (int i = 0; i < n; i++) srow[i].parent = i;
for (int i = 0; i < m; i++) scol[i].parent = i;
for (int i = 0; i < n; i++)
for (int j = 1; j < roww[i].size(); j++)
Union(scol, roww[i][0], roww[i][j]);
for (int i = 0; i < m; i++)
for (int j = 1; j < colw[i].size(); j++)
Union(srow, colw[i][0], colw[i][j]);
for (int i = 0; i < n; i++) {
if (roww[i].size() == 0)
zrow++;
else if (find(srow, i) == i)
nrow++;
}
for (int i = 0; i < m; i++) {
if (colw[i].size() == 0)
zcol++;
else if (find(scol, i) == i)
ncol++;
}
cerr << nrow << ' ' << zcol << ' ' << zrow << endl;
assert(nrow == ncol);
if (nrow == 0)
cout << n + m - 1 << endl;
else
cout << nrow + zcol + zrow - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (T e : v) {
os << e << ' ';
}
return os;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m;
cin >> q;
vector<int> L[n], R[m];
vector<bool> Lv(n), Rv(m);
for (int i = 0; i < q; ++i) {
int r, c;
cin >> r >> c;
L[r - 1].emplace_back(c - 1);
R[c - 1].emplace_back(r - 1);
}
function<bool(int, bool)> dfs = [&](int u, bool left) {
auto& edges = left ? L[u] : R[u];
auto& visited = left ? Lv : Rv;
if (visited[u]) {
return false;
}
visited[u] = true;
for (int v : edges) {
dfs(v, !left);
}
return true;
};
int cx = 0;
for (int i = 0; i < n; ++i) {
if (dfs(i, true)) {
cx++;
}
}
for (int i = 0; i < m; ++i) {
if (dfs(i, false)) {
cx++;
}
}
cout << cx - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 400001;
int n, m, q;
int mark[maxn];
vector<int> g[maxn];
void dfs(int now) {
mark[now] = 1;
int size = g[now].size();
for (int i = 0; i < g[now].size(); i++) {
if (mark[g[now][i]]) continue;
dfs(g[now][i]);
}
}
int main() {
cin >> n >> m >> q;
int r, c;
for (int i = 1; i <= q; i++) {
cin >> r >> c;
c = c + n;
g[r].push_back(c);
g[c].push_back(r);
}
int ans = 0;
for (int i = 1; i <= n + m; i++) {
if (mark[i]) continue;
dfs(i);
ans++;
}
cout << ans - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int getint() {
int num = 0, flag = 1;
char c;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = -1;
while (c >= '0' && c <= '9') num = num * 10 + c - 48, c = getchar();
return num * flag;
}
int n, m, q, ans;
bool vis[400005];
int fir[400005], nxt[400005], to[400005], cnt;
inline void newnode(int u, int v) {
to[++cnt] = v, nxt[cnt] = fir[u];
fir[u] = cnt;
}
inline void dfs(int u) {
vis[u] = 1;
for (int i = fir[u]; i; i = nxt[i])
if (!vis[to[i]]) dfs(to[i]);
}
int main() {
n = getint(), m = getint(), q = getint();
for (int i = 1; i <= q; i++) {
int u = getint(), v = getint() + n;
newnode(u, v), newnode(v, u);
}
for (int i = 1; i <= m + n; i++)
if (!vis[i]) dfs(i), ans++;
printf("%d\n", ans - 1);
}
|
#include <bits/stdc++.h>
int n, m, q;
std::vector<int> adj[400005];
bool visited[400005];
void dfs(int u) {
visited[u] = true;
for (auto v : adj[u]) {
if (!visited[v]) {
dfs(v);
}
}
}
int main() {
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= q; i++) {
int r, c;
scanf("%d %d", &r, &c);
int n_r = r, n_c = n + c;
adj[n_r].push_back(n_c);
adj[n_c].push_back(n_r);
}
int ans = -1;
for (int i = 1; i <= n + m; i++) {
if (!visited[i]) {
dfs(i);
ans++;
}
}
printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int pai[1000010];
int find(int x) {
if (pai[x] == x) return x;
return pai[x] = find(pai[x]);
}
void join(int a, int b) { pai[find(a)] = find(b); }
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) pai[i] = i;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
b += n;
join(a, b);
}
int comps = 0;
for (int i = 1; i <= n + m; i++) comps += pai[i] == i;
cout << comps - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 4e5 + 500;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 500;
vector<long long> ger[maxn];
bool vis[maxn];
void dfs(long long a) {
if (vis[a]) return;
vis[a] = 1;
for (auto v : ger[a]) {
dfs(v);
}
}
int main() {
long long n, m, q;
cin >> n >> m >> q;
for (long long i = 0; i < q; i++) {
long long x, y;
cin >> x >> y;
ger[x].push_back(y + n);
ger[y + n].push_back(x);
}
n = n + m;
long long ans = 0;
for (long long i = 1; i <= n; i++) {
if (vis[i] == 0) {
dfs(i);
ans++;
}
}
cout << ans - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int x[200010], y[200010];
vector<int> row[200010], col[200010];
int parent[200010];
int sz[200100];
char used[200010];
void make_set(int v) {
parent[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (parent[v] == v) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
}
set<int> rows[200010], cols[200010];
int r[200010], c[200010];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> q;
if (q == 0) {
cout << n + m - 1 << endl;
exit(0);
}
for (int i = 1; i <= q; i++) {
cin >> x[i] >> y[i];
row[x[i]].push_back(i);
col[y[i]].push_back(i);
r[x[i]]++;
c[y[i]]++;
make_set(i);
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (int)row[i].size() - 1; j++) {
union_sets(row[i][j], row[i][j + 1]);
}
}
for (int i = 1; i <= m; i++) {
for (int j = 0; j < (int)col[i].size() - 1; j++) {
union_sets(col[i][j], col[i][j + 1]);
}
}
int cnt = 0;
for (int i = 1; i <= q; i++) {
int p = find_set(i);
if (!used[p]) {
cnt++;
used[p] = true;
}
}
for (int i = 1; i <= n; i++)
if (!r[i]) cnt++;
for (int j = 1; j <= m; j++)
if (!c[j]) cnt++;
cout << cnt - 1 << endl;
}
|
#include <bits/stdc++.h>
#pragma optimize("-O3")
using namespace std;
const int N = 400005;
int n, m, q;
vector<int> G[N];
bool vis[N];
inline void dfs(int u) {
vis[u] = 1;
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (!vis[v]) dfs(v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin.tie(nullptr);
cin >> n >> m >> q;
for (int i = 1; i <= q; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v + n), G[v + n].push_back(u);
}
int k = n + m;
int ans = -1;
for (int i = 1; i <= k; ++i) {
if (!vis[i]) {
dfs(i);
++ans;
}
}
return !printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mxn = 200033;
const int mod = 1e9 + 7;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) f = c == '-' ? -1 : 1, c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
long long ksm(long long a, long long b, long long mod) {
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); }
void exgcd(long long a, long long b, long long &x, long long &y, long long &d) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
exgcd(b, a % b, y, x, d);
y -= x * (a / b);
}
}
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m, q;
vector<int> g[mxn << 1];
bool vis[mxn << 1];
void dfs(int x) {
vis[x] = 1;
for (auto it : g[x])
if (!vis[it]) dfs(it);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
;
cin >> n >> m >> q;
int r, c;
while (q--) {
cin >> r >> c;
g[r].push_back((c + n));
g[c + n].push_back((r));
}
int res = 0;
for (int i = 1; i <= n + m; ++i)
if (!vis[i]) {
dfs(i);
res++;
}
cout << res - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f;
const int maxn = 4e5 + 10;
int f[maxn];
int getf(int u) {
if (f[u] == u)
return f[u];
else {
f[u] = getf(f[u]);
return f[u];
}
}
void Merge(int u, int v) {
int t1 = getf(u);
int t2 = getf(v);
if (t1 != t2) f[t1] = t2;
}
int n, m, q, x, y;
int main() {
while (~scanf("%d%d%d", &n, &m, &q)) {
for (int i = 1; i <= n + m; i++) f[i] = i;
while (q--) {
scanf("%d%d", &x, &y);
Merge(x, n + y);
}
int cnt = 0;
for (int i = 1; i <= n + m; i++)
if (f[i] == i) cnt++;
printf("%d\n", cnt - 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 500005;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 100000007;
const double eps = 1e-10;
const double pi = 3.1415926535;
long long qpow(long long a, long long b) {
long long tmp = a % MOD, ans = 1;
while (b) {
if (b & 1) {
ans *= tmp, ans %= MOD;
}
tmp *= tmp, tmp %= MOD, b >>= 1;
}
return ans;
}
long long lowbit(long long x) { return x & -x; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
long long mmax(long long a, long long b, long long c) {
return max(a, max(b, c));
}
long long mmin(long long a, long long b, long long c) {
return min(a, min(b, c));
}
void mod(long long &a) {
a += MOD;
a %= MOD;
}
bool chk(long long now) {}
long long half(long long l, long long r) {
while (l <= r) {
long long m = (l + r) / 2;
if (chk(m))
r = m - 1;
else
l = m + 1;
}
return l;
}
long long ll(long long p) { return p << 1; }
long long rr(long long p) { return p << 1 | 1; }
long long mm(long long l, long long r) { return (l + r) / 2; }
long long lg(long long x) {
if (x == 0) return 1;
return (long long)log2(x) + 1;
}
bool smleql(double a, double b) {
if (a < b || fabs(a - b) <= eps) return true;
return false;
}
bool bigeql(double a, double b) {
if (a > b || fabs(a - b) <= eps) return true;
return false;
}
bool eql(double a, double b) {
if (fabs(a - b) < eps) return 1;
return 0;
}
double len(double a, double b, double c, double d) {
return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}
bool isp(long long x) {
if (x == 1) return false;
if (x == 2) return true;
for (long long i = 2; i * i <= x; ++i)
if (x % i == 0) return false;
return true;
}
inline long long read() {
char ch = getchar();
long long s = 0, w = 1;
while (ch < 48 || ch > 57) {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= 48 && ch <= 57) {
s = (s << 1) + (s << 3) + ch - 48;
ch = getchar();
}
return s * w;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
if (!(a & 1) && !(b & 1))
return gcd(a >> 1, b >> 1) << 1;
else if (!(b & 1))
return gcd(a, b >> 1);
else if (!(a & 1))
return gcd(a >> 1, b);
else
return gcd(abs(a - b), min(a, b));
}
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long n, m, q, x, y;
long long f[maxn];
long long _find(long long x) {
if (f[x] != x) f[x] = _find(f[x]);
return f[x];
}
void _merge(long long x, long long y) {
x = _find(x), y = _find(y);
if (x != y) f[x] = y;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
for (long long i = 1; i <= n + m; ++i) f[i] = i;
for (long long i = 1; i <= q; ++i) {
cin >> x >> y;
_merge(x, y + n);
}
long long ans = 0;
for (long long i = 1; i <= n + m; ++i)
if (f[i] == i) ans++;
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 2;
int n, m, q, ans, p[N], sz[N];
pair<int, int> a[N];
int f_s(int v) { return (p[v] == v) ? v : p[v] = f_s(p[v]); }
void u_s(int a, int b) {
a = f_s(a);
b = f_s(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
int main() {
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) p[i] = i, sz[i] = 1;
for (int i = 1; i <= q; i++) {
cin >> a[i].first >> a[i].second;
a[i].second += n;
u_s(a[i].first, a[i].second);
}
for (int i = 1; i <= n + m; i++) {
if (p[i] == i) ans++;
}
cout << ans - 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long double PI = 2 * acos(0.0);
const vector<long long int> dx = {1, -1, 0, 0};
const vector<long long int> dy = {0, 0, 1, -1};
vector<long long int> ga(long long int n, bool oneIndexed = false) {
vector<long long int> a;
if (oneIndexed) a.push_back(0ll);
for (long long int i = 0; i < n; i++) {
long long int p;
cin >> p;
a.push_back(p);
}
return move(a);
}
vector<string> gas(unsigned long long int n, bool oneIndexed = false) {
vector<string> a;
if (oneIndexed) a.push_back("");
for (unsigned long long int i = 0; i < n; i++) {
string p;
cin >> p;
a.push_back(p);
}
return move(a);
}
template <typename T, typename A>
void pa(vector<T, A> const &a, long long int begin = 0,
long long int end = -1) {
if (end == -1) end = (long long int)a.size() - 1;
for (long long int i = begin; i <= end; i++) {
cout << a[i] << " ";
}
cout << "\n";
}
template <typename T, typename A>
void papair(vector<T, A> const &a, long long int begin = 0,
long long int end = -1) {
if (end == -1) end = (long long int)a.size() - 1;
for (long long int i = begin; i <= end; i++) {
cout << a[i].first << " " << a[i].second << "\n";
}
}
vector<long long int> visitedX((long long int)3e5, 0);
vector<long long int> visitedY((long long int)3e5, 0);
vector<vector<long long int> > adjListX(2e5 + 1);
vector<vector<long long int> > adjListY(2e5 + 1);
long long int edgeCount = 0;
void dfs(long long int curr, long long int prev, bool isX) {
if (isX) {
visitedX[curr] = 1;
} else {
visitedY[curr] = 1;
}
const vector<long long int> &neighs = (isX ? adjListX[curr] : adjListY[curr]);
for (auto neigh : neighs) {
if (neigh == prev) continue;
edgeCount++;
if ((isX ? !visitedY[neigh] : !visitedX[neigh])) {
dfs(neigh, curr, !isX);
}
}
}
void solve() {
long long int n, m, q;
cin >> n >> m >> q;
for (long long int i = 0; i < q; i++) {
long long int x, y;
cin >> x >> y;
adjListX[x].push_back(y);
adjListY[y].push_back(x);
}
long long int c = 0;
for (long long int x = 1; x <= n; x++) {
if (!visitedX[x]) {
dfs(x, -1, true);
c++;
}
}
for (long long int x = 1; x <= m; x++) {
if (!visitedY[x]) {
c++;
}
}
cout << c - 1 << "\n";
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000005;
long long n, m, k, t1, t2, fa[N];
long long find(long long p) { return p == fa[p] ? p : fa[p] = find(fa[p]); }
void merge(long long p, long long q) {
p = find(p);
q = find(q);
if (p - q) fa[p] = q;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
for (long long i = 1; i <= n + m; i++) fa[i] = i;
for (long long i = 1; i <= k; i++) {
cin >> t1 >> t2;
merge(t1, t2 + n);
}
long long ans = 0;
for (long long i = 1; i <= n + m; i++)
if (find(i) == i) ++ans;
cout << ans - 1;
}
|
#include <bits/stdc++.h>
const int N = 500005;
int n, m, q;
int fa[N];
int Seek(int x) { return (fa[x] == x) ? (x) : (fa[x] = Seek(fa[x])); }
void Merge(int x, int y) {
x = Seek(x);
y = Seek(y);
if (x != y) fa[x] = y;
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; ++i) {
fa[i] = i;
}
for (int i = 1, x, y; i <= q; ++i) {
scanf("%d%d", &x, &y);
Merge(x, y + n);
}
int cnt = 0;
for (int i = 1; i <= n + m; ++i) {
if (Seek(i) == i) ++cnt;
}
printf("%d\n", cnt - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int pre[N << 1];
int find(int x) {
if (!pre[x]) return x;
return pre[x] = find(pre[x]);
}
int main() {
ios::sync_with_stdio(false);
int n, m, r, x, y;
cin >> n >> m >> r;
int ans = n + m - 1;
while (r--) {
cin >> x >> y;
x = find(x);
y = find(y + n);
if (x == y) continue;
ans--;
pre[x] = y;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int fa[400010], siz[400010];
inline int hd(int x) { return fa[x] == x ? x : fa[x] = hd(fa[x]); }
int main() {
int n = gi(), m = gi(), q = gi(), r, c;
for (int i = 1; i <= n + m; ++i) fa[i] = i, siz[i] = 1;
while (q--) {
r = hd(gi()), c = hd(gi() + n);
if (r ^ c) {
if (siz[r] > siz[c])
siz[r] += siz[c], fa[c] = r;
else
siz[c] += siz[r], fa[r] = c;
}
}
int ans = -1;
for (int i = 1; i <= n + m; ++i) ans += fa[i] == i;
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> gra(400000 + 2);
vector<int> vis(400000 + 2);
void dfs(int i) {
vis[i] = 1;
for (auto n : gra[i]) {
if (!vis[n]) dfs(n);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
int i = 0;
int r, c;
while (i < q) {
cin >> r >> c;
gra[r].push_back(n + c);
gra[n + c].push_back(r);
i++;
}
int cnt = 0;
for (int i = 1; i <= n + m; ++i) {
if (!vis[i]) {
dfs(i);
cnt++;
}
}
cout << cnt - 1;
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 6;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
int n, m, q;
vector<int> v[maxn];
int vis[maxn];
void dfs(int x) {
if (vis[x]) return;
vis[x] = 1;
for (int y : v[x]) {
dfs(y);
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
y += n;
v[x].push_back(y);
v[y].push_back(x);
}
int ans = 0;
for (int i = 1; i <= n + m; i++) {
if (!vis[i]) {
dfs(i);
ans++;
}
}
printf("%d\n", ans - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, cnt, f[400005];
int fnd(int x) { return f[x] == 0 ? x : f[x] = fnd(f[x]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
cnt = n + m - 1;
for (int i = 1, x, y; i <= q; i++) {
scanf("%d%d", &x, &y);
y += n;
x = fnd(x);
y = fnd(y);
if (x != y) {
cnt--;
f[x] = y;
}
}
printf("%d\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> G[400040];
int c[400040];
void dfs(int u, int C) {
c[u] = C;
for (int i = 0; i < G[u].size(); i++) {
if (c[G[u][i]] == 0) dfs(G[u][i], C);
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i < q; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
G[u].push_back(n + v);
G[n + v].push_back(u);
}
int cnt = 0;
for (int i = 0; i < n + m; i++) {
if (c[i] == 0) {
cnt++;
dfs(i, cnt);
}
}
printf("%d\n", cnt - 1);
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T>
inline long long squ(T x) {
return (long long)x * x;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline T read() {
T cur = 0, flag = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') flag = -1;
for (; isdigit(c); c = getchar()) cur = (cur << 3) + (cur << 1) + (c ^ 0x30);
return flag * cur;
}
const int maxn = 4e5 + 10;
const int N = 100000;
const long long mod = 1e9 + 7;
int n, m, q, res;
int fa[maxn];
inline int getFa(int cur) {
return cur == fa[cur] ? cur : fa[cur] = getFa(fa[cur]);
}
int main() {
scanf("%d%d%d", &n, &m, &q);
res = n + m - 1;
for (register int i = 1; i <= n + m; ++i) fa[i] = i;
int r, c;
for (register int i = 1; i <= q; ++i) {
scanf("%d%d", &r, &c);
int fr = getFa(r), fc = getFa(c + n);
if (fr != fc) {
--res;
fa[fr] = fc;
}
}
printf("%d\n", res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200001], w[200001];
int fi[200001], f[200001], fc[200001];
void dfs(int x, int ok) {
int i, vecin;
if (ok) {
f[x] = 1;
for (i = 0; i < v[x].size(); i++) {
vecin = v[x][i];
if (!fc[vecin]) dfs(vecin, 1 - ok);
}
} else {
fc[x] = 1;
for (i = 0; i < w[x].size(); i++) {
vecin = w[x][i];
if (!f[vecin]) dfs(vecin, 1 - ok);
}
}
}
int main() {
FILE *fin = fopen("a.in", "r");
FILE *fout = fopen("a.out", "w");
int n, m, k, a, b, i;
long long sol = 0;
scanf("%d%d%d", &n, &m, &k);
for (i = 1; i <= k; i++) {
scanf("%d%d", &a, &b);
fi[a] = 1;
v[a].push_back(b);
w[b].push_back(a);
}
sol = -1;
for (i = 1; i <= n; i++) {
if (!f[i]) {
sol++;
dfs(i, 1);
}
}
for (i = 1; i <= m; i++) {
if (!fc[i]) {
sol++;
dfs(i, 1);
}
}
printf("%lld", sol);
return 0;
}
|
#include <bits/stdc++.h>
struct edge {
int to, next;
} e[400005];
int N, M, Q, s, t, head[400005], cnt = 0, ans = 0;
bool v[400005] = {0};
template <class T>
inline void read(T &m) {
m = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) m = (m << 3) + (m << 1) + (c & 15), c = getchar();
}
inline void find(int x) {
for (register int y = head[x]; y; y = e[y].next) {
if (!v[e[y].to]) {
v[e[y].to] = 1;
find(e[y].to);
}
}
}
inline void add(register int x, register int y) {
e[++cnt].next = head[x];
head[x] = cnt;
e[cnt].to = y;
}
int main() {
read(N), read(M), read(Q);
while (Q--) {
read(s), read(t);
add(s, t + N);
add(t + N, s);
}
for (register int i = 1; i <= N + M; ++i) {
if (!v[i]) {
++ans;
v[i] = 1;
find(i);
}
}
printf("%d", ans - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
void getint(int &v) {
char ch, fu = 0;
for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
;
if (ch == '-') fu = 1, ch = getchar();
for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
if (fu) v = -v;
}
int n, m, q, x, y, bl, fa[500010];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int main() {
cin >> n >> m >> q;
bl = n + m - 1;
for (int i = 1; i <= n + m; ++i) fa[i] = i;
while (q--) {
getint(x), getint(y);
int X = getf(y + n), Y = getf(x);
if (X != Y) fa[X] = Y, --bl;
}
cout << bl << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long power(long long x, long long y, long long p = 1e18) {
long long res = 1;
x %= p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
class dsu {
public:
long long n;
vector<long long> p, sz;
dsu() {}
dsu(long long _n) : n(_n) {
p.resize(n);
sz.resize(n, 1);
iota(p.begin(), p.end(), 0);
}
long long inline find(long long x) {
return (x == p[x] ? x : (p[x] = find(p[x])));
}
bool inline unite(long long u, long long v) {
u = find(u), v = find(v);
if (sz[u] < sz[v]) swap(u, v);
if (u != v) {
p[v] = u;
return true;
}
return false;
}
long long inline num_connect() {
vector<long long> cnt(n, 0);
long long res = 0;
for (long long i = 0; i < n; i++) {
long long par = find(i);
cnt[par]++;
}
for (long long i = 0; i < n; i++) {
if (cnt[i] > 0) res++;
}
return res;
}
};
void solve() {
long long n, m, size;
cin >> n >> m >> size;
dsu d(n + m + 2);
long long res = n + m - 1;
for (long long i = 0; i < (long long)(size); ++i) {
long long x, y;
cin >> x >> y;
y += n;
x = d.find(x);
y = d.find(y);
if (x != y) {
res--;
d.unite(x, y);
}
}
cout << res << "\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
long long x = 1;
cout << fixed << setprecision(12);
while (t--) {
solve();
x++;
}
cerr << "Time Taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
long long rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long mod = 1e9 + 7;
const long long N = 400050;
vector<vector<long long> > adj(N);
vector<long long> vis(N);
void dfs(long long node) {
vis[node] = 1;
for (long long child : adj[node]) {
if (!vis[child]) {
dfs(child);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long test = 1;
while (test--) {
long long n, m, q;
cin >> n >> m >> q;
for (long long i = 0; i < q; i++) {
long long x, y;
cin >> x >> y;
adj[x].push_back(n + y);
adj[n + y].push_back(x);
}
long long cnt = 0;
for (long long i = 1; i <= n + m; i++) {
if (!vis[i]) {
cnt++;
dfs(i);
}
}
cout << cnt - 1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> w[400001];
int check[400001];
int ans;
void input() {
scanf("%d %d %d", &n, &m, &q);
int x, y;
for (int i = (0); i < (q); i++) {
scanf("%d %d", &y, &x);
x--;
y--;
x += n;
w[x].push_back(y);
w[y].push_back(x);
}
n = n + m;
}
void dfs(int node) {
check[node] = 1;
for (int i = (0); i < (w[node].size()); i++) {
int next = w[node][i];
if (check[next] == 0) {
dfs(next);
}
}
}
void process() {
for (int i = (0); i < (n); i++) {
if (check[i] == 0) {
dfs(i);
ans++;
}
}
}
void output() { printf("%d", ans - 1); }
int main() {
input();
process();
output();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 9;
const long long INF1 = 1e9 + 9;
const long long MAXN = 1e6 + 7;
const long long MAXN1 = 507;
const long long MAXN2 = 2e7 + 9;
const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 9;
const long long ALPH = 512;
const long long PW1 = 157;
const long long PW2 = 199;
const long long PW3 = 193;
const long long PW4 = 117;
const long double EPS = 1e-9;
const long long BLOCK = 381;
void solve();
signed main() {
srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
bool used[MAXN];
vector<vector<int>> vec(MAXN);
void dfs(int v) {
used[v] = 1;
for (auto I : vec[v]) {
if (!used[I]) {
dfs(I);
}
}
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<int> last(m + 1);
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
if (last[b] != 0) {
vec[a].emplace_back(last[b]);
vec[last[b]].emplace_back(a);
}
last[b] = a;
}
int ans = -1;
for (int i = 1; i <= m; i++) {
if (!last[i]) {
ans++;
}
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
dfs(i);
ans++;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
int x, y;
} a[N];
int n, m, q;
bool cmp(node x, node y) { return x.y == y.y ? x.x < y.x : x.y < y.y; }
int f[N << 1];
bool is[N << 1];
int fa(int x) { return f[x] == x ? x : f[x] = fa(f[x]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= q; ++i) scanf("%d%d", &a[i].x, &a[i].y);
sort(a + 1, a + q + 1, cmp);
for (int i = 1; i <= n + m; ++i) f[i] = i;
for (int i = 1; i <= q; ++i) {
int x = fa(a[i].x), y = fa(a[i].y + n);
f[x] = y;
}
int ans = -1;
for (int i = 1; i <= n + m; ++i) {
int x = fa(i);
if (!is[x]) ++ans, is[x] = 1;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> G[400010];
bool used[400010];
void dfs(int s) {
for (int v : G[s]) {
used[s] = true;
if (!used[v]) dfs(v);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, n, m, q, cnt = 0;
cin >> n >> m >> q;
for (i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
G[x].push_back(n + y);
G[n + y].push_back(x);
}
for (i = 0; i < n + m; i++) used[i] = false;
for (i = 0; i < n + m; i++) {
if (!used[i]) {
cnt++;
dfs(i);
}
}
cout << cnt - 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return f * x;
}
int n, m, q, fa[400003], ans;
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
int main() {
n = read(), m = read(), q = read();
for (int i = 1; i <= m + n; ++i) fa[i] = i;
for (int i = 1; i <= q; ++i) {
int x = read(), y = n + read();
fa[getfa(x)] = getfa(y);
}
int now = getfa(1);
for (int i = 2; i <= n + m; ++i)
if (getfa(i) != now) fa[getfa(i)] = now, ans++;
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 9;
long long par[N], cnt[N], ans, com;
vector<long long> v[N];
long long root(long long x) {
while (par[x] != x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
}
void uni(long long x, long long y) { par[root(x)] = par[root(y)]; }
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n, m, q;
cin >> n >> m >> q;
while (q--) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
}
for (long long i = (long long)(1); i <= (long long)(m); i++) par[i] = i;
for (long long i = (long long)(1); i <= (long long)(n); i++) {
if (v[i].size() == 0) ans++;
if (v[i].size() < 2) continue;
for (long long j = (long long)(1); j <= (long long)(v[i].size() - 1); j++) {
if (root(v[i][j]) != root(v[i][j - 1])) uni(v[i][j], v[i][j - 1]);
}
}
for (long long i = (long long)(1); i <= (long long)(m); i++) cnt[root(i)]++;
for (long long i = (long long)(1); i <= (long long)(m); i++)
if (cnt[i]) com++;
ans += com - 1;
cout << ans;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.