結果

問題 No.1390 Get together
ユーザー sten_san
提出日時 2021-02-12 21:52:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 2,006 ms
コンパイル使用メモリ 199,332 KB
最終ジャッジ日時 2025-01-18 18:24:07
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
using namespace std;
struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
return vec(Element(), arg, args...);
}
int par[200000 + 1];
void init() {
fill(begin(par), end(par), -1);
}
int root(int x) {
if (par[x] < 0) {
return x;
}
return par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return;
par[x] += par[y];
par[y] = x;
}
bool same(int x, int y) {
return root(x) == root(y);
}
int size(int x) {
return -par[root(x)];
}
int main() {
init();
int n, m; cin >> n >> m;
auto a = vec<int>(uns, n, 0);
for (int i = 0; i < n; ++i) {
int b, c; cin >> b >> c; --b; --c;
a[c].push_back(b);
}
for (auto &b : a) {
for (int i = 1; i < b.size(); ++i) {
unite(b[i - 1], b[i]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
if (same(i, m)) continue;
ans += size(i) - 1;
unite(i, m);
}
cout << ans << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0