Submission #632020

#TimeUsernameProblemLanguageResultExecution timeMemory
632020CyanForcesThousands Islands (IOI22_islands)C++17
100 / 100
317 ms36276 KiB
#include "islands.h" #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug(...) //ignore typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef long double ld; variant<bool, vi> find_journey(int n, int m, vi u, vi v) { vi dead(n); vector<set<int>> rg(n), g(n); rep(i,0,m) { g[u[i]].emplace(i); rg[v[i]].emplace(i); } queue<int> q; rep(x,0,n) if(sz(g[x]) == 0) q.emplace(x); auto del = [&](int e) { g[u[e]].erase(e); rg[v[e]].erase(e); if(sz(g[u[e]]) == 0) q.emplace(u[e]); }; auto trim = [&]() { while(sz(q)) { int x = q.front(); q.pop(); dead[x] = true; vi es(all(rg[x])); for(int e : es) del(e); } }; trim(); if(dead[0]) return false; vi pth0; int x = 0; int lst_e = -1; while(sz(g[x]) == 1) { assert(!dead[x]); int e = *g[x].begin(); pth0.emplace_back(e); vi es(all(rg[x])); for(int e : es) if(e != lst_e) del(e); trim(); if(dead[0]) return false; x = v[e]; lst_e = e; } assert(!dead[x]); assert(sz(g[x]) >= 2); debug(x); debug(g[x]); vi vis(n); vis[x] = 3; vi pth1, pth2; { int e = *g[x].begin(); pth1.emplace_back(e); while(!vis[v[e]]) { vis[v[e]] = 1; e = *g[v[e]].begin(); pth1.emplace_back(e); } } { int e = *g[x].rbegin(); pth2.emplace_back(e); while(!vis[v[e]]) { vis[v[e]] = 2; e = *g[v[e]].begin(); pth2.emplace_back(e); } } debug(pth0, pth1, pth2); vi important(n); important[x] = 1; important[v[pth1.back()]] = 1; important[v[pth2.back()]] = 1; vector<vi> s1, s2; vi cur; for(int a : pth1) { cur.emplace_back(a); if(important[v[a]]) { s1.emplace_back(cur); cur.clear(); } } cur.clear(); for(int a : pth2) { cur.emplace_back(a); if(important[v[a]]) { s2.emplace_back(cur); cur.clear(); } } debug(s1); debug(s2); auto rev = [&](vi v) { reverse(all(v)); return v; }; auto flatten = [&](vector<vi> ve) { vi res; for(auto& es : ve) for(int e : es) res.emplace_back(e); return res; }; auto go = [&](vector<vi> ve) { ve.insert(begin(ve),pth0); ve.emplace_back(rev(pth0)); int x = 0; vi res = flatten(ve); int lst_e = -1; for(int e : res) { assert(x == u[e]); assert(e != lst_e); lst_e = e; x = v[e]; swap(u[e], v[e]); } return res; }; auto do_cyc = [&](vector<vi> s, bool r) { vector<vi> g; if(sz(s) == 1) g.emplace_back(r ? rev(s[0]) : s[0]); else if(sz(s) == 2) { g.emplace_back(s[0]); g.emplace_back(r ? rev(s[1]) : s[1]); g.emplace_back(rev(s[0])); } else assert(false); return flatten(g); }; if(vis[v[s2.back().back()]] != 1) { // separate vector<vi> g; g.emplace_back(do_cyc(s1,false)); g.emplace_back(do_cyc(s2,false)); g.emplace_back(do_cyc(s1,true)); g.emplace_back(do_cyc(s2,true)); debug(g); return go(g); } assert(sz(s2) == 1); if(v[s1[0].back()] == v[s2[0].back()]) { vector<vi> g; if(v[s1.back().back()] == x) { assert(sz(s1) == 2); g.emplace_back(s1[0]); g.emplace_back(s1[1]); g.emplace_back(s2[0]); g.emplace_back(rev(s1[0])); g.emplace_back(rev(s1[1])); g.emplace_back(rev(s2[0])); return go(g); } auto ss1 = s1; ss1.erase(begin(ss1)); g.emplace_back(s1[0]); g.emplace_back(do_cyc(ss1,false)); g.emplace_back(rev(s1[0])); g.emplace_back(s2[0]); g.emplace_back(do_cyc(ss1,true)); g.emplace_back(rev(s2[0])); return go(g); } if(v[s1[1].back()] == v[s2[0].back()]) { assert(sz(s1) == 3); vector<vi> g; g.emplace_back(s1[0]); g.emplace_back(s1[1]); g.emplace_back(s1[2]); g.emplace_back(rev(s1[0])); g.emplace_back(s2[0]); g.emplace_back(rev(s1[1])); g.emplace_back(rev(s1[2])); g.emplace_back(rev(s2[0])); return go(g); } return true; assert(false); return true; }
#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...