結果
問題 | No.1390 Get together |
ユーザー | leaf_1415 |
提出日時 | 2021-02-12 21:27:32 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 81 ms / 2,000 ms |
コード長 | 2,108 bytes |
コンパイル時間 | 817 ms |
コンパイル使用メモリ | 90,124 KB |
実行使用メモリ | 18,324 KB |
最終ジャッジ日時 | 2024-07-19 20:15:19 |
合計ジャッジ時間 | 3,451 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
10,880 KB |
testcase_01 | AC | 7 ms
10,880 KB |
testcase_02 | AC | 7 ms
11,008 KB |
testcase_03 | AC | 9 ms
11,136 KB |
testcase_04 | AC | 8 ms
11,136 KB |
testcase_05 | AC | 8 ms
11,136 KB |
testcase_06 | AC | 9 ms
11,136 KB |
testcase_07 | AC | 8 ms
11,136 KB |
testcase_08 | AC | 8 ms
11,008 KB |
testcase_09 | AC | 9 ms
11,264 KB |
testcase_10 | AC | 7 ms
10,880 KB |
testcase_11 | AC | 7 ms
10,880 KB |
testcase_12 | AC | 7 ms
11,008 KB |
testcase_13 | AC | 7 ms
11,008 KB |
testcase_14 | AC | 7 ms
10,880 KB |
testcase_15 | AC | 6 ms
11,008 KB |
testcase_16 | AC | 61 ms
16,208 KB |
testcase_17 | AC | 69 ms
18,300 KB |
testcase_18 | AC | 61 ms
16,172 KB |
testcase_19 | AC | 76 ms
18,304 KB |
testcase_20 | AC | 75 ms
18,296 KB |
testcase_21 | AC | 81 ms
18,324 KB |
testcase_22 | AC | 70 ms
17,296 KB |
testcase_23 | AC | 73 ms
17,292 KB |
testcase_24 | AC | 74 ms
17,296 KB |
testcase_25 | AC | 79 ms
18,304 KB |
testcase_26 | AC | 79 ms
18,304 KB |
testcase_27 | AC | 78 ms
18,288 KB |
testcase_28 | AC | 78 ms
18,236 KB |
testcase_29 | AC | 77 ms
18,232 KB |
testcase_30 | AC | 80 ms
18,280 KB |
testcase_31 | AC | 81 ms
18,252 KB |
ソースコード
#include <iostream>#include <cstdio>#include <cmath>#include <ctime>#include <cstdlib>#include <cassert>#include <vector>#include <list>#include <stack>#include <queue>#include <deque>#include <map>#include <set>#include <bitset>#include <string>#include <algorithm>#include <utility>#include <complex>#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)#define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)#define chmin(x, y) (x) = min((x), (y))#define chmax(x, y) (x) = max((x), (y))#define all(x) (x).begin(),(x).end()#define outl(x) cout << x << endl#define SP << " " <<#define inf 1e18using namespace std;typedef long long llint;typedef long long ll;typedef pair<llint, llint> P;struct UnionFind{int size;vector<int> parent;vector<int> rank;vector<int> v, e;UnionFind(){}UnionFind(int size){this->size = size;parent.resize(size+1);rank.resize(size+1);v.resize(size+1);e.resize(size+1);init();}void init(){for(int i = 0; i <= size; i++){parent[i] = i, rank[i] = 0;v[i] = 1, e[i] = 0;}}int root(int i){if(parent[i] == i) return i;return parent[i] = root(parent[i]);}bool same(int i, int j){return root(i) == root(j);}void merge(int i, int j){ // j will become new rootparent[i] = j;v[j] += v[i];e[j] += e[i] + 1;}void unite(int i, int j){int root_i = root(i), root_j = root(j);if(root_i == root_j){e[root_i]++;return;}if(rank[root_i] < rank[root_j]) merge(root_i, root_j);else merge(root_j, root_i);if(rank[root_i] == rank[root_j]) rank[root_i]++;}};ll n, m;ll b[200005], c[200005];vector<ll> vec[200005];UnionFind uf(200005);int main(void){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> m;rep(i, 1, n) cin >> b[i] >> c[i];rep(i, 1, n) vec[c[i]].push_back(b[i]);ll ans = 0;rep(i, 1, n){sort(all(vec[i]));if(vec[i].size() <= 1) continue;rep(j, 1, (int)vec[i].size()-1){ll u = vec[i][j-1], v = vec[i][j];if(!uf.same(u, v)) ans++, uf.unite(u, v);}}cout << ans << endl;return 0;}