Submission #57509
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include "bits/stdc++.h" using namespace std; struct UnionFind { // {{{ vector< int > u; explicit UnionFind( int n): u(n+5, -1) {} int size( int x) { return -u[root(x)]; } int root( int x) { return (u[x] < 0)? x : (u[x] = root(u[x])); } bool same( int x, int y) { return (root(x) == root(y)); } bool unite( int x, int y){ return (x=root(x)) == (y=root(y))? false : (u[x]+=u[y], u[y]=x, true ); } }; // }}} int main() { cin.tie(nullptr); ios_base::sync_with_stdio( false ); int N, M, K; cin >> N >> M >> K; UnionFind uf(N + 5); vector< int > a(N); unordered_map< int , int > id; vector<priority_queue< int >> kthFinder(N + 5); for ( int i = 0; i < N; ++i) { cin >> a[i]; id[a[i]] = i; kthFinder[i].push(a[i]); } const auto merge = [&]( int x, int y) { x = uf.root(x); y = uf.root(y); int parentID, childID; if (kthFinder[x].size() > kthFinder[y].size()) { parentID = x; childID = y; } else { parentID = y; childID = x; } uf.unite(parentID, childID); auto &parent = kthFinder[parentID]; auto &child = kthFinder[childID]; while (not child.empty()) { const int v = child.top(); child.pop(); if (parent.size() < K) { parent.push(v); } else if (v < parent.top()) { parent.pop(); parent.push(v); } } return ; }; const auto findKth = [&]( int x) -> int { x = uf.root(x); if (kthFinder[x].size() < K) return -1; else return kthFinder[x].top(); }; for ( int _ = 0; _ < M; ++_) { int com, x, y; cin >> com; if (com == 1) { cin >> x >> y; const int id_x = id[x]; const int id_y = id[y]; if (not uf.same(id_x, id_y)) { merge(id_x, id_y); } } else { cin >> x; cout << findKth(id[x]) << "\n" ; } } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 1232 - Corner Kth ⇔ Corner Case |
ユーザー名 | Arumakan_ei1727 |
投稿日時 | 2019-12-17 23:41:16 |
言語 | C++14 |
状態 | Accepted |
得点 | 100 |
ソースコード長 | 2245 Byte |
最大実行時間 | 89 ms |
最大メモリ使用量 | 13284 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 100 / 100 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
corner_in1.txt | AC | 56 ms | 11648 KB |
1
|
corner_in2.txt | AC | 58 ms | 11692 KB |
1
|
corner_in3.txt | AC | 56 ms | 11712 KB |
1
|
corner_in4.txt | AC | 53 ms | 11748 KB |
1
|
corner_in5.txt | AC | 54 ms | 11664 KB |
1
|
large_in1.txt | AC | 82 ms | 12220 KB |
1
|
large_in2.txt | AC | 84 ms | 12368 KB |
1
|
large_in3.txt | AC | 89 ms | 12524 KB |
1
|
large_in4.txt | AC | 88 ms | 12820 KB |
1
|
large_in5.txt | AC | 89 ms | 12788 KB |
1
|
large_in6.txt | AC | 88 ms | 13284 KB |
1
|
sample_in1.txt | AC | 18 ms | 1832 KB |
1
|
sample_in2.txt | AC | 19 ms | 1788 KB |
1
|
small_in1.txt | AC | 25 ms | 1872 KB |
1
|
small_in2.txt | AC | 21 ms | 1952 KB |
1
|
small_in3.txt | AC | 20 ms | 1868 KB |
1
|
small_in4.txt | AC | 22 ms | 1912 KB |
1
|
small_in5.txt | AC | 17 ms | 1828 KB |
1
|
small_in6.txt | AC | 21 ms | 1872 KB |
1
|
small_in7.txt | AC | 17 ms | 1792 KB |
1
|
small_in8.txt | AC | 17 ms | 1836 KB |
1
|
small_in9.txt | AC | 21 ms | 1880 KB |
1
|
small_in10.txt | AC | 23 ms | 1916 KB |
1
|