結果
問題 | No.1390 Get together |
ユーザー |
|
提出日時 | 2021-02-12 22:23:45 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 417 ms / 2,000 ms |
コード長 | 3,526 bytes |
コンパイル時間 | 2,032 ms |
コンパイル使用メモリ | 187,836 KB |
実行使用メモリ | 28,288 KB |
最終ジャッジ日時 | 2024-07-19 22:49:47 |
合計ジャッジ時間 | 9,318 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h>using namespace std;// 型定義typedef long long ll;typedef pair<ll, ll> P;// forループ#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)// 定数宣言const int INF = 1e9;const int MOD = 1e9+7;const ll LINF = 1e18;// グラフ表現using Graph = vector<vector<int>>;// グラフの辺表現using Edge = map<pair<int,int>,int>;// n次元配列の初期化。第2引数の型のサイズごとに初期化していく。template<typename A, size_t N, typename T>void Fill(A (&array)[N], const T &val){std::fill( (T*)array, (T*)(array+N), val );}// 最大公約数ll gcd(ll a,ll b){if (a%b == 0) return(b);else return(gcd(b, a%b));}// 最小公倍数ll lcm(ll a, ll b){return a/gcd(a, b) * b;}struct dsu {public:dsu() : _n(0) {}dsu(int n) : _n(n), parent_or_size(n, -1) {}int merge(int a, int b) {assert(0 <= a && a < _n);assert(0 <= b && b < _n);int x = leader(a), y = leader(b);if (x == y) return x;if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);parent_or_size[x] += parent_or_size[y];parent_or_size[y] = x;return x;}bool same(int a, int b) {assert(0 <= a && a < _n);assert(0 <= b && b < _n);return leader(a) == leader(b);}int leader(int a) {assert(0 <= a && a < _n);if (parent_or_size[a] < 0) return a;return parent_or_size[a] = leader(parent_or_size[a]);}int size(int a) {assert(0 <= a && a < _n);return -parent_or_size[leader(a)];}std::vector<std::vector<int>> groups() {std::vector<int> leader_buf(_n), group_size(_n);for (int i = 0; i < _n; i++) {leader_buf[i] = leader(i);group_size[leader_buf[i]]++;}std::vector<std::vector<int>> result(_n);for (int i = 0; i < _n; i++) {result[i].reserve(group_size[i]);}for (int i = 0; i < _n; i++) {result[leader_buf[i]].push_back(i);}result.erase(std::remove_if(result.begin(), result.end(),[&](const std::vector<int>& v) { return v.empty(); }),result.end());return result;}private:int _n;// root node: -1 * component size// otherwise: parentstd::vector<int> parent_or_size;};int main(){cout << fixed << setprecision(15);ll N, M;cin >> N >> M;// M個の箱dsu d(M);// その色がどこの箱につながっているかmap<ll,ll> Color;set<ll> BOX;REP(i, N){ll b, c;cin >> b >> c;b--;c--;// 登場した箱の数を数えるBOX.insert(b);// その色が既に現れているときif(Color.count(c)){if(Color[c] == b) continue;// 今の箱と、その色が既に入っている箱をマージd.merge(b, Color[c]);}else{// その色が入っている箱にbを記録Color[c] = b;}}ll usedBox = BOX.size();ll Nouse = M - usedBox;// cout << usedBox << endl;vector<vector<int>> V = d.groups();// for(auto v: V){// cout << "point" << endl;// for(auto i: v){// cout << i << endl;// }// }ll indepent = V.size();cout << M - indepent << endl;return 0;}