제출 #1076769

#제출 시각아이디문제언어결과실행 시간메모리
1076769zsombor수천개의 섬 (IOI22_islands)C++17
55 / 100
45 ms12892 KiB
#include "islands.h" #include <bits/stdc++.h> #include <variant> using namespace std; int n, m, X = -1, Y = -1, I = -1; vector<int> u; vector<int> v; vector<vector<int> > g(2e5); vector<int> e(2e5, -1); vector<int> oda; vector<int> kor; vector<bool> be(2e5, false); vector<bool> ki(2e5, false); vector<int> ans; void solve1() { vector<int> a; vector<int> b; for (int i = 0; i < m; i++) { if (!u[i]) a.push_back(i); else b.push_back(i); } if (a.size() < 2 || b.size() < 1) return; ans = {a[0], b[0], a[1], a[0], b[0], a[1]}; } void solve2() { ans.resize(12); for (int i = 0; i < m; i++) { if (u[i] == 0 && v[i] == 1) ans[0] = ans[8] = i; if (u[i] == 1 && v[i] == 0) ans[5] = ans[9] = i; if (u[i] == 0 && v[i] == 2) ans[3] = ans[11] = i; if (u[i] == 2 && v[i] == 0) ans[2] = ans[6] = i; if (u[i] == 1 && v[i] == 2) ans[1] = ans[7] = i; if (u[i] == 2 && v[i] == 1) ans[4] = ans[10] = i; } } void dfs3(int x) { if (I == -1 && g[x].size() >= 3) I = x; for (int i: g[x]) { if (i == (e[x] ^ 1)) continue; int y = v[i]; if (e[y] > -1) continue; e[y] = i; dfs3(y); } } void solve3() { for (int i = 0; i < m; i++) g[u[i]].push_back(i); e[0] = m; dfs3(0); if (g[0].size() >= 2) I = 0; if (I == -1) return; X = I; while (X) { oda.push_back(e[X]); X = u[e[X]]; } vector<int> z; for (int i: g[I]) if (i != (e[I] ^ 1)) z.push_back(i); kor = {z[0], z[0] ^ 1, z[1], z[1] ^ 1, z[0] ^ 1, z[0], z[1] ^ 1, z[1]}; reverse(oda.begin(), oda.end()); for (int i: oda) ans.push_back(i); for (int i: kor) ans.push_back(i); reverse(oda.begin(), oda.end()); for (int i: oda) ans.push_back(i); } void dfs4(int x) { be[x] = true; for (int i: g[x]) { int y = v[i]; if (be[y] && !ki[y]) { X = x; Y = y; I = i; } if (be[y]) continue; e[y] = i; dfs4(y); } ki[x] = true; } void solve4() { for (int i = 0; i < m; i += 2) g[u[i]].push_back(i); e[0] = m; dfs4(0); if (I == -1) return; kor.push_back(I); while (X != Y) { kor.push_back(e[X]); X = u[e[X]]; } while (X) { oda.push_back(e[X]); X = u[e[X]]; } reverse(oda.begin(), oda.end()); for (int i: oda) ans.push_back(i); reverse(kor.begin(), kor.end()); for (int i: kor) ans.push_back(i); for (int i: kor) ans.push_back(i ^ 1); reverse(kor.begin(), kor.end()); for (int i: kor) ans.push_back(i); for (int i: kor) ans.push_back(i ^ 1); reverse(oda.begin(), oda.end()); for (int i: oda) ans.push_back(i); } variant<bool, vector<int> > find_journey(int N, int M, vector<int> U, vector<int> V) { n = N; m = M; u = U; v = V; bool st3 = true, st4 = true; if (m % 2) st3 = st4 = false; for (int i = 0; i < m - 1; i += 2) { if (u[i] != v[i + 1]) st3 = false; if (v[i] != u[i + 1]) st3 = false; if (u[i] != u[i + 1]) st4 = false; if (v[i] != v[i + 1]) st4 = false; } if (n == 2) solve1(); else if (st3) solve3(); else if (st4) solve4(); else solve2(); if (ans.size()) return ans; return false; }
#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...