結果

問題 No.1390 Get together
ユーザー kappybar
提出日時 2021-02-12 21:54:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 2,282 bytes
コンパイル時間 1,770 ms
コンパイル使用メモリ 177,892 KB
実行使用メモリ 48,384 KB
最終ジャッジ日時 2024-07-19 21:11:00
合計ジャッジ時間 7,447 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;
struct Unionfind{
vector<int> parents;
int n;
Unionfind(int n):n(n),parents(n,-1){ }
int find(int x){
if(parents[x] < 0) return x;
else return parents[x] = find(parents[x]);
}
void unite(int x,int y){
x = find(x);
y = find(y);
if(x==y) return;
if(parents[x] > parents[y]) swap(x,y);
parents[x] += parents[y];
parents[y] = x;
}
int size(int x){
return -parents[find(x)];
}
bool same(int x,int y){
return find(x)==find(y);
}
vector<int> members(int x){
int root = find(x);
vector<int> member;
for(int i=0;i<n;i++){
if(find(i)==root ){
member.push_back(i);
}
}
return member;
}
int group_cnt(){
int c = 0;
rep(i,n){
if(parents[i] < 0) ++c;
}
return c;
}
};
int main(){
int n,m;
cin >> n >> m;
vector<int> b(n),c(n);
rep(i,n) cin >> b[i] >> c[i];
rep(i,n) --b[i], --c[i];
vector<set<int>> in(m);
rep(i,n) in[b[i]].insert(c[i]);
Unionfind uf(n);
rep(i,m){
int before = -1;
for(auto s:in[i]){
if(before == -1){
before = s;
continue;
}
uf.unite(before,s);
before = s;
}
}
vector<set<int>> col(n);
rep(i,n) col[c[i]].insert(b[i]);
for(int color = 0; color < n; color ++){
if(uf.find(color) == color) continue;
int par = uf.find(color);
for(auto s:col[color]){
col[par].insert(s);
}
}
int ans = 0;
for(int color = 0; color < n; color ++){
if(uf.find(color) != color) continue;
if((int)col[color].size() == 0) continue;
ans += (int)col[color].size() - 1;
}
cout << ans << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0