結果
問題 | No.1390 Get together |
ユーザー |
![]() |
提出日時 | 2021-05-12 17:37:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 219 ms / 2,000 ms |
コード長 | 1,852 bytes |
コンパイル時間 | 1,217 ms |
コンパイル使用メモリ | 121,896 KB |
実行使用メモリ | 24,332 KB |
最終ジャッジ日時 | 2024-09-23 01:23:28 |
合計ジャッジ時間 | 6,866 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <iostream>#include<vector>#include<string>#include<algorithm>#include<iomanip>#include<map>#include<random>#include<complex>#include<math.h>#include<functional>#include<stack>#include<queue>#include<unordered_map>#include<set>#include<numeric>#include <cassert>using namespace std;#define rep(i, n) for (int i = 0; i < (ll)(n); i++)#define req(i,n) for(int i = 1;i <= n; i++)#define rrep(i,n) for(ll i = n-1;i >= 0;i--)#define ALL(obj) begin(obj), end(obj)#define RALL(a) rbegin(a),rend(a)typedef long long ll;typedef long double ld;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; }const int inf = 0x3fffffff;const ll INF = 1e18;struct UnionFind {vector<int> par;UnionFind(int n) : par(n, -1) { }void init(int n) { par.assign(n, -1); }int root(int x) {if (par[x] < 0) return x;else return par[x] = root(par[x]);}bool same(int x, int y) { return root(x) == root(y); }bool unite(int x, int y) {x = root(x); y = root(y);if (x == y) return false;if (par[x] > par[y]) swap(x, y); // merge techniquepar[x] += par[y]; par[y] = x;return 1;}int size(int x) { return -par[root(x)]; }};int main() {int n, m; cin >> n >> m; vector<int> b(n), c(n);UnionFind uf(n + m); ll sum = 0; vector<vector<int>> G(n);rep(i, n) {cin >> b[i] >> c[i], b[i]--, c[i]--;G[c[i]].push_back(b[i]);}rep(i, n) {rep(j, G[i].size() - 1) {int now = G[i][j], nx = G[i][j + 1];if (now != nx) uf.unite(now, nx);}}set<int> st;rep(i, m) st.insert(uf.root(i));for (int i : st) sum += uf.size(i) - 1;cout << sum << endl;}