text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int f[410000];
int vis[410000];
int sum = 0;
int finds(int x) { return x == f[x] ? x : f[x] = finds(f[x]); }
void unions(int x, int y) {
x = finds(x);
y = finds(y);
if (x != y) f[x] = y;
return;
}
int main() {
int n, m, q;
int x, y;
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) {
f[i] = i;
}
while (q--) {
cin >> x >> y;
unions(x, y + n);
}
for (int i = 1; i <= n + m; i++) {
vis[finds(i)]++;
if (vis[f[i]] == 1) sum++;
}
cout << sum - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<int> adj[400005];
bool vis[400005];
void dfs(int i) {
vis[i] = 1;
for (auto v : adj[i]) {
if (!vis[v]) dfs(v);
}
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
while (q--) {
int r, c;
cin >> r >> c;
r--;
c += (200000 - 1);
adj[r].push_back(c);
adj[c].push_back(r);
}
int c = 0;
for (int i = 0; i < n; i++)
if (!vis[i]) c++, dfs(i);
for (int i = 0; i < m; i++)
if (!vis[i + 200000]) c++, dfs(i);
cout << c - 1;
}
int main() {
fastio();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int oo = numeric_limits<int>::max();
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << '(' << p.first << ", " << p.second << ')';
}
template <typename I>
ostream &print(ostream &o, I s, I e, int w = 5, int prec = 2,
const string &sep = ", ", const string &lhs = "",
const string &rhs = "") {
o << lhs;
if (s != e) o << setw(w) << setprecision(prec) << *(s++);
for (; s != e; ++s) o << sep << setw(w) << setprecision(prec) << *s;
return o << rhs;
}
template <typename T,
template <typename E, typename A = std::allocator<E>> class C>
ostream &operator<<(ostream &o, const C<T> &c) {
return print(o, c.begin(), c.end(), 0, 2, ", ", "[", "]");
}
template <typename T, template <typename E, typename Compare = less<E>,
typename Alloc = allocator<E>>
class C>
ostream &operator<<(ostream &o, const C<T> &c) {
return print(o, c.begin(), c.end(), 0, 2, ", ", "{", "}");
}
template <typename K, typename T,
template <typename E1, typename E2, typename Compare = std::less<E1>,
class Allocator = std::allocator<std::pair<const E1, E2>>>
class C>
ostream &operator<<(ostream &o, const C<K, T> &c) {
return print(o, c.begin(), c.end(), 0, 2, ", ", "{", "}");
}
template <typename T>
void uniq(vector<T> *v) {
sort(v->begin(), v->end());
v->erase(unique(v->begin(), v->end()), v->end());
}
struct Point {
int x, y;
int cluster;
};
void mark_clusters(const int n, const int m, vector<Point> *points) {
vector<vector<Point *>> x_to_pts(n);
vector<vector<Point *>> y_to_pts(m);
for (auto &pt : *points) {
x_to_pts[pt.x].push_back(&pt);
y_to_pts[pt.y].push_back(&pt);
}
for (auto &pt : *points) {
pt.cluster = -1;
}
int next_cluster = 0;
for (auto &pt : *points) {
if (pt.cluster != -1) continue;
vector<Point *> stack;
stack.push_back(&pt);
pt.cluster = next_cluster++;
while (!stack.empty()) {
Point *stack_pt = stack.back();
stack.pop_back();
for (vector<Point *> *child_pts :
{&x_to_pts[stack_pt->x], &y_to_pts[stack_pt->y]}) {
for (auto &child_pt : *child_pts) {
if (child_pt->cluster != -1) continue;
child_pt->cluster = pt.cluster;
stack.push_back(child_pt);
}
child_pts->clear();
}
}
}
}
struct Cluster {
vector<int> xs, ys;
};
void create_clusters(const vector<Point> &points, vector<Cluster> *clusters) {
int max_cluster = -1;
for (auto &pt : points) {
max_cluster = max(max_cluster, pt.cluster);
}
clusters->resize(max_cluster + 1);
for (auto &pt : points) {
(*clusters)[pt.cluster].xs.push_back(pt.x);
(*clusters)[pt.cluster].ys.push_back(pt.y);
}
for (auto &cluster : *clusters) {
for (auto *coords : {&cluster.xs, &cluster.ys}) {
uniq(coords);
}
}
}
int find_cost(const int n, const int m, const vector<Cluster> &clusters) {
int cost = 0;
vector<bool> h(n, false), v(m, false);
bool corner = false;
for (const auto &cluster : clusters) {
const int h_diff = cluster.xs.size();
const int v_diff = cluster.ys.size();
const int mxs = min(cluster.xs.back(), cluster.xs.front());
const int mys = min(cluster.ys.back(), cluster.ys.front());
corner |= mxs == 0 && mys == 0;
if (mxs != 0 && mys != 0) {
cost += 1;
}
for (const int x : cluster.xs) h[x] = true;
for (const int y : cluster.ys) v[y] = true;
}
vector<pair<int, int>> left;
for (int i = 0; i < n; i += 1)
if (h[i]) left.emplace_back(i, 0);
for (int i = 0; i < m; i += 1)
if (v[i]) left.emplace_back(0, i);
uniq(&left);
if (!left.empty() && left.front() == pair<int, int>{0, 0} && !corner)
left.pop_back();
return cost + n + m - 1 - left.size();
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
long long n, m, q;
cin >> n >> m >> q;
vector<Point> points(q);
for (auto &pt : points) {
cin >> pt.x >> pt.y;
pt.x -= 1;
pt.y -= 1;
}
mark_clusters(n, m, &points);
vector<Cluster> clusters;
create_clusters(points, &clusters);
vector<Cluster> clusters_rotated;
int cost = +oo;
clusters_rotated = clusters;
cost = min(cost, find_cost(n, m, clusters_rotated));
clusters_rotated = clusters;
for (auto &cluster : clusters_rotated) {
for (int &x : cluster.xs) x = n - x - 1;
}
cost = min(cost, find_cost(n, m, clusters_rotated));
clusters_rotated = clusters;
for (auto &cluster : clusters_rotated) {
for (int &y : cluster.ys) y = m - y - 1;
}
cost = min(cost, find_cost(n, m, clusters_rotated));
clusters_rotated = clusters;
for (auto &cluster : clusters_rotated) {
for (int &x : cluster.xs) x = n - x - 1;
for (int &y : cluster.ys) y = m - y - 1;
}
cost = min(cost, find_cost(n, m, clusters_rotated));
cout << cost << '\n';
return 0;
}
|
#include <bits/stdc++.h>
void re(int& x);
template <class T, class... Ts>
void re(T& t, Ts&... ts);
void pr(int x);
void pr(const char* x);
void ps();
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts);
using namespace std;
int n, m, q;
int par[410000];
int f(int* p, int a) {
if (p[a] == a) return a;
return p[a] = f(p, p[a]);
}
void l(int* p, int a, int b) {
int x = f(p, a), y = f(p, b);
p[x] = y;
}
void solve() {
re(n, m, q);
for (int(i) = 0; (i) < (int)(n + m); (i)++) par[i] = i;
for (int(i) = 0; (i) < (int)(q); (i)++) {
int a, b;
re(a, b);
a--;
b--;
l(par, a, n + b);
}
int ans = 0;
for (int i = 0; i < n + m; i++) ans += (f(par, i) == i);
ps(ans - 1);
}
int main() { solve(); }
void re(int& x) { scanf("%d", &x); }
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
void pr(int x) { printf("%d", x); }
void pr(const char* x) { printf("%s", x); }
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
struct ett {
int x, y;
};
ett e[N];
bool cmp1(ett a, ett b) { return a.x < b.x || a.x == b.x && a.y < b.y; }
bool cmp2(ett a, ett b) { return a.y < b.y || a.y == b.y && a.x < b.x; }
int fa[N], b[N];
int findfa(int x) {
if (fa[x] == x) return x;
fa[x] = findfa(fa[x]);
return fa[x];
}
pair<int, int> cal1(int q, int m) {
int i, j, k;
sort(e + 1, e + q + 1, cmp1);
e[0].x = e[1].x - 1;
for (i = 1; i <= m; i++) fa[i] = i, b[i] = 0;
for (i = 1; i <= q; i++) {
if (e[i].x == e[i - 1].x) continue;
int tt = e[i].y, ft = findfa(tt);
b[tt] = 1;
for (j = i; j <= q; j++)
if (e[j].x > e[i].x) break;
for (k = i + 1; k < j; k++) {
int t = e[k].y;
b[t] = 1;
fa[findfa(t)] = ft;
}
}
int tot1 = 0, tot2 = 0;
for (i = 1; i <= m; i++) {
if (!b[i]) {
tot2++;
continue;
}
if (findfa(i) == i) tot1++;
}
return make_pair(tot1, tot2);
}
pair<int, int> cal2(int q, int m) {
int i, j, k;
sort(e + 1, e + q + 1, cmp2);
e[0].y = e[1].y - 1;
for (i = 1; i <= m; i++) fa[i] = i, b[i] = 0;
for (i = 1; i <= q; i++) {
if (e[i].y == e[i - 1].y) continue;
int tt = e[i].x, ft = findfa(tt);
b[tt] = 1;
for (j = i; j <= q; j++)
if (e[j].y > e[i].y) break;
for (k = i + 1; k < j; k++) {
int t = e[k].x;
b[t] = 1;
fa[findfa(t)] = ft;
}
}
int tot1 = 0, tot2 = 0;
for (i = 1; i <= m; i++) {
if (!b[i]) {
tot2++;
continue;
}
if (findfa(i) == i) tot1++;
}
return make_pair(tot1, tot2);
}
int main() {
int i, n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (i = 1; i <= q; i++) scanf("%d%d", &e[i].x, &e[i].y);
pair<int, int> p1 = cal1(q, m), p2 = cal2(q, n);
int ans1 = p1.first - 1;
int ans2 = p1.second + p2.second;
cout << ans1 + ans2 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
class DSU {
public:
int p[1000000], s[1000000];
int CC;
void init(int n) {
CC = n;
for (int i = 0; i < n; i++) p[i] = i, s[i] = 1;
}
int root(int x) {
if (p[x] == x) return x;
return root(p[x]);
}
void merge(int a, int b) {
a = root(a);
b = root(b);
if (a == b) return;
CC--;
if (s[a] < s[b]) {
s[b] += s[a];
p[a] = b;
} else {
s[a] += s[b];
p[b] = a;
}
}
};
int main() {
long long int n, m;
int q;
cin >> n >> m >> q;
DSU *D = new DSU();
D->init(n + m);
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
D->merge(a, b + n);
}
cout << D->CC - 1 << endl;
return 0;
}
|
#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 x = 1, c = 0;
while (x <= n) {
if (!visitedX[x]) {
dfs(x, -1, true);
c++;
}
x++;
}
long long int y = 1;
while (y <= m) {
if (!visitedY[y]) c++;
y++;
}
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 int MAXN = 4e5 + 7;
int N, M, Q;
int p[MAXN];
int Find(int x) { return p[x] = p[x] == x ? x : Find(p[x]); }
int main() {
scanf("%d%d%d", &N, &M, &Q);
for (int i = (1); i <= (N + M); i++) p[i] = i;
for (int i = (1); i <= (Q); i++) {
int x, y;
scanf("%d%d", &x, &y);
y += N;
int fx = Find(x), fy = Find(y);
if (fx ^ fy) {
p[fx] = fy;
}
}
int cnt = 0;
for (int i = (1); i <= (N + M); i++) cnt += (Find(i) == i);
printf("%d\n", cnt - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int m, n, q, num = -1;
int r, c;
queue<int> Q;
int main() {
cin >> n >> m >> q;
vector<vector<int>> v(n + m);
vector<int> a(n + m, -1);
for (int i = 0; i < q; i++) {
cin >> r >> c;
v[r - 1].push_back(n + c - 1);
v[n + c - 1].push_back(r - 1);
}
for (int i = 0; i < v.size(); i++) {
if (a[i] == -1) {
num++;
a[i] = num;
Q.push(i);
while (!Q.empty()) {
r = Q.front();
Q.pop();
for (int j = 0; j < v[r].size(); j++) {
if (a[v[r][j]] == -1) {
a[v[r][j]] = num;
Q.push(v[r][j]);
}
}
}
}
}
cout << num;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
inline long long readl() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
template <class T>
inline void print(T x) {
if (x / 10 != 0) print(x / 10);
putchar(x % 10 + '0');
}
template <class T>
inline void writeln(T x) {
if (x < 0) putchar('-');
x = abs(x);
print(x);
putchar('\n');
}
template <class T>
inline void write(T x) {
if (x < 0) putchar('-');
x = abs(x);
print(x);
putchar(' ');
}
template <class T>
inline T power(T a, T b) {
T ans = 1;
while (b) {
if (b & 1) {
ans = ans * a % 1000000007;
}
b >>= 1;
a = a * a % 1000000007;
}
return ans % 1000000007;
}
int n, m, q, f[400005], ans;
inline int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) f[i] = i;
for (int i = 1; i <= q; i++) {
int r, c;
scanf("%d%d", &r, &c);
c += n;
int fx = find(r), fy = find(c);
f[fy] = fx;
}
for (int i = 1; i <= n + m; i++) ans += (f[i] == i);
printf("%d\n", ans - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
void setIO(const string &name = "") {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
if (name.length()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
template <typename T>
void read(vector<T> &a) {
for (auto &x : a) cin >> x;
}
template <typename T>
void read(vector<T> &a, long long n) {
a.resize(n);
for (auto &x : a) cin >> x;
}
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &v) {
out << "(";
out << v.first << "," << v.second;
return out << ")";
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << "[";
for (auto(i) = 0; (i) < ((long long)(v).size()); (i)++) {
if (i) out << ", ";
out << v[i];
}
return out << "]";
}
template <typename T>
void print(vector<T> &a) {
for (const auto &x : a) cout << x << ' ';
cout << '\n';
}
void MOD(long long &x, long long m = 1000000007) {
x %= m;
if (x < 0) x += m;
}
template <typename T>
void dbg(const char *name, T &&arg1) {
cout << name << " : " << arg1 << '\n';
}
template <typename T, typename... U>
void dbg(const char *names, T &&arg1, U &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
dbg(comma + 1, args...);
}
template <class T>
void read(T &x) {
cin >> x;
}
template <class T, class... U>
void read(T &t, U &...u) {
read(t);
read(u...);
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
struct dsu {
vector<long long> link;
vector<long long> size;
long long num_comp = 1;
void init(long long n) {
link.assign(n, 0LL);
size.assign(n, 1);
num_comp = n;
for (auto(i) = 0; (i) < (n); (i)++) link[i] = i;
}
long long find(long long x) {
while (x != link[x]) x = link[x];
return x;
}
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 (size[a] < size[b]) swap(a, b);
size[a] += size[b];
link[b] = a;
num_comp--;
}
};
long long n, m, q;
dsu graph;
int32_t main() {
setIO();
read(n, m, q);
graph.init(n + m);
for (auto(_) = 0; (_) < (q); (_)++) {
long long a, b;
read(a, b);
--a, --b;
b += n;
graph.unite(a, b);
}
cout << graph.num_comp - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
int n, m, q;
int r[N], c[N], p[N];
int ans;
int find(int i) {
if (i == 0) return 0;
if (i == p[i]) return i;
return p[i] = find(p[i]);
};
int main() {
cin.sync_with_stdio(0);
cin >> n >> m >> q;
if (q) {
for (int i = 1; i <= q; ++i) {
int x, y;
cin >> x >> y;
int u = find(r[x]);
int v = find(c[y]);
if (u && v && u == v)
;
else if (u && v)
p[u] = v, ans--;
else if (u)
c[y] = u;
else if (v)
r[x] = v;
else
r[x] = c[y] = p[i] = i, ans += (i > 1);
}
ans += count(r + 1, r + n + 1, 0);
ans += count(c + 1, c + m + 1, 0);
} else {
ans = n + m - 1;
}
cout << ans << endl;
};
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[400000];
bool used[400000];
int n, m, k;
void dfs(int x) {
used[x] = 1;
for (int i = 0; i < v[x].size(); i++)
if (!used[v[x][i]]) dfs(v[x][i]);
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
x--;
y += n - 1;
v[x].push_back(y);
v[y].push_back(x);
}
int ans = -1;
for (int i = 0; i < n + m; i++) {
if (!used[i]) {
ans++;
dfs(i);
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, x, y, ans;
int fa[400005];
int getf(int u) { return fa[u] == 0 ? u : fa[u] = getf(fa[u]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
ans = n + m - 1;
while (q--) {
scanf("%d%d", &x, &y);
y += n;
x = getf(x);
y = getf(y);
if (x != y) {
ans--;
fa[x] = y;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
vector<pii> elements;
vector<int> adj[400001];
bool visited[400001];
void dfs(int u) {
visited[u] = true;
for (int v : adj[u]) {
if (not visited[v]) {
dfs(v);
}
}
}
long long answer(int n, int m) {
int components = 0;
for (int i = 1; i <= n + m; ++i) {
if (not visited[i]) {
dfs(i);
components++;
}
}
return components - 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
int r, c;
for (int i = 0; i < q; ++i) {
cin >> r >> c;
c += n;
adj[r].push_back(c);
adj[c].push_back(r);
}
cout << answer(n, m);
return 0;
}
|
#include <bits/stdc++.h>
int n, m, q, i, x, y, r, p[400400], rk[400400];
void un(int a, int b) {
if (a == b)
return;
else
r--;
if (rk[a] > rk[b])
p[b] = a;
else {
p[a] = b;
if (rk[a] == rk[b]) rk[b]++;
}
}
int fs(int x) {
if (x != p[x]) p[x] = fs(p[x]);
return p[x];
}
int main() {
scanf("%d%d%d", &n, &m, &q);
r = n + m;
for (i = 1; i <= r; i++) p[i] = i;
while (q--) {
scanf("%d%d", &x, &y);
un(fs(x), fs(n + y));
}
printf("%d\n", r - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int find(int x) {
if (p[x] == x) {
return x;
}
return p[x] = find(p[x]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
p = vector<int>(n + m);
for (int i = 0; i < n + m; i++) {
p[i] = i;
}
auto join = [=](int a, int b) {
a = find(a);
b = find(b);
if (a == b) {
return false;
}
if (rand() & 1) {
swap(a, b);
}
p[a] = b;
return true;
};
int ans = n + m;
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--, y--;
y += n;
if (join(x, y)) {
ans--;
}
}
ans--;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {}
const long long maxn = 200500;
long long disj[2 * maxn];
bool par[2 * maxn];
long long findp(long long v) {
if (disj[v] == v)
return v;
else
return disj[v] = findp(disj[v]);
}
void merge(long long a, long long b) {
long long c = findp(a);
long long d = findp(b);
disj[c] = d;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
for (long long i = 0; i < 2 * maxn; i++) disj[i] = i;
long long n, m, q;
cin >> n >> m >> q;
while (q--) {
long long r, c;
cin >> r >> c;
c += n;
merge(findp(r), findp(c));
}
for (long long i = 1; i <= n + m; i++) {
par[findp(i)] = true;
}
long long cum = 0;
for (long long i = 1; i <= n + m; i++) {
cum += par[i];
}
cout << cum - 1 << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 202020;
int n, m, q;
int pre[N];
bool vis[N];
pair<int, int> a[N];
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void join(int x, int y) {
x = find(x);
y = find(y);
pre[x] = y;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
cin >> n >> m >> q;
for (int i = (1); i < (q + 1); ++i)
cin >> a[i].first >> a[i].second, vis[a[i].first] = 1;
sort(a + 1, a + 1 + q);
for (int i = (1); i < (m + 1); ++i) pre[i] = i;
for (int i = (2); i < (q + 1); ++i)
if (a[i].first == a[i - 1].first) join(a[i].second, a[i - 1].second);
int ans = -1;
for (int i = (1); i < (m + 1); ++i) ans += (i == find(i));
for (int i = (1); i < (n + 1); ++i) ans += (!vis[i]);
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, r, c, ans = -1;
set<int> st;
bool ch[400012];
vector<int> ad[400012];
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;
r--;
c--;
c += n;
ad[r].push_back(c);
ad[c].push_back(r);
}
for (int j = 0; j < n + m; j++) {
if (ch[j] == 1) continue;
ans++;
st.insert(j);
while (st.size()) {
int now = *st.begin();
st.erase(st.begin());
ch[now] = 1;
for (int i = 0; i < ad[now].size(); i++) {
if (ch[ad[now][i]] == 1) continue;
st.insert(ad[now][i]);
}
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int parent[200005];
int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void join(int x, int y) {
int p = find(x);
int q = find(y);
if (p == q) return;
parent[p] = q;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
vector<int> X[n + 1], Y[m + 1];
set<int> idx, gp;
for (int i = (int)(1); i <= (int)(n); i++) parent[i] = i;
for (int i = (int)(1); i <= (int)(q); i++) {
int x, y;
cin >> x >> y;
X[x].push_back(y);
Y[y].push_back(x);
idx.insert(y);
}
for (int i = (int)(1); i <= (int)(m); i++)
for (int j = (int)(1); j <= (int)(Y[i].size() - 1); j++)
join(Y[i][j - 1], Y[i][j]);
for (int i = (int)(1); i <= (int)(n); i++) gp.insert(find(i));
cout << gp.size() - 1 + (m - idx.size());
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> vx, vy;
vector<bool> usedx, usedy;
int cnt = 0, ans = 0;
void dfs(int u) {
usedx[u] = 1;
for (int i = 0; i < vx[u].size(); i++) {
int to = vx[u][i];
if (!usedy[to]) {
usedy[to] = 1;
for (int j = 0; j < vy[to].size(); j++) {
if (!usedx[vy[to][j]]) {
dfs(vy[to][j]);
}
}
}
}
}
int main() {
int n, m, q;
cin >> n >> m >> q;
vx.resize(n);
vy.resize(m);
usedx.resize(n);
usedy.resize(m);
if (q == 0) {
cout << n + m - 1 << endl;
return 0;
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
vx[x - 1].push_back(y - 1);
vy[y - 1].push_back(x - 1);
}
for (int i = 0; i < n; i++) {
if (!usedx[i]) {
if (vx[i].size() == 0) {
usedx[i] = 1;
ans++;
} else {
cnt++;
dfs(i);
}
}
}
for (int i = 0; i < m; i++) {
if (!usedy[i]) {
ans++;
}
}
ans += cnt - 1;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
inline long long power(long long a, long long b) {
long long x = 1;
a = a % 1000000007ULL;
while (b) {
if (b & 1) x = (x * a) % 1000000007ULL;
a = (a * a) % 1000000007ULL;
b >>= 1;
}
return x;
}
inline long long inv(long long a) { return power(a, 1000000007ULL - 2); }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long n, m, q;
map<pair<int, int>, bool> check;
int p[400005], rnk[400005];
set<int> ss;
int find(int v) { return v == p[v] ? v : p[v] = find(p[v]); }
void merge(int u, int v) { p[find(u)] = find(v); }
int main() {
cin >> n >> m >> q;
int x, y;
int ans = 0;
for (int i = 1; i <= 400000; i++) {
p[i] = i;
rnk[i] = 0;
}
for (int i = 0; i < q; i++) {
cin >> x >> y;
merge(x, y + 200000);
}
for (int i = 1; i <= n; i++) ss.insert(find(i));
for (int i = 200001; i <= 200000 + m; i++) ss.insert(find(i));
ans = ss.size() - 1;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 5e5 + 5;
int par[N];
int n, m, q;
int getpar(int u) {
if (par[par[u]] == par[u]) return par[u];
return par[u] = getpar(par[u]);
}
void unite(int a, int b) {
a = getpar(a);
b = getpar(b);
par[a] = b;
}
int main() {
iota(par, par + N, 0);
cin >> n >> m >> q;
for (int i = 0; i < q; ++i) {
int a, b;
scanf("%d %d", &a, &b);
unite(a, b + n);
}
int ans = -1;
for (int i = 1; i <= n + m; ++i)
if (getpar(i) == i) ++ans;
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1LL << 60;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
struct UnionFind {
vector<long long> par, w;
UnionFind(long long n) : par(n, -1), w(n, 0) {}
void init(long long n) {
par.assign(n, -1);
w.assign(n, 0);
}
long long root(long long x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(long long x, long long y) { return root(x) == root(y); }
bool merge(long long x, long long y) {
x = root(x);
y = root(y);
if (x == y) {
++w[x];
return false;
}
if (par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
w[x] += w[y];
++w[x];
return true;
}
long long size(long long x) { return -par[root(x)]; }
long long wei(long long x) { return w[root(x)]; }
};
void solve() {
long long n, m, q;
cin >> n >> m >> q;
UnionFind uf(n + m);
long long cnt = n + m;
for (long long i = 0; i < q; ++i) {
long long r, c;
cin >> r >> c;
--r, --c;
if (!uf.issame(r, c + n)) {
uf.merge(r, c + n);
--cnt;
}
}
cout << cnt - 1 << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 4e5 + 100;
vector<int> adj[MN];
bool mark[MN];
void dfs(int v) {
mark[v] = 1;
int siz = adj[v].size();
for (int i = 0; i < siz; i++) {
int u = adj[v][i];
if (!mark[u]) {
dfs(u);
}
}
}
int main() {
ios::sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].push_back(y + n);
adj[y + n].push_back(x);
}
int cnt = 0;
for (int i = 0; i < n + m; i++) {
if (!mark[i]) {
dfs(i);
cnt++;
}
}
cout << cnt - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adj;
map<int, bool> vis;
void search(int s) {
vis[s] = true;
for (auto x : adj[s]) {
if (!vis[x]) search(x);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int q;
cin >> n >> m >> q;
int x, y;
adj.resize(n + m + 2);
for (int i = 0; i <= n; i++) adj[i].push_back(i);
for (int i = 1; i <= m; i++) adj[n + i].push_back(n + i);
while (q--) {
cin >> x >> y;
adj[x].push_back(y + n);
adj[y + n].push_back(x);
}
int ans = -1;
for (int i = 1; i <= n + m; i++) {
if (!vis[i]) {
ans++;
search(i);
}
}
if (ans < 0)
cout << 0;
else
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using ul = uint64_t;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vpi = vector<pi>;
static constexpr int inf = (int)1e9 + 5;
static constexpr ll infl = (ll)1e18 + 5;
static mt19937 rng((uint32_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count());
vi vis;
vvi adj;
void dfs(int v) {
for (auto& e : adj[v]) {
if (!vis[e]) vis[e] = true, dfs(e);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
int n, m, q;
cin >> n >> m >> q;
vpi s(q);
for (auto& si : s) cin >> si.first >> si.second;
if (n == 1 || m == 1) {
cout << n * m - q << '\n';
return 0;
}
adj.resize(n + m);
vis.resize(n + m);
for (auto& si : s)
adj[si.first - 1].push_back(si.second - 1 + n),
adj[si.second + n - 1].push_back(si.first - 1);
int cnt = 0;
for (int i = 0; i < (n + m); ++i)
if (!vis[i]) vis[i] = true, cnt++, dfs(i);
cout << cnt - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, f[400050];
int F(int x) { return f[x] == x ? x : f[x] = F(f[x]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) f[i] = i;
for (int i = 1, x, y; i <= q; i++) {
scanf("%d%d", &x, &y);
y += n;
x = F(x), y = F(y);
if (x != y) f[x] = y;
}
int ans = 0;
for (int i = 1; i <= n + m; i++)
if (f[i] == i) ans++;
printf("%d\n", ans - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(4e5) + 5;
int vi[maxn], X[maxn], Y[maxn];
queue<int> nxt[maxn];
int n, m, q;
void dfs(int p) {
vi[X[p]] = 1;
vi[Y[p] + n] = 1;
while (!nxt[X[p]].empty()) {
int v = nxt[X[p]].front();
nxt[X[p]].pop();
dfs(v);
}
while (!nxt[Y[p] + n].empty()) {
int v = nxt[Y[p] + n].front();
nxt[Y[p] + n].pop();
dfs(v);
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= q; ++i) {
int x, y;
scanf("%d%d", &x, &y);
X[i] = x;
Y[i] = y;
nxt[x].push(i);
nxt[y + n].push(i);
}
int R = 0, C = 0, W = 0;
for (int i = 1; i <= n; ++i)
if (nxt[i].empty()) ++R;
for (int i = 1; i <= m; ++i)
if (nxt[i + n].empty()) ++C;
for (int i = 1; i <= q; ++i)
if (!nxt[X[i]].empty()) dfs(i), ++W;
cout << W + R + C - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct dsu {
vector<int> p, s;
dsu(int n) {
s.resize(n, 1);
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
}
int get(int a) {
if (p[a] == a) return a;
return p[a] = get(p[a]);
}
void merge(int a, int b) {
a = get(a), b = get(b);
if (a == b) return;
if (s[a] > s[b])
p[b] = a, s[a] += s[b];
else
p[a] = b, s[b] += s[a];
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> pos_x(n), pos_y(m);
if (q == 0) {
cout << n + m - 1 << '\n';
return 0;
}
while (q--) {
int a, b;
cin >> a >> b;
pos_x[a - 1].push_back(b - 1);
pos_y[b - 1].push_back(a - 1);
}
dsu x(n), y(m);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (!pos_x[i].size()) ++ans;
for (int j : pos_x[i]) y.merge(j, pos_x[i][0]);
}
for (int i = 0; i < m; ++i) {
if (!pos_y[i].size()) ++ans;
for (int j : pos_y[i]) x.merge(j, pos_y[i][0]);
}
set<int> comp_x, comp_y;
for (int i = 0; i < n; ++i) {
if (pos_x[i].size()) comp_x.insert(x.get(i));
}
for (int i = 0; i < m; ++i) {
if (pos_y[i].size()) comp_y.insert(y.get(i));
}
cout << ans + min(max((int)comp_y.size() - 1, 0),
max((int)comp_x.size() - 1, 0))
<< '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int fa[N];
bool book[N];
int Find(int x) {
if (fa[x] < 0) return x;
return fa[x] = Find(fa[x]);
}
void init(int n, int m) { memset(fa, -1, sizeof(int) * (n + m + 1)); }
void Union(int x, int y) {
int rx = Find(x);
int ry = Find(y);
if (rx == ry) return;
if (fa[rx] > fa[ry]) {
fa[ry] += fa[rx];
fa[rx] = ry;
} else {
fa[rx] += fa[ry];
fa[ry] = rx;
}
}
int n, m, q;
int main() {
scanf("%d%d%d", &n, &m, &q);
init(n, m);
int a, b;
for (int i = 1; i <= q; ++i) {
scanf("%d%d", &a, &b);
Union(a, n + b);
}
int ans = 0;
for (int i = 1; i <= n + m; ++i) {
if (fa[i] < 0) ++ans;
}
printf("%d\n", ans - 1);
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> V[400005];
bool used[400005] = {};
void dfs(int i, int pa) {
used[i] = 1;
for (auto x : V[i]) {
if (!used[x]) {
dfs(x, i);
}
}
}
int main() {
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
;
while (q--) {
int a, b;
scanf("%d %d", &a, &b);
;
b += n;
V[a].push_back(b);
V[b].push_back(a);
}
int comp = 0;
for (int i = 1; i <= n + m; i++) {
if (!used[i]) {
comp++;
dfs(i, i);
}
}
cout << comp - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 15;
int pre[maxn * 2];
void init(int n) {
for (int i = 0; i <= n; i++) pre[i] = i;
}
int Find(int x) {
if (x == pre[x])
return x;
else
return pre[x] = Find(pre[x]);
}
int main() {
int n, m, t;
cin >> n >> m >> t;
init(n + m);
int ans = n + m;
while (t--) {
int x, y;
cin >> x >> y;
int xx = Find(x);
int yy = Find(n + y);
if (xx != yy) {
ans -= 1;
pre[xx] = yy;
}
}
cout << ans - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int pri[400100];
int find(int x) { return pri[x] == x ? x : pri[x] = find(pri[x]); }
void join(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) pri[fx] = fy;
}
int main() {
int n, m, t;
cin >> n >> m >> t;
for (int i = 1; i <= n + m; i++) pri[i] = i;
while (t--) {
int x, y;
cin >> x >> y;
join(x, y + n);
}
int ans = 0;
for (int i = 1; i <= n + m; i++)
if (find(i) == i) ans++;
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct UnionFind {
vector<long long> par;
UnionFind(long long N) : par(N) {
for (long long i = 0; i < N; i++) par[i] = i;
}
long long root(long long x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
if (rx == ry) return;
par[rx] = ry;
}
bool same(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
return rx == ry;
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N, M, Q;
cin >> N >> M >> Q;
UnionFind uni(N + M);
for (long long i = 0; i < Q; i++) {
long long x, y;
cin >> x >> y;
x--;
y--;
uni.unite(x, y + N);
}
set<long long> S;
for (long long i = 0; i < N + M; i++) S.insert(uni.root(i));
cout << S.size() - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
const int maxn = 2e5 + 20;
int n, m, q;
struct Ele {
int r, c;
} ele[maxn];
bool cmpr(const Ele &a, const Ele &b) {
if (a.r != b.r) return a.r < b.r;
return a.c < b.c;
}
bool cmpc(const Ele &a, const Ele &b) {
if (a.c != b.c) return a.c < b.c;
return a.r < b.r;
}
int rf[maxn], cf[maxn];
void init() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1, x, y; i <= q; i++) {
scanf("%d%d", &x, &y);
ele[i] = (Ele){x, y};
}
}
int find(int x, int fa[]) { return x == fa[x] ? x : fa[x] = find(fa[x], fa); }
void solve_row() {
for (int i = 1; i <= n; i++) rf[i] = i;
std::sort(ele + 1, ele + q + 1, cmpc);
for (int i = 1, j = 1; i <= q; i = j) {
while (j <= q && ele[j].c == ele[i].c) j++;
for (int k = i; k < j; k++) {
rf[find(ele[k].r, rf)] = find(ele[i].r, rf);
}
}
}
void solve_col() {
for (int i = 1; i <= m; i++) cf[i] = i;
std::sort(ele + 1, ele + q + 1, cmpr);
for (int i = 1, j = 1; i <= q; i = j) {
while (j <= q && ele[j].r == ele[i].r) j++;
for (int k = i; k < j; k++) {
cf[find(ele[k].c, cf)] = find(ele[i].c, cf);
}
}
}
void solve() {
static bool R[maxn], C[maxn];
for (int i = 1; i <= n; i++) R[i] = false;
for (int i = 1; i <= m; i++) C[i] = false;
for (int i = 1; i <= q; i++) {
R[find(ele[i].r, rf)] = true;
C[find(ele[i].c, cf)] = true;
}
int ans = 0, cnt = 0;
for (int i = 1; i <= n; i++)
if (find(i, rf) == i && R[i] == false) ans++;
for (int i = 1; i <= m; i++)
if (find(i, cf) == i && C[i] == false) ans++;
for (int i = 1; i <= n; i++)
if (find(i, rf) == i && R[i] == true) cnt++;
ans += cnt - 1;
printf("%d\n", ans);
}
int main() {
init();
if (q == 0) {
printf("%d\n", m + n - 1);
} else {
solve_row();
solve_col();
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
class DSU {
int n;
int* p;
int* w;
public:
DSU(int _n) {
n = _n;
p = new int[n];
w = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
w[i] = 1;
}
}
int getNum(int a) {
int b = a;
while (p[b] != b) b = p[b];
while (p[a] != a) {
int t = p[a];
p[a] = b;
a = t;
}
return b;
}
void setUnion(int a, int b) {
int p1 = getNum(a);
int p2 = getNum(b);
if (w[p1] <= w[p2]) {
w[p2] += w[p1];
p[p1] = p2;
} else {
w[p1] += w[p2];
p[p2] = p1;
}
}
bool isEqual(int a, int b) { return getNum(a) == getNum(b); }
int getSize(int a) { return w[getNum(a)]; }
};
int main() {
int m, n, q;
cin >> n >> m >> q;
DSU d(m + n);
int ans = m + n - 1;
for (int i = 0; i < q; i++) {
int r, c;
cin >> r >> c;
r--;
c--;
if (!d.isEqual(c, m + r)) {
d.setUnion(c, m + r);
ans--;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
int fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i <= n + m; i++) fa[i] = i;
for (int i = 1; i <= q; i++) {
int r, c;
cin >> r >> c;
int t1 = find(r), t2 = find(c + n);
fa[t1] = t2;
}
int ans = 0;
for (int i = 1; i <= n + 1 + m; i++) {
if (find(i) == i) ans++;
}
cout << ans - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << " = " << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << " = " << h << ", ";
_dbg(sdbg + 1, a...);
}
const int maxn = (1e6) + 7;
const int maxk = 20;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
int F[maxn], G[maxn];
int finf(int a) {
if (F[a] != a) F[a] = finf(F[a]);
return F[a];
}
int fing(int a) {
if (G[a] != a) G[a] = fing(G[a]);
return G[a];
}
void unif(int a, int b) { F[finf(a)] = finf(b); }
void unig(int a, int b) { G[fing(a)] = fing(b); }
vector<int> wek[maxn], xd[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= max(m, n); i++) G[i] = F[i] = i;
while (q--) {
int a, b;
cin >> a >> b;
wek[a].push_back(b);
xd[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j + 1 < ((int)(wek[i]).size()); j++)
unig(wek[i][j], wek[i][j + 1]);
}
for (int i = 1; i <= m; i++) {
for (int j = 0; j + 1 < ((int)(xd[i]).size()); j++)
unif(xd[i][j], xd[i][j + 1]);
}
int ile = 0;
int ile2 = 0;
int wol = 0;
int wol2 = 0;
for (int i = 1; i <= n; i++)
if (finf(i) == i) {
if (((int)(wek[i]).size()) == 0)
wol++;
else
ile++;
}
for (int i = 1; i <= m; i++)
if (fing(i) == i) {
if (((int)(xd[i]).size()) == 0)
wol2++;
else
ile2++;
}
cout << ile + wol + wol2 - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[400010];
int n, m, q;
vector<int> color(400010);
void dfs(int u, int cc) {
color[u] = cc;
for (int i = 0; i < g[u].size(); ++i) {
if (color[g[u][i]] == -1) dfs(g[u][i], cc);
}
}
int main() {
cin >> n >> m >> q;
set<int> s;
fill(color.begin(), color.end(), -1);
for (int i = 0; i < q; ++i) {
int r, c;
cin >> r >> c;
g[r].push_back(c + n);
g[c + n].push_back(r);
s.insert(r);
s.insert(c + n);
}
int cc = 0;
long long res = -1;
for (int i = 1; i <= n + m; ++i) {
if (color[i] == -1) {
dfs(i, cc);
res++;
}
}
cout << res;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class K>
inline bool cmax(K& a, const K& b) {
return (a < b) ? a = b, 1 : 0;
}
template <class K>
inline bool cmin(K& a, const K& b) {
return (a > b) ? a = b, 1 : 0;
}
inline int read() {
register int s = 0;
register char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) s = (s << 1) + (s << 3) + c - '0', c = getchar();
return s;
}
int n, m, p, f[400007], res;
inline int get(register int x) { return (f[x] != x) ? f[x] = get(f[x]) : x; }
int main() {
register int i, x, y;
n = read(), m = read(), p = read();
for (i = 1; i <= n + m; i++) f[i] = i;
for (i = 1; i <= p; i++) {
x = get(read()), y = get(read() + n);
if (x != y) {
f[y] = x, ++res;
}
}
printf("%d", n + m - res - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5;
int n, m, q;
vector<int> G[maxn];
int vis[maxn];
void dfs(int x) {
vis[x] = 1;
for (int i = 0; i < G[x].size(); i++)
if (!vis[G[x][i]]) dfs(G[x][i]);
}
int main() {
scanf("%d%d%d", &n, &m, &q);
int x, y;
for (int i = 1; i <= q; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y + n);
G[y + n].push_back(x);
}
int cnt = 0;
for (int i = 1; i <= (n + m); i++) {
if (!vis[i]) {
dfs(i);
cnt++;
}
}
printf("%d\n", cnt - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
return x;
}
const int N = 400005;
int n, m, q;
vector<int> r[N], c[N];
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int vis[N];
int main() {
n = read(), m = read(), q = read();
if (q == 0) {
printf("%d\n", n + m - 1);
return 0;
}
for (int i = 1; i <= n; i++) r[i].clear();
for (int i = 1; i <= m; i++) c[i].clear();
for (int i = 1; i <= q; i++) {
int x = read(), y = read();
r[x].push_back(y);
c[y].push_back(x);
}
for (int i = 1; i <= n + m; i++) fa[i] = i;
for (int i = 1; i <= n; i++)
for (int j = 1; j < r[i].size(); j++)
fa[getf(r[i][j] + n)] = getf(r[i][j - 1] + n);
for (int i = 1; i <= m; i++)
for (int j = 1; j < c[i].size(); j++) fa[getf(c[i][j])] = getf(c[i][j - 1]);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n + m; i++) vis[getf(i)]++;
int x = 0, y = 0, xx = 0, yy = 0;
for (int i = 1; i <= n; i++) {
if (r[i].size() < 1)
xx++;
else if (vis[i] > 0)
x++;
}
for (int i = n + 1; i <= n + m; i++) {
if (c[i - n].size() < 1)
yy++;
else if (vis[i] > 0)
y++;
}
printf("%d", max(x, y) + xx + yy - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, ans = 0, i, x, y, dsu[400005];
int get(int p) { return dsu[p] == p ? p : dsu[p] = get(dsu[p]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
for (i = 1; i <= n + m; i++) dsu[i] = i;
for (i = 1; i <= q; i++) {
scanf("%d%d", &x, &y);
dsu[get(x)] = get(n + y);
}
for (i = 1; i <= n + m; i++)
if (dsu[i] == i) ans++;
printf("%d", --ans);
return 0;
}
|
#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--;
vector<bool> bs(n, false);
for (int i = 0; i < n; ++i) {
int p = d.find(i);
ans += bs[p] == 0;
bs[p] = 1;
}
cout << ans - 1 << el;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> was;
void dfs(int v) {
was[v] = 1;
for (auto &u : g[v])
if (!was[u]) dfs(u);
}
int main() {
int n, m, q;
cin >> n >> m >> q;
g.resize(n + m);
was.resize(n + m);
int a, b;
for (int i = 0; i < q; i++) {
cin >> a >> b;
a--;
b--;
g[a].push_back(n + b);
g[n + b].push_back(a);
}
int ans = 0;
for (int i = 0; i < n + m; i++) {
if (!was[i]) {
ans++;
dfs(i);
}
}
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 10000010;
int n, m, q;
int p[N];
int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
int main() {
cin >> n >> m >> q;
int ans = n + m, temp = max(m, n);
for (int i = 1; i <= n + m; i++) {
p[i] = i;
}
while (q--) {
int a, b;
cin >> a >> b;
int pa = find(a), pb = find(b + n);
if (pa != pb) {
ans--;
p[pa] = pb;
}
}
cout << ans - 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<int> g[400400];
bool used[400400];
void dfs(int v) {
used[v] = true;
for (int i = 0; i < g[v].size(); i++) {
if (used[g[v][i]]) {
continue;
}
dfs(g[v][i]);
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < k; i++) {
int v, u;
cin >> v >> u;
v--;
u += n - 1;
g[v].push_back(u);
g[u].push_back(v);
}
int ans = 0;
for (int i = 0; i < n + m; i++) {
if (used[i]) {
continue;
}
dfs(i);
ans++;
}
cout << ans - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int fa[400010];
void init(int n) {
for (int i = 0; i <= n; i++) fa[i] = i;
}
int find(int x) {
int r = x;
while (fa[r] != r) r = fa[r];
int i = x, j;
while (i != r) {
j = fa[i];
fa[i] = r;
i = j;
}
return r;
}
void join(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) fa[fx] = fa[y];
}
map<int, int> mp;
int main() {
int n, m, q, i, r, c;
cin >> n >> m >> q;
init(n + m);
for (i = 0; i < q; i++) {
cin >> r >> c;
c += n;
join(r, c);
}
for (i = 1; i <= n + m; i++) {
int cc = find(i);
mp[cc]++;
}
cout << mp.size() - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, s, ans = 0, f[2 * 200005];
inline int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
scanf("%d%d%d", &n, &m, &s);
for (register int i = 1; i <= m + n; i++) f[i] = i;
for (register int i = 1; i <= s; i++) {
register int a, b;
scanf("%d%d", &a, &b), b += n;
f[find(b)] = find(a);
}
for (register int i = 1; i <= n + m; i++)
if (f[i] == i) ans++;
printf("%d", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
struct UnionFind {
V<int> ig;
VV<int> gi;
int gc;
UnionFind(int n) {
ig = V<int>(n);
gi = VV<int>(n);
for (int i = 0; i < n; i++) {
ig[i] = i;
gi[i] = {i};
}
gc = n;
}
void merge(int a, int b) {
if (same(a, b)) return;
gc--;
int x = ig[a], y = ig[b];
if (gi[x].size() < gi[y].size()) swap(x, y);
for (int j : gi[y]) {
ig[j] = x;
}
gi[x].insert(gi[x].end(), gi[y].begin(), gi[y].end());
gi[y].clear();
}
bool same(int a, int b) { return ig[a] == ig[b]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
auto uf = UnionFind(n + m);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
uf.merge(x, n + y);
}
cout << uf.gc - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, fa[1000010], siz[1000010];
void init(int xx) {
for (int i = 1; i <= xx; i++) {
fa[i] = i;
siz[i] = 1;
}
return;
}
int find(int xx) {
if (fa[xx] == xx) return xx;
fa[xx] = find(fa[xx]);
return fa[xx];
}
void merge(int xx, int yy) {
int fx = find(xx), fy = find(yy);
if (fx == fy) return;
if (siz[fx] < siz[fy]) {
fa[fx] = fy;
siz[fy] += siz[fx];
fa[xx] = fa[yy] = fy;
return;
} else {
fa[fy] = fx;
siz[fx] += siz[fy];
fa[xx] = fa[yy] = fx;
return;
}
}
int getcnt(int xx) {
int ans = 0;
for (int i = 1; i <= xx; i++)
if (fa[i] == i) ans++;
return ans;
}
int main() {
scanf("%d%d%d", &n, &m, &q);
init(n + m);
for (int i = 1; i <= q; i++) {
int x, y;
scanf("%d%d", &x, &y);
merge(x, y + n);
}
printf("%d\n", getcnt(n + m) - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
int mar[N], used[N];
int n, m, q, color, sz, cnt;
vector<int> ed[N], lst[N];
void dfs(int v) {
mar[v] = 1;
sz++;
for (int i = 0; i < lst[v].size(); i++) {
int c = lst[v][i];
if (used[c] != color) {
cnt++;
used[c] = color;
}
}
for (int i = 0; i < ed[v].size(); i++) {
int to = ed[v][i];
if (mar[to] != 0) continue;
dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> q;
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
lst[x].push_back(y);
ed[x].push_back(y + n);
ed[y + n].push_back(x);
}
color = 0;
long long ans = 0;
int cmp = 0;
for (int i = 1; i <= n; i++) {
if (mar[i] == 0) {
sz = cnt = 0;
color++;
dfs(i);
cmp++;
}
}
ans += cmp - 1;
for (int i = 1; i <= m; i++)
if (used[i] == 0) ans++;
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
struct DSU {
int par[N];
DSU() {
for (int i = 0; i < N; i++) par[i] = i;
}
int find(int u) {
if (par[u] == u) return u;
return par[u] = find(par[u]);
}
void merge(int u, int v) { par[find(v)] = find(u); }
};
vector<int> rowvals[N];
vector<int> colvals[N];
bool rowhas[N], colhas[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<pair<int, int> > st;
set<pair<int, int> > st2;
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
st.push_back(pair<int, int>(x, y));
rowvals[x].push_back(y);
colvals[y].push_back(x);
}
DSU rows, cols;
for (int i = 0; i < n; i++) {
if (rowvals[i].empty()) continue;
for (auto u : rowvals[i]) cols.merge(u, rowvals[i][0]);
}
for (int i = 0; i < m; i++) {
if (colvals[i].empty()) continue;
for (auto u : colvals[i]) rows.merge(u, colvals[i][0]);
}
for (auto pr : st) {
int x = rows.find(pr.first);
int y = cols.find(pr.second);
rowhas[x] = 1;
colhas[y] = 1;
}
int nn = 0, mm = 0, kn = 0, km = 0;
for (int i = 0; i < n; i++)
if (rows.par[i] == i) {
nn++;
if (rowhas[i]) kn++;
}
for (int i = 0; i < m; i++)
if (cols.par[i] == i) {
++mm;
if (colhas[i]) km++;
}
assert(kn == km);
cout << (mm + nn) - (kn + 1) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A>
string to_string(A);
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool f = 1;
string r = "{";
for (const auto &x : v) {
if (!f) r += ", ";
f = 0;
r += to_string(x);
}
return r + "}";
}
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << " " << to_string(H);
debug_out(T...);
}
inline int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
inline int mul(int a, int b) { return (int)((long long)a * b % MOD); }
inline int binpow(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b /= 2;
}
return res;
}
inline int inv(int a) { return binpow(a, MOD - 2); }
int gcd(int a, int b, int &x, int &y) {
if (a == 0) {
x = 0, y = 1;
return b;
}
int x1, y1;
int d = gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
const int N = 2e5 + 5;
int n, m, q, p[N], comp, size[N];
void init() {
for (int i = 1; i <= n; ++i) p[i] = i, size[i] = 1;
comp = n;
}
int root(int x) {
if (x != p[x]) p[x] = root(p[x]);
return p[x];
}
void merge(int x, int y) {
x = root(x), y = root(y);
if (x == y) return;
if (size[x] < size[y]) swap(x, y);
p[y] = x;
size[x] += size[y];
comp--;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
init();
vector<int> v[m + 1];
for (int i = 0; i < q; ++i) {
int x, y;
cin >> x >> y;
v[y].push_back(x);
}
int emp = 0;
for (int i = 1; i <= m; ++i) {
if (v[i].empty()) {
emp++;
continue;
}
for (int j = 1; j < v[i].size(); ++j) merge(v[i][j], v[i][0]);
}
cout << comp - 1 + emp << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long ksm(long long a, long long b) {
long long ns = ksm(a, b >> 1);
ns = ns * ns % 998244353;
if (b & 1) ns = ns * a % 998244353;
return ns;
}
int fa[400005];
int gfa(int a) {
if (fa[a] == a) return a;
return fa[a] = gfa(fa[a]);
}
int main() {
int n, m, q;
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[gfa(y + n)] = gfa(x);
}
int tot = 0;
for (int i = 1; i <= n + m; i++)
if (fa[i] == i) tot++;
printf("%d\n", tot - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool minimize(X &x, const Y &y) {
X eps = 1e-9;
if (x > y + eps) {
x = y;
return true;
} else
return false;
}
template <class X, class Y>
bool maximize(X &x, const Y &y) {
X eps = 1e-9;
if (x + eps < y) {
x = y;
return true;
} else
return false;
}
template <class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
class DisjointSet {
private:
vector<int> label;
public:
DisjointSet() {}
DisjointSet(int n) { label.assign(n + 7, -1); }
int find(int x) {
if (label[x] < 0) return (x);
label[x] = find(label[x]);
return (label[x]);
}
bool join(int a, int b) {
int x = find(a);
int y = find(b);
if (x == y) return (false);
if (label[x] > label[y]) swap(x, y);
label[x] += label[y];
label[y] = x;
return (true);
}
int getSize(int x) { return (-label[find(x)]); }
};
int main(void) {
int numRow, numCol, numEdge;
scanf("%d%d%d", &numRow, &numCol, &numEdge);
DisjointSet dsu(numRow + numCol);
int numComp = numRow + numCol;
for (int love = 0, _n = (numEdge); love < _n; love++) {
int x, y;
scanf("%d%d", &x, &y);
if (dsu.join(x, numRow + y)) numComp--;
}
cout << numComp - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300000;
int f[2 * N];
int n, m;
void init() {
for (int i = 0; i <= n + m + 2; i++) {
f[i] = i;
}
}
int find1(int x) {
if (f[x] == x) return f[x];
f[x] = find1(f[x]);
return f[x];
}
bool merge(int x, int y) {
int t1 = find1(x);
int t2 = find1(y);
if (t1 != t2) {
f[t2] = t1;
return true;
}
return false;
}
int main() {
long long t;
scanf("%d %d %lld", &n, &m, &t);
init();
long long ans = n + m - 1;
while (t--) {
int x, y;
scanf("%d %d", &x, &y);
y += n;
if (merge(x, y)) {
ans--;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> a[400002];
bool vis[400002];
void dfs(int u) {
vis[u] = 1;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i];
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
cin >> n >> m >> q;
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
y += n;
a[x].push_back(y);
a[y].push_back(x);
}
int cnt = 0;
for (int i = 1; i <= n + m; i++)
if (!vis[i]) {
dfs(i);
cnt++;
}
cout << cnt - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int par[maxn * 2], sz[maxn * 2];
bool vis[maxn * 2];
int n, m, q, ans;
int root(int v) { return v == par[v] ? v : par[v] = root(par[v]); }
void unite(int a, int b) {
if ((a = root(a)) == (b = root(b))) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < n + m; ++i) par[i] = i, sz[i] = 1;
while (q--) {
int a, b;
cin >> a >> b;
a--, b--;
b += n;
unite(a, b);
}
for (int i = 0; i < n + m; ++i) {
ans += !vis[root(i)];
vis[root(i)] = true;
}
cout << ans - 1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
vector<char*> tokenizer(const char* args) {
char* token = new char[111];
strcpy(token, args);
token = strtok(token, ", ");
vector<char*> v({token});
while (token = strtok(NULL, ", ")) v.push_back(token);
return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char*> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char*> args, Head H, Tail... T) {
debug() << " [" << args.back() << ": " << H << "] ";
args.pop_back();
debugg(args, T...);
}
long long mod = 1e9 + 7;
const int MX = 0x3f3f3f3f;
vector<int> adj[int(1e6) + 1];
int vis[int(1e6) + 1];
void dfs(int i) {
vis[i] = 1;
for (auto u : adj[i]) {
if (!vis[u]) dfs(u);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q, i, u, v;
cin >> n >> m >> q;
int cnt = 0;
for (i = 1; i <= q; i++) {
cin >> u >> v;
adj[u].emplace_back(n + v);
adj[n + v].emplace_back(u);
}
for (i = 1; i <= n + m; i++) {
if (!vis[i]) {
cnt++;
dfs(i);
}
}
cout << cnt - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int find(int n) {
if (p[n] < 0) return n;
return p[n] = find(p[n]);
}
bool merge(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
p[a] = b;
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, ans = 0, q;
cin >> n >> m >> q;
p.assign(n + m + 1, -1);
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
merge(a, n + b);
}
for (int i = 1; i < n + m; i++) {
ans += merge(i, i + 1);
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int father[400500], v[400500];
int fa(int x) {
if (father[x] == x) return x;
return father[x] = fa(father[x]);
}
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) father[i] = i;
for (int i = 1; i <= q; i++) {
int x, y;
scanf("%d%d", &x, &y);
int fx = fa(x), fy = fa(n + y);
if (fx != fy) father[fx] = fy;
}
long long ans = 0;
for (int i = 1; i <= n + m; i++)
if (!v[fa(i)]) ans++, v[fa(i)] = 1;
printf("%I64d", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int fa[400010];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
int n, m, q, ans = -1;
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) fa[i] = i;
for (int i = 1; i <= q; i++) fa[find(gi())] = find(gi() + n);
for (int i = 1; i <= n + m; i++) ans += (fa[i] == i);
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int __ = 600005;
struct DisjointSetUnion {
int pre[__];
DisjointSetUnion() { memset(pre, -1, sizeof(pre)); }
void un(int x, int y) {
x = fd(x), y = fd(y);
if (x == y) return;
if (pre[x] < pre[y])
pre[x] += pre[y], pre[y] = x;
else
pre[y] += pre[x], pre[x] = y;
}
int fd(int x) {
if (pre[x] < 0) return x;
return pre[x] = fd(pre[x]);
}
void clear() { memset(pre, -1, sizeof(pre)); }
} dsu;
bool vis[__];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = (1); i <= (q); ++i) {
int x, y;
scanf("%d%d", &x, &y);
dsu.un(x, y + n);
}
int ans = 0;
for (int i = (1); i <= (n + m); ++i)
if (dsu.fd(i) != dsu.fd(1)) dsu.un(i, 1), ++ans;
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
using namespace std;
const int N = 500500;
const long long INF = 0x3f3f3f3f;
int ans, n, m, q, x, y, fa[N];
bitset<N> vis;
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
signed 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++) {
scanf("%d%d", &x, &y);
y += n;
int fx = Find(x), fy = Find(y);
if (fx == fy) continue;
fa[fx] = fy;
}
for (int i = 1; i <= n + m; i++) Find(i);
for (int i = 1; i <= n + m; i++) {
int x = Find(i);
vis[x] = true;
}
for (int i = 1; i <= n + m; i++) ans += vis[i];
printf("%d\n", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
list<int> ar[200001];
list<int> ac[200001];
bool is_ar[200001];
bool is_ac[200001];
void dfs_r(int i);
void dfs_c(int i);
void dfs_r(int i) {
list<int>::iterator it;
for (it = ar[i].begin(); it != ar[i].end(); ++it) {
if (!is_ac[*it]) {
is_ac[*it] = true;
dfs_c(*it);
}
}
}
void dfs_c(int i) {
list<int>::iterator it;
for (it = ac[i].begin(); it != ac[i].end(); ++it) {
if (!is_ar[*it]) {
is_ar[*it] = true;
dfs_r(*it);
}
}
}
int main() {
int n, m, q, i, j, k;
scanf("%d%d%d", &n, &m, &q);
if (q <= 1) {
printf("%d\n", n + m - 1 - q);
return 0;
}
for (i = 0; i < n; i++) is_ar[i] = false;
for (i = 0; i < m; i++) is_ac[i] = false;
for (i = 0; i < q; i++) {
scanf("%d%d", &j, &k);
j--;
k--;
ar[j].push_back(k);
ac[k].push_back(j);
}
for (i = 0; i < n; i++) {
if (!is_ar[i]) {
is_ar[i] = true;
dfs_r(i);
ans++;
}
}
for (i = 0; i < m; i++) {
if (!is_ac[i]) {
is_ac[i] = true;
dfs_c(i);
ans++;
}
}
printf("%d\n", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int par[N];
bool vis[N];
int fin(int x) {
while (par[x] != x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
}
void un(int x, int y) {
int px = fin(x);
int py = fin(y);
if (px != py) {
par[px] = py;
}
}
int main() {
int n, m, q, r, c;
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= n + m; ++i) par[i] = i;
for (int i = 1; i <= q; ++i) {
scanf("%d %d", &r, &c);
c += n;
un(r, c);
}
int ans = -1;
for (int i = 1; i <= n + m; ++i)
if (vis[fin(i)] == 0) {
vis[fin(i)] = 1;
++ans;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, q, x, y, ans;
int fa[400002];
int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); }
void unionn(int x, int y) { fa[find(y)] = find(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 >> x >> y;
y += n;
unionn(x, y);
}
for (int i = 1; i <= n + m; ++i) ans += fa[i] == i;
cout << ans - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read();
inline void write(int x);
const int M = 500016, MOD = 1000000007;
vector<int> pr[M];
vector<int> pc[M];
int visr[M], visc[M];
vector<pair<int, int>> save;
int main(void) {
int n = read(), m = read(), q = read();
for (int i = 0; i < q; i++) {
int r = read(), c = read();
save.emplace_back(r, c);
if (visr[r] == 0) n--, visr[r] = -1;
if (visc[c] == 0) m--, visc[c] = -1;
pr[r].push_back(c);
pc[c].push_back(r);
}
queue<int> que;
int cont = 0;
for (auto x : save) {
if (visr[x.first] != 1) {
visr[x.first] = 1;
que.push(x.first);
cont++;
}
while (!que.empty()) {
int r = que.front();
que.pop();
for (auto c : pr[r])
if (visc[c] != 1) {
visc[c] = 1;
for (auto xr : pc[c])
if (visr[xr] != 1) {
visr[xr] = 1;
que.push(xr);
}
}
}
}
printf("%d\n", cont + n + m - 1);
return 0;
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, mod2 = 998244353;
unsigned long long base = 131;
const int w = 4e5 + 5;
int n, m, k;
int f[w];
int get(int x) {
if (x != f[x]) f[x] = get(f[x]);
return f[x];
}
int main() {
int i, j, t;
int q;
cin >> n >> m >> q;
for (i = 1; i <= n + m; i++) f[i] = i;
int ans = n + m - 1;
while (q--) {
int x, y;
scanf("%d %d", &x, &y);
y += n;
int fx = get(x), fy = get(y);
if (fx != fy) {
f[fx] = fy;
ans--;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int SZ = 2e5 + 100;
int p[SZ], r[SZ];
vector<int> c[SZ];
int get(int v) {
if (v != p[v]) p[v] = get(p[v]);
return p[v];
}
void join(int u, int v) {
v = get(v);
u = get(u);
if (r[v] < r[u]) swap(v, u);
p[u] = v;
if (r[u] = r[v]) r[v]++;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
int cnt = 0;
int comp = n;
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
if (c[y].empty()) cnt++;
c[y].push_back(x);
}
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 1; i <= m; i++) {
for (int j = 1; j < c[i].size(); j++)
if (get(c[i][0]) != get(c[i][j])) {
join(c[i][0], c[i][j]);
comp--;
}
}
cout << comp - 1 + m - cnt;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
int n, m, q, f[N << 1];
void read(int &x) {
char ch = getchar();
x = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 3) + (x << 1) + ch - '0';
}
int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); }
int main() {
read(n);
read(m);
read(q);
for (int i = 1; i <= n + m; i++) f[i] = i;
while (q--) {
int x, y;
read(x);
read(y);
y += n;
int fx = gf(x), fy = gf(y);
f[fx] = fy;
}
int ans = 0;
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 vi = vector<int>;
using vvi = vector<vi>;
int n, m;
vvi g;
vi s;
void dfs(int u) {
s[u] = 1;
for (int v : g[u])
if (!s[v]) dfs(v);
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int n, m, q, r, c;
cin >> n >> m >> q;
g = vvi(n + m);
for (int i = 0; i < q; i++) {
cin >> r >> c;
r--, c--;
g[r].push_back(c + n);
g[c + n].push_back(r);
}
s = vi(n + m);
int k = 0;
for (int i = 0; i < n + m; i++)
if (!s[i]) k++, dfs(i);
cout << k - 1 << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 400005;
int ans = 0, fa[MAXN], n, m, q, r, c;
int get(int x) {
if (fa[x] == x) return x;
return fa[x] = get(fa[x]);
}
void merge(int x, int y) { fa[get(x)] = get(y); }
bool query(int x, int y) { return get(x) == get(y); }
void Search() {
for (int i = 1; i <= (n + m); i++) {
if (fa[i] == i) {
ans++;
}
}
}
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 >> r >> c;
merge(r, c + n);
}
Search();
cout << ans - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
int H, W, Q;
cin >> W >> H >> Q;
UnionFind uf(H + W);
for (int i = 0; i < Q; ++i) {
int x, y;
scanf("%d %d", &x, &y);
x--;
y--;
uf.unionSet(x, y + W);
}
set<int> aset;
for (int i = 0; i < H + W; ++i) {
aset.emplace(uf.root(i));
}
int answer = aset.size() - 1;
cout << answer << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, q;
int pre[maxn << 1];
int Find(int x) { return x == pre[x] ? x : pre[x] = Find(pre[x]); }
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n + m; i++) pre[i] = i;
int a, b, ans = n + m;
for (int i = 1; i <= q; i++) {
scanf("%d%d", &a, &b);
int x = Find(a), y = Find(b + n);
if (x != y) {
pre[x] = y;
ans--;
}
}
printf("%d\n", ans - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int f[maxn];
int finds(int x) { return f[x] == x ? x : f[x] = finds(f[x]); }
void init(int n) {
for (int i = 1; i <= n; i++) f[i] = i;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
init(n + m);
for (int i = 0; i < q; i++) {
int u, v;
cin >> u >> v;
int fu = finds(u);
int fv = finds(v + n);
if (fu != fv) f[fu] = fv;
}
int ans = 0;
int root = finds(1);
for (int i = 2; i <= n + m; i++) {
if (finds(i) != root) {
ans++;
f[finds(i)] = root;
}
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int parent[400050];
int getroot(int x) {
if (parent[x] != x) {
parent[x] = getroot(parent[x]);
}
return parent[x];
}
void Merge(int a, int b) {
int p1 = getroot(a), p2 = getroot(b);
if (p1 == p2)
return;
else
parent[p1] = p2;
}
int main() {
int n, m, q;
while (scanf("%d%d%d", &n, &m, &q) == 3) {
for (int i = 1; i <= n + m; i++) parent[i] = i;
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
Merge(a, b + n);
}
int ans = -1;
for (int i = 1; i <= n + m; i++)
if (parent[i] == i) ans++;
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int add(int x, int y, int CMOD = MOD) { return (0LL + x + y) % CMOD; }
int mult(int x, int y, int CMOD = MOD) { return (1LL * x * y) % CMOD; }
long long fast_expo(long long x, long long y, long long CMOD = MOD) {
if (x == 0) return 0;
if (y == 0) return 1;
long long ans = fast_expo(x, y / 2, CMOD);
ans = mult(ans, ans, CMOD);
if (y & 1) ans = mult(ans, x, CMOD);
return ans;
}
const int TAM = 2e5 + 100;
const long long INF = LLONG_MAX / 4;
int n;
int a[TAM];
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int k;
cin >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
if (a[n / 2] == k) {
cout << "0" << endl;
return 0;
}
long long ans = 0;
if (a[n / 2] < k) {
for (int i = n / 2; i < n; i++) {
ans += max(0, k - a[i]);
}
} else {
for (int i = n / 2; i >= 0; i--) {
ans += max(0, a[i] - k);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long s;
cin >> n >> s;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long ans = 0;
if (s == a[n / 2]) {
} else if (s > a[n / 2]) {
int i = n / 2;
while (s > a[i] && i < n) {
ans += (s - a[i]);
i++;
}
} else {
int i = n / 2;
while (s < a[i] && i >= 0) {
ans += (a[i] - s);
i--;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n >> m;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long sum = 0;
if (a[n / 2] == m) {
cout << 0 << endl;
return 0;
} else {
sum += abs(m - a[n / 2]);
if (a[n / 2] < m) {
long long i = (n / 2) + 1;
while (i < n && a[i] < m) {
sum += (m - a[i]);
i++;
}
} else {
long long i = (n / 2) - 1;
while (i >= 0 && a[i] > m) {
sum += (a[i] - m);
i--;
}
}
cout << sum << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, ai, cont = 0;
vector<int> arr;
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> ai;
arr.push_back(ai);
}
sort(arr.begin(), arr.end());
long long sm = 0, hm = 0;
for (int i = 0; i < n / 2; i++) {
if (arr[i] > s) hm += arr[i] - s;
if (arr[n - i - 1] < s) sm += s - arr[n - i - 1];
}
cout << hm + sm + abs(arr[n / 2] - s);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, s;
long long int moves;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
vector<int> v(n);
for (auto &it : v) cin >> it;
sort(v.begin(), v.end());
if (v[n / 2] < s) {
for (int i = n / 2; i < n; i++) {
if (v[i] < s)
moves += s - v[i];
else
break;
}
} else {
for (int i = n / 2; i >= 0; i--) {
if (v[i] > s)
moves += v[i] - s;
else
break;
}
}
cout << moves << endl;
return 0;
}
|
#include <bits/stdc++.h>
long long MOD = 1e9 + 7;
using namespace std;
int main() {
int n, s;
cin >> n >> s;
int a[310000];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long sum = 0;
for (int i = 0; i < n; i++) sum += a[i];
int mid = (n - 1) / 2;
if (a[mid] == s) {
cout << 0;
return 0;
}
if (a[mid] < s) {
long long ss = 0;
for (int i = 0; i < mid; i++) {
ss += a[i];
}
for (int i = mid; i <= n - 1; ++i) {
ss += max(s, a[i]);
}
cout << ss - sum;
return 0;
} else {
long long ss = 0;
for (int i = 0; i < mid + 1; i++) {
ss += min(a[i], s);
}
for (int i = mid + 1; i <= n - 1; ++i) ss += a[i];
cout << sum - ss;
return 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long T;
T = 1;
while (T--) {
long long i, n, s;
cin >> n >> s;
long long a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
sort(a, a + n);
if (s >= a[n / 2]) {
for (i = n / 2; i < n && a[i] <= s; i++) ans += abs(s - a[i]);
} else {
for (i = n / 2; i >= 0 && a[i] >= s; i--) ans += abs(s - a[i]);
}
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, s, ans, i, j, k, a[300000];
long long ab(long long dd) {
if (dd < 0) return -dd;
return dd;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> s;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + 1 + n);
ans += ab(s - a[n / 2 + 1]);
for (i = 1; i < n / 2 + 1; i++) {
if (a[i] > s) ans += ab(s - a[i]);
}
for (i = n / 2 + 2; i <= n; i++) {
if (a[i] < s) ans += ab(s - a[i]);
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
int main() {
long long n, s, i, a, ctr = 0;
cin >> n >> s;
for (i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
}
sort(v.begin(), v.end());
long long x = n / 2;
if (v[x] < s)
while (v[x] <= s && x < n) {
ctr += s - v[x];
x++;
}
else
while (v[x] >= s && x >= 0) {
ctr += abs(s - v[x]);
x--;
}
cout << ctr;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
bool Debug;
const int mod = 1e9 + 7, MAXN = 1e5 + 7, inft = 1e9;
const long long infl = 1ll << 60;
template <class A, class B>
void chmax(A &x, B y) {
if (x < y) x = y;
}
template <class A, class B>
void chmin(A &x, B y) {
if (x > y) x = y;
}
template <class A, class B>
A max(A a, B b) {
return a > b ? a : b;
}
template <class A, class B>
A min(A a, B b) {
return a < b ? a : b;
}
template <class A, class B>
A gcd(A a, B b) {
if (a < b) swap(a, b);
if (!b) return a;
while (A t = a % b) a = b, b = t;
return b;
}
template <class A, class B>
A lcm(A a, B b) {
return a / gcd(a, b) * b;
}
template <class A, class B>
A Pow(A a, B b) {
A ret = 1;
for (; b; b >>= 1) {
if (b & 1) ret = ret * 1ll * a % mod;
a = a * 1ll * a % mod;
}
return ret % mod;
}
template <class T>
inline T abs(T x) {
return x >= 0 ? x : -x;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
bool IS(int x) { return x == 10 || x == 13 || x == ' '; }
struct IO {
struct Cg {
inline int operator()() { return getchar(); }
};
struct Cp {
inline void operator()(int x) { putchar(x); }
};
template <typename T>
struct Fr {
int f;
T P;
inline Fr &operator,(int &x) {
x = 0;
int t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
f = 1;
if (t == '-') t = P(), f = -1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
x *= f;
return *this;
}
inline operator int() {
int x;
*this, x;
return x;
}
inline Fr &operator,(long long &x) {
x = 0;
int t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
f = 1;
if (t == '-') t = P(), f = -1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
x *= f;
return *this;
}
inline operator long long() {
long long x;
*this, x;
return x;
}
inline Fr &operator,(char &x) {
for (x = P(); IS(x); x = P())
;
return *this;
}
inline operator char() {
char x;
*this, x;
return x;
}
inline Fr &operator,(char *x) {
char t = P();
for (; IS(t); t = P())
;
if (~t) {
for (; !IS(t) && ~t; t = P()) *x++ = t;
}
*x++ = 0;
return *this;
}
inline Fr &operator,(double &x) {
x = 0;
int t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
f = 1;
if (t == '-') t = P(), f = -1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (t == '.') {
double u = 0.1;
for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1)
x += u * (t - '0');
}
if (f == -1) x = -x;
;
return *this;
}
inline operator double() {
double x;
*this, x;
return x;
}
inline Fr &operator,(long double &x) {
x = 0;
int t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
f = 1;
if (t == '-') t = P(), f = -1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (t == '.') {
double u = 0.1;
for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1)
x += u * (t - '0');
}
if (f == -1) x = -x;
;
return *this;
}
inline operator long double() {
long double x;
*this, x;
return x;
}
inline Fr &operator,(unsigned int &x) {
x = 0;
int t = P();
while (t < '0' || t > '9') t = P();
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
return *this;
}
inline operator unsigned int() {
unsigned int x;
*this, x;
return x;
}
inline Fr &operator,(unsigned long long &x) {
x = 0;
int t = P();
while (t < '0' || t > '9') t = P();
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
return *this;
}
inline operator unsigned long long() {
unsigned long long x;
*this, x;
return x;
}
};
Fr<Cg> in;
template <typename T>
struct Fw {
int c, s[24];
T P;
inline Fw &operator,(int x) {
if (x) {
if (x < 0) P('-'), x = -x;
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(int x) {
if (x) {
if (x < 0) P('-'), x = -x;
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(unsigned int x) {
if (x) {
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(unsigned int x) {
if (x) {
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(long long x) {
if (x) {
if (x < 0) P('-'), x = -x;
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(long long x) {
if (x) {
if (x < 0) P('-'), x = -x;
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(unsigned long long x) {
if (x) {
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(unsigned long long x) {
if (x) {
c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(char x) {
P(x);
return *this;
}
inline Fw &operator()(char x) {
P(x);
return *this;
}
inline Fw &operator,(const char *x) {
while (*x) P(*x++);
return *this;
}
inline Fw &operator()(const char *x) {
while (*x) P(*x++);
return *this;
}
inline Fw &operator()(double x, int y) {
if (y) {
double t = 0.5;
for (int i = y; i--;) t *= 0.1;
if (x >= 0)
x += t;
else
x -= t, P('-');
*this, (long long)(abs(x));
P('.');
if (x < 0) x = -x;
while (y--) {
x *= 10;
x -= floor(x * 0.1) * 10;
P(((int)x) % 10 + '0');
}
} else if (x >= 0)
*this, (long long)(x + 0.5);
else
*this, (long long)(x - 0.5);
;
return *this;
}
inline Fw &operator()(long double x, int y) {
if (y) {
double t = 0.5;
for (int i = y; i--;) t *= 0.1;
if (x >= 0)
x += t;
else
x -= t, P('-');
*this, (long long)(abs(x));
P('.');
if (x < 0) x = -x;
while (y--) {
x *= 10;
x -= floor(x * 0.1) * 10;
P(((int)x) % 10 + '0');
}
} else if (x >= 0)
*this, (long long)(x + 0.5);
else
*this, (long long)(x - 0.5);
;
return *this;
}
};
Fw<Cp> out;
} io;
long long n, s, a[MAXN << 1], b, c, d, tmp, x;
int main() {
io.in, n, s;
for (int i = (1), iend = (n); i <= iend; ++i) io.in, a[i];
sort(a + 1, a + 1 + n);
int mid = (n + 1) >> 1;
if (a[mid] == s) return puts("0"), 0;
long long ans = abs(s - a[mid]);
for (int i = 1; i < mid; ++i) {
if (a[i] > s) ans += a[i] - s;
}
for (int i = mid + 1; i <= n; ++i) {
if (a[i] < s) ans += s - a[i];
}
io.out, ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265359;
const double eps = 1e-9;
const int mod = 1e9 + 7;
const int mod1 = 1e9 + 9;
const int INF = 2e9;
const long long INFLL = 4e18;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, s;
cin >> n >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
long long ans = abs(a[n / 2] - s);
for (int i = n / 2 + 1; i < n; i++) ans += max(0, s - a[i]);
for (int i = 0; i < n / 2; i++) ans += max(0, a[i] - s);
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
long long n, s, a[maxn];
long long ans;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int med = a[n / 2];
if (med == s) {
cout << 0;
return 0;
}
for (int i = 0; i < n / 2; i++) {
if (a[i] > s) ans += a[i] - s;
if (a[n - i - 1] < s) ans += s - a[n - i - 1];
}
ans += abs(a[n / 2] - s);
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, s;
cin >> n >> s;
long long int a[n], c = 0;
for (long long int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int l = n / 2;
if (a[l] != s) {
for (long long int i = 0; i <= l; i++) {
if (a[i] > s) c += a[i] - s;
}
for (long long int i = l; i < n; i++) {
if (a[i] < s) c += s - a[i];
}
}
cout << c << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
void input() {
long long int n, second;
cin >> n >> second;
long long int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long int ans = 0, ans1 = 0;
if (a[n / 2] <= second) {
for (int i = n / 2; i < n; i++) {
if (a[i] > second) {
break;
}
ans += abs(second - a[i]);
}
cout << ans << "\n";
} else {
for (int i = 0; i <= n / 2; i++) {
if (a[i] < second) {
continue;
}
ans1 += abs(second - a[i]);
}
cout << ans1 << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
input();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
long long mod(long long n) {
return (n % 1000000007 + 1000000007) % 1000000007;
}
string test = "-------------------";
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, s;
cin >> n >> s;
vector<long long> vec(n);
for (long long i = 0; i < n; ++i) {
cin >> vec[i];
}
sort((vec).begin(), (vec).end());
long long mid = n / 2;
if (vec[mid] == s) {
cout << 0 << "\n";
return 0;
}
long long ans = 0;
if (vec[mid] < s) {
for (long long i = n / 2; i < n; ++i) {
if (vec[i] < s) {
ans += s - vec[i];
}
}
} else {
for (long long i = n / 2; i >= 0; i--) {
if (vec[i] > s) {
ans += vec[i] - s;
}
}
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int main() {
int n, s;
while (cin >> n >> s) {
long long num = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
if (a[(n + 1) / 2] == s)
cout << 0 << endl;
else if (a[(n + 1) / 2] < s) {
for (int i = (n + 1) / 2; i <= n; i++) {
if (a[i] >= s) break;
if (a[i] < s) num += s - a[i];
}
cout << num << endl;
} else if (a[(n + 1) / 2] > s) {
for (int i = 1; i <= (n + 1) / 2; i++) {
if (a[i] >= s) num += a[i] - s;
}
cout << num << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, s, in;
vector<int> a;
cin >> n >> s;
for (long long int i = 0; i < n; i++) {
cin >> in;
a.push_back(in);
}
sort(a.begin(), a.end());
long long int k = (n - 1) / 2;
long long int curmed = a[k];
long long int ans = 0;
if (s > curmed) {
for (long long int i = k; i < n; i++) {
if (a[i] < s) ans += (s - a[i]);
}
} else if (s < curmed) {
for (long long int i = k; i >= 0; i--) {
if (a[i] > s) ans += (a[i] - s);
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long ans = 0;
for (int i = 0; i <= n / 2; i++) {
if (a[i] > s) {
ans += a[i] - s;
}
}
for (int i = n / 2; i < n; i++) {
if (a[i] < s) {
ans += s - a[i];
}
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, i, s, z, z1, x, res, a[200007];
int main() {
cin >> n >> s;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + 1 + n);
if (a[n / 2 + 1] == s) {
cout << 0 << endl;
return 0;
} else if (a[n / 2 + 1] < s) {
for (i = n / 2 + 1; i <= n; i++) {
if (a[i] < s) {
res += s - a[i];
a[i] = s;
}
if (a[i] >= s) {
s = a[i];
}
}
} else {
for (i = n / 2 + 1; i >= 1; i--) {
if (a[i] > s) {
res += a[i] - s;
a[i] = s;
}
if (a[i] <= s) {
s = a[i];
}
}
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s;
cin >> n >> s;
vector<long long> nums(n);
for (auto &x : nums) {
cin >> x;
}
sort(nums.begin(), nums.end());
long long answ = 0;
if (nums[n / 2] > s) {
for (long long i = 0; i <= n / 2; i++) {
if (nums[i] > s) {
answ += nums[i] - s;
}
}
} else if (nums[n / 2] < s) {
for (long long i = n / 2; i < n; i++) {
if (nums[i] < s) {
answ += s - nums[i];
}
}
}
cout << answ;
cin >> n;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.