結果
問題 | No.860 買い物 |
ユーザー | Y17 |
提出日時 | 2019-08-10 15:37:02 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 137 ms / 1,000 ms |
コード長 | 1,051 bytes |
コンパイル時間 | 2,032 ms |
コンパイル使用メモリ | 153,092 KB |
実行使用メモリ | 10,812 KB |
最終ジャッジ日時 | 2023-09-26 23:23:01 |
合計ジャッジ時間 | 4,018 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,376 KB |
testcase_01 | AC | 2 ms
4,376 KB |
testcase_02 | AC | 2 ms
4,376 KB |
testcase_03 | AC | 2 ms
4,380 KB |
testcase_04 | AC | 1 ms
4,376 KB |
testcase_05 | AC | 2 ms
4,376 KB |
testcase_06 | AC | 6 ms
4,380 KB |
testcase_07 | AC | 137 ms
10,036 KB |
testcase_08 | AC | 136 ms
10,252 KB |
testcase_09 | AC | 136 ms
10,812 KB |
testcase_10 | AC | 136 ms
9,696 KB |
testcase_11 | AC | 90 ms
9,500 KB |
testcase_12 | AC | 88 ms
9,340 KB |
testcase_13 | AC | 125 ms
9,628 KB |
testcase_14 | AC | 129 ms
10,616 KB |
testcase_15 | AC | 74 ms
9,548 KB |
testcase_16 | AC | 116 ms
10,372 KB |
testcase_17 | AC | 117 ms
10,064 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long struct UnionFind{ vector<int> tree; UnionFind(int n): tree(n+1, -1) {}; int root(int i){ return tree[i] < 0 ? i : tree[i] = root(tree[i]); } bool unite(int i, int j){ i = root(i), j = root(j); if(i == j) return false; tree[i] += tree[j], tree[j] = i; return true; } bool same(int i, int j){ return root(i) == root(j); } int size(int i){ return -tree[root(i)]; } }; signed main(){ int n; cin >> n; int ans = 0; int c, d; typedef pair<int, pair<int, int>> P; priority_queue<P, vector<P>, greater<P>> que; for(int i = 0;i < n;i++){ cin >> c >> d; ans += c; que.push({c, {0, i+1}}); if(i != 0) que.push({d, {i, i+1}}); } UnionFind uf(n); while(!que.empty()){ if(uf.unite(que.top().second.first, que.top().second.second)){ ans += que.top().first; } que.pop(); } cout << ans << endl; return 0; }