Submission #2136331
Source Code Expand
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int INF = 1e9 + 1e8; typedef pair<int, int> ii; int n, q; int best[N]; int par[N]; int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); } void join(int p, int q) { par[anc(p)] = anc(q); } struct edge { int w; int u; int v; bool operator < (const edge &rhs) { return w < rhs.w; } }; vector<edge> ed; long long res; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (int i = 0; i < n; ++i) best[i] = INF, par[i] = i; while(q--) { int u, v, w; cin >> u >> v >> w; ed.push_back({w, u, v}); best[u] = min(best[u], w + 1); best[v] = min(best[v], w + 2); } priority_queue < ii, vector<ii>, greater<ii> > pq; for (int i = 0; i < n; ++i) if (best[i] != INF) { pq.push(make_pair(best[i], i)); } while(!pq.empty()) { ii top = pq.top(); pq.pop(); int u = top.second, best_u = top.first; if (best[u] != best_u) continue; if (best[(u + 1) % n] > best[u] + 2) { pq.push(ii(best[(u + 1) % n] = best[u] + 2, (u + 1) % n)); } } for (int i = 0; i < n; ++i) { ed.push_back({best[i], i, (i + 1) % n}); } sort(ed.begin(), ed.end()); for (auto &e : ed) { if (anc(e.u) != anc(e.v)) { join(e.u, e.v); res += e.w; } } cout << res << endl; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | cheater2k |
Language | C++14 (GCC 5.4.1) |
Score | 1300 |
Code Size | 1363 Byte |
Status | AC |
Exec Time | 155 ms |
Memory | 13804 KB |
Judge Result
Set Name | sample | all | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1300 / 1300 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
sample | sample-01.txt, sample-02.txt, sample-03.txt |
all | sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, sample-01.txt, sample-02.txt, sample-03.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01-01.txt | AC | 1 ms | 256 KB |
01-02.txt | AC | 64 ms | 4596 KB |
01-03.txt | AC | 138 ms | 12144 KB |
01-04.txt | AC | 26 ms | 6640 KB |
01-05.txt | AC | 19 ms | 6388 KB |
01-06.txt | AC | 24 ms | 5876 KB |
01-07.txt | AC | 24 ms | 4984 KB |
01-08.txt | AC | 30 ms | 5644 KB |
01-09.txt | AC | 47 ms | 6256 KB |
01-10.txt | AC | 106 ms | 10352 KB |
01-11.txt | AC | 112 ms | 10480 KB |
01-12.txt | AC | 154 ms | 12400 KB |
01-13.txt | AC | 154 ms | 12528 KB |
01-14.txt | AC | 153 ms | 12400 KB |
01-15.txt | AC | 155 ms | 13040 KB |
01-16.txt | AC | 154 ms | 12656 KB |
01-17.txt | AC | 154 ms | 11888 KB |
01-18.txt | AC | 77 ms | 10348 KB |
01-19.txt | AC | 21 ms | 4980 KB |
01-20.txt | AC | 40 ms | 5668 KB |
01-21.txt | AC | 67 ms | 6128 KB |
01-22.txt | AC | 108 ms | 12144 KB |
01-23.txt | AC | 108 ms | 12272 KB |
01-24.txt | AC | 22 ms | 7664 KB |
01-25.txt | AC | 113 ms | 13804 KB |
01-26.txt | AC | 27 ms | 6896 KB |
01-27.txt | AC | 39 ms | 6384 KB |
01-28.txt | AC | 82 ms | 10348 KB |
01-29.txt | AC | 90 ms | 9968 KB |
01-30.txt | AC | 110 ms | 11376 KB |
sample-01.txt | AC | 1 ms | 256 KB |
sample-02.txt | AC | 1 ms | 256 KB |
sample-03.txt | AC | 1 ms | 256 KB |