You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
648 B
C++
27 lines
648 B
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
int fa[1 << 20 | 1];
|
|
|
|
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
|
|
|
|
void link(int x, int y) { fa[find(y)] = find(x); }
|
|
|
|
int main() {
|
|
int m, n, k, ans = 0;
|
|
scanf("%d%d%d", &m, &n, &k);
|
|
for (int i = m * n; i; i--) fa[i] = i;
|
|
for (int a, b; k--;) {
|
|
scanf("%d%d", &a, &b);
|
|
int fa = find(a), fb = find(b);
|
|
link(a, b);
|
|
}
|
|
set<int> S;
|
|
for (int i = m * n; i; i--) S.insert(find(i));
|
|
printf("%d", S.size());
|
|
return 0;
|
|
}
|