Submission #1134040

#TimeUsernameProblemLanguageResultExecution timeMemory
1134040alterioFountain Parks (IOI21_parks)C++20
15 / 100
95 ms20904 KiB
#include <bits/stdc++.h> #include "parks.h" using namespace std; #define all(x) (x).begin(), (x).end() const int mxn = 2e5 + 100; int ldr[mxn], rnk[mxn]; int Find(int x) { if (ldr[x] == x) return x; return ldr[x] = Find(ldr[x]); } void Union(int x, int y) { x = Find(x), y = Find(y); if (x == y) return; if (rnk[y] > rnk[x]) swap(x, y); rnk[x] += rnk[y]; ldr[y] = x; } int construct_roads(vector<int> x, vector<int> y) { int n = x.size(); if (x.size() == 1) { build({}, {}, {}, {}); return 1; } for (int i = 0; i < mxn; i++) ldr[i] = i, rnk[i] = 1; vector<int> u, v, a, b; vector<pair<int, int>> V1, V2; for (int i = 0; i < n; i++) { if (x[i] == 2) V1.push_back({y[i], i}); else V2.push_back({y[i], i}); } sort(all(V1)); sort(all(V2)); for (int i = 0; i < n; i++) { if (i + 1 < V1.size() && V1[i + 1].first - V1[i].first == 2) { Union(V1[i].second, V1[i + 1].second); u.push_back(V1[i].second); v.push_back(V1[i + 1].second); a.push_back(1); b.push_back(V1[i].first + 1); } if (i + 1 < V2.size() && V2[i + 1].first - V2[i].first == 2) { Union(V2[i].second, V2[i + 1].second); u.push_back(V2[i].second); v.push_back(V2[i + 1].second); a.push_back(5); b.push_back(V2[i].first + 1); } } int l = 0, r = 0; while (l < V1.size() && r < V2.size()) { if (V1[l].first < V2[r].first) { l++; continue; } if (V2[r].first < V1[l].first) { r++; continue; } Union(V1[l].second, V2[r].second); u.push_back(V1[l].second); v.push_back(V2[r].second); a.push_back(3); b.push_back(V1[l].first + 1); l++; r++; } set<int> s; for (int i = 0; i < n; i++) s.insert(Find(i)); if (s.size() != 1) return 0; build(u, v, a, b); return 1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...