結果
| 問題 |
No.1390 Get together
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-13 09:14:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 59 ms / 2,000 ms |
| コード長 | 1,325 bytes |
| コンパイル時間 | 2,223 ms |
| コンパイル使用メモリ | 206,376 KB |
| 最終ジャッジ日時 | 2025-01-18 20:04:45 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
/**
* author: shu8Cream
* created: 12.02.2021 21:19:22
**/
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
struct UnionFind{
vector<int> par;
vector<map<int, int>> mp;
UnionFind(int n=0): par(n, -1), mp(n) {}
int find(int x){
if(par[x] < 0) return x;
return par[x] = find(par[x]);
}
bool unite(int x, int y){
x = find(x); y = find(y);
if(x == y) return false;
if(par[x] > par[y]) swap(x,y); //マージテク
for(auto p : mp[y]){
mp[x][p.first] += p.second;
}
mp[y] = map<int,int>(); //メモリの解放
par[x] += par[y];
par[y] = x;
return true;
}
bool same(int x, int y) {return find(x) == find(y);}
int size(int x) {return -par[find(x)];}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,m;
cin >> n >> m;
UnionFind uf(m);
vi f(n,-1);
int ans = 0;
rep(i,n){
int b,c;
cin >> b >> c;
--b; --c;
if(f[c]>=0){
if(!uf.same(b,f[c])) ans++, uf.unite(b,f[c]);
}else f[c]=b;
}
cout << ans << endl;
}