Submission #641747

#TimeUsernameProblemLanguageResultExecution timeMemory
641747jamezzzThousands Islands (IOI22_islands)C++17
100 / 100
238 ms44168 KiB
#include "islands.h" #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #define dbg(...) printf(__VA_ARGS__); #define getchar_unlocked getchar #else #define dbg(...) #endif #define sf scanf #define pf printf #define fi first #define se second #define pb push_back #define psf push_front #define ppb pop_back #define ppf pop_front #define sz(x) (int)x.size() #define mnto(x,y) x=min(x,(__typeof__(x))y) #define mxto(x,y) x=max(x,(__typeof__(x))y) #define INF 1023456789 #define LINF 1023456789123456789 #define all(x) x.begin(),x.end() #define lb(x,v) (lower_bound(all(x),v)-x.begin()) #define ub(x,v) (upper_bound(all(x),v)-x.begin()) #define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin()); typedef long long ll; typedef long double ld; typedef pair<int,int> ii; typedef pair<ll,ll> pll; typedef tuple<int,int,int> iii; typedef tuple<int,int,int,int> iiii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ii> vii; mt19937 rng(time(0)); #define maxn 200005 int mxdep[maxn],pedge[maxn],par[maxn],dep[maxn]; bool vis[maxn],onstk[maxn],down[maxn],incycle[maxn],bothcycle[maxn]; map<int,ii> edgeid[maxn]; vi ans,cyclenode[2],tocycle[2],tocycleid[2],cycleid[2];; vii AL[maxn]; void add(vector<int> &v){ for(int x:v)ans.pb(x); } void addrev(vector<int> &v){ for(int i=sz(v)-1;i>=0;--i)ans.pb(v[i]); } void dfs(int u){ vis[u]=onstk[u]=true; mxdep[u]=-1; for(auto[v,i]:AL[u]){ if(!vis[v]){ pedge[v]=i; par[v]=u; dep[v]=dep[u]+1; dfs(v); down[i]=true; } if(onstk[v])mxto(mxdep[u],dep[v]); else{ mxto(mxdep[u],mxdep[v]); down[i]=true; } } onstk[u]=false; } bool getcyc(int u,vi &nodes){ vis[u]=onstk[u]=true; for(auto[v,i]:AL[u]){ if(!vis[v]){ if(getcyc(v,nodes)){ nodes.pb(u); return true; } } if(onstk[v]){ nodes.pb(v),nodes.pb(u); return true; } } onstk[u]=false; return false; } variant<bool,vi> find_journey(int N,int M,vi U,vi V){ for(int i=0;i<M;++i){ AL[U[i]].pb({V[i],i}); if(edgeid[U[i]].find(V[i])==edgeid[U[i]].end())edgeid[U[i]][V[i]]={i,i}; else edgeid[U[i]][V[i]].se=i; } dfs(0); for(int rt=0;rt<N;++rt){ vi goodedge; for(auto[x,i]:AL[rt]){ if(down[i]&&mxdep[x]>=dep[rt])goodedge.pb(x); } if(sz(goodedge)<2)continue; vi pathtort; int tmp=rt; while(tmp){ pathtort.pb(pedge[tmp]); tmp=U[pedge[tmp]]; } reverse(all(pathtort)); add(pathtort); for(int i=0;i<2;++i){ int x=goodedge[i]; memset(vis,false,sizeof vis); memset(onstk,false,sizeof onstk); for(int x:pathtort)vis[U[x]]=vis[V[x]]=true; vis[rt]=onstk[rt]=true; vi nodes; getcyc(x,nodes); nodes.pb(rt); while(true){ tocycle[i].pb(nodes.back()); if(nodes.back()!=nodes[0])nodes.ppb(); else break; } reverse(all(nodes)); swap(nodes,cyclenode[i]); } int type=0; for(int i:cyclenode[0])incycle[i]=true; for(int i:cyclenode[1]){ if(incycle[i]){ bothcycle[i]=true; if(i!=rt)type=1; } } for(int x=0;x<2;++x){ for(int i=0;i<sz(tocycle[x])-1;++i){ int u=tocycle[x][i],v=tocycle[x][i+1]; tocycleid[x].pb(edgeid[u][v].fi); swap(edgeid[u][v].fi,edgeid[u][v].se); } for(int i=0;i<sz(cyclenode[x])-1;++i){ int u=cyclenode[x][i],v=cyclenode[x][i+1]; cycleid[x].pb(edgeid[u][v].fi); swap(edgeid[u][v].fi,edgeid[u][v].se); } } if(type==0){//disjoint cycle for(int x=0;x<2;++x){ add(tocycleid[x]); add(cycleid[x]); addrev(tocycleid[x]); } for(int x=0;x<2;++x){ add(tocycleid[x]); addrev(cycleid[x]); addrev(tocycleid[x]); } } else{//overlapping cycle add(tocycleid[0]); add(cycleid[0]); addrev(tocycleid[0]); add(tocycleid[1]); vi added; for(int i:cycleid[1]){ int u=U[i],v=V[i]; if(u!=rt&&bothcycle[u]){ int start=-1; for(int j=0;j<sz(cycleid[0]);++j){ int id=cycleid[0][j]; int u2=U[id],v2=V[id]; if(v2==u)start=j; } for(int j=start;j>=0;--j)ans.pb(cycleid[0][j]); for(int j=sz(cycleid[0])-1;j>start;--j)ans.pb(cycleid[0][j]); break; } ans.pb(i); added.pb(i); } addrev(added); addrev(tocycleid[1]); } addrev(pathtort); return ans; } return false; }

Compilation message (stderr)

islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, vi, vi)':
islands.cpp:176:11: warning: unused variable 'u2' [-Wunused-variable]
  176 |       int u2=U[id],v2=V[id];
      |           ^~
islands.cpp:171:16: warning: unused variable 'v' [-Wunused-variable]
  171 |     int u=U[i],v=V[i];
      |                ^
#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...