Submission #66702
ソースコード
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 91 92 | #include <bits/extc++.h> using namespace std; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio( false ); int n, m, q, k; cin >> n >> m >> q >> k; vector< int > c(n); vector<vector<pair< int , int > > > graph(n); for ( int i = 0; i < n; ++i){ cin >> c[i]; --c[i]; } for ( int i = 0; i < m; ++i){ int u, v, l; cin >> u >> v >> l; --u, --v; graph[u].emplace_back(v, l); graph[v].emplace_back(u, l); } vector< long long > dist(n, LLONG_MAX >> 1); priority_queue<pair< long long , int >, vector<pair< long long , int > >, greater<> > pq; dist[0] = 0; pq.emplace(0, 0); while (!pq.empty()){ auto [cur_cost, cur_node] = pq.top(); pq.pop(); if (dist[cur_node] < cur_cost) continue ; for (auto [nxt_node, l] : graph[cur_node]){ if (dist[cur_node] + l < dist[nxt_node]){ dist[nxt_node] = dist[cur_node] + l; pq.emplace(dist[nxt_node], nxt_node); } } } int idx = 0; __gnu_pbds::tree<pair< long long , int >, __gnu_pbds::null_type, less<pair< long long , int > >, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> s; vector<map< long long , int > > cnt(1e5); for ( int i = 0; i < n; ++i){ cnt[c[i]][dist[i]]++; } for ( int i = 0; i < 1e5; ++i){ if (!cnt[i].empty()){ s.insert({cnt[i].begin()->first, idx++}); } } for ( int i = 0; i < q; ++i){ int x, nc; cin >> x >> nc; --x, --nc; if (!cnt[c[x]].empty()){ s.erase(s.lower_bound({cnt[c[x]].begin()->first, 0})); } if (--cnt[c[x]][dist[x]] == 0){ cnt[c[x]].erase(dist[x]); } if (!cnt[c[x]].empty()){ s.insert({cnt[c[x]].begin()->first, idx++}); } c[x] = nc; if (!cnt[c[x]].empty()){ s.erase(s.lower_bound({cnt[c[x]].begin()->first, 0})); } cnt[c[x]][dist[x]]++; if (!cnt[c[x]].empty()){ s.insert({cnt[c[x]].begin()->first, idx++}); } if (s.size() < k){ cout << -1 << '\n' ; } else { cout << s.find_by_order(k - 1)->first << '\n' ; } } return (0); } |
ステータス
項目 | データ |
---|---|
問題 | 1512 - Colorful Graph |
ユーザー名 | ei1903 |
投稿日時 | 2021-05-15 08:09:12 |
言語 | C++17 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 2408 Byte |
最大実行時間 | 320 ms |
最大メモリ使用量 | 35620 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01.txt | AC | 153 ms | 17624 KB |
1
|
in02.txt | AC | 71 ms | 8516 KB |
1
|
in03.txt | AC | 112 ms | 11232 KB |
1
|
in04.txt | AC | 159 ms | 13616 KB |
1
|
in05.txt | AC | 92 ms | 9484 KB |
1
|
in06.txt | AC | 296 ms | 26240 KB |
1
|
in07.txt | AC | 320 ms | 27184 KB |
1
|
in08.txt | AC | 293 ms | 28368 KB |
1
|
in09.txt | AC | 272 ms | 28780 KB |
1
|
in10.txt | AC | 314 ms | 29048 KB |
1
|
in11.txt | AC | 207 ms | 30756 KB |
1
|
in12.txt | AC | 199 ms | 31060 KB |
1
|
in13.txt | AC | 237 ms | 33784 KB |
1
|
in14.txt | AC | 138 ms | 18680 KB |
1
|
in15.txt | AC | 141 ms | 24904 KB |
1
|
in16.txt | AC | 191 ms | 35620 KB |
1
|
in17.txt | AC | 306 ms | 34196 KB |
1
|
in18.txt | AC | 276 ms | 33656 KB |
1
|
in19.txt | AC | 316 ms | 34676 KB |
1
|
in20.txt | AC | 82 ms | 18552 KB |
1
|
in21.txt | AC | 84 ms | 20076 KB |
1
|
in22.txt | AC | 85 ms | 21108 KB |
1
|
in23.txt | AC | 69 ms | 20724 KB |
1
|
in24.txt | AC | 74 ms | 21236 KB |
1
|
in25.txt | AC | 73 ms | 23428 KB |
1
|
in26.txt | AC | 93 ms | 24236 KB |
1
|
in27.txt | AC | 52 ms | 22908 KB |
1
|
in28.txt | AC | 50 ms | 23160 KB |
1
|
sample01.txt | AC | 22 ms | 23032 KB |
1
|
sample02.txt | AC | 18 ms | 23028 KB |
1
|