Submission #5018658
Source Code Expand
#include <bits/stdc++.h> #define pb push_back #define fi first #define se second using std::cin; using std::cout; using LL = long long; using pii = std::pair<int, int>; using pip = std::pair<int, pii>; const int MAXN = 2e5 + 10; int f[MAXN]; std::vector<pii> G[MAXN]; int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } bool unite(int x, int y) { int fx = find(x), fy = find(y); if (fx == fy)return false; f[fx] = fy; return true; } struct hh { size_t operator()(const pii &s) const { size_t out = 14695981039346656037ULL; static constexpr size_t mul = 1099511628211ULL; out = (out ^ s.first) * mul; out = (out ^ s.second) * mul; return out; } }; void solve() { int n, m; cin >> n >> m; std::priority_queue<pip, std::vector<pip>, std::greater<>> q; for (int i = 1; i <= n; ++i) f[i] = i; std::vector<pip> V(m); for (int i = 0, A, B, C; i < m; ++i) { cin >> A >> B >> C; V[i] = {C, {A, B}}; } std::unordered_set<pii, hh> S; std::sort(begin(V), end(V)); int C = V[0].fi; for (auto & i : V) { if (i.fi - C > n + n) break; G[i.fi - C].pb(i.se); S.insert(i.se); } int cnt = 0; LL ans = 0; for (int i = 0; cnt != n - 1; ++i) { pii p; for (auto it : G[i]) { if (unite(it.fi, it.se)) ans += i + C, cnt++; // cout << it.fi << it.se << std::endl; p = {it.se, (it.fi + 1) % n}; if (S.find(p) != end(S)) continue; G[i + 1].pb(p); S.insert(p); } } cout << ans << "\n"; // while (cnt != n - 1) { // pip p = q.top(); // q.pop(); // int c = p.fi, a = p.se.fi, b = p.se.se; // // cout << a << " " << b <<"\n"; // if (S.find({a, b}) != end(S)) continue; // S.insert({a, b}); // if (unite(a, b)) ans += c, cnt++; // q.push({c + 1, {b, (a + 1) % n}}); // } // cout << ans << "\n"; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); #ifdef trote freopen("../1.txt", "r", stdin); freopen("../out.txt", "w", stdout); #endif solve(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | vjudge5 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 2209 Byte |
Status | RE |
Exec Time | 2115 ms |
Memory | 282060 KB |
Judge Result
Set Name | sample | all | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 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 | 3 ms | 4992 KB |
01-02.txt | AC | 62 ms | 7296 KB |
01-03.txt | RE | 167 ms | 8064 KB |
01-04.txt | RE | 149 ms | 22600 KB |
01-05.txt | RE | 151 ms | 21704 KB |
01-06.txt | RE | 100 ms | 5760 KB |
01-07.txt | RE | 99 ms | 5760 KB |
01-08.txt | RE | 100 ms | 5760 KB |
01-09.txt | RE | 103 ms | 6272 KB |
01-10.txt | RE | 144 ms | 10380 KB |
01-11.txt | AC | 433 ms | 55312 KB |
01-12.txt | RE | 187 ms | 14756 KB |
01-13.txt | RE | 190 ms | 14628 KB |
01-14.txt | RE | 189 ms | 14756 KB |
01-15.txt | RE | 189 ms | 14628 KB |
01-16.txt | RE | 187 ms | 14628 KB |
01-17.txt | RE | 191 ms | 14756 KB |
01-18.txt | RE | 214 ms | 24176 KB |
01-19.txt | AC | 215 ms | 42740 KB |
01-20.txt | AC | 585 ms | 80080 KB |
01-21.txt | AC | 1431 ms | 141132 KB |
01-22.txt | AC | 1540 ms | 143812 KB |
01-23.txt | AC | 1536 ms | 144196 KB |
01-24.txt | RE | 150 ms | 20424 KB |
01-25.txt | RE | 172 ms | 15268 KB |
01-26.txt | AC | 125 ms | 28532 KB |
01-27.txt | TLE | 2115 ms | 281048 KB |
01-28.txt | TLE | 2115 ms | 280660 KB |
01-29.txt | TLE | 2114 ms | 282060 KB |
01-30.txt | TLE | 2114 ms | 281932 KB |
sample-01.txt | AC | 3 ms | 4992 KB |
sample-02.txt | AC | 3 ms | 4992 KB |
sample-03.txt | AC | 3 ms | 4992 KB |