Submission #812795

#TimeUsernameProblemLanguageResultExecution timeMemory
812795blackyukiThousands Islands (IOI22_islands)C++17
100 / 100
151 ms25272 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<bool> vb; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++) #define all(a) a.begin(),a.end() #define fi first #define se second #define pb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} const ll inf=1001001001001; #include "islands.h" std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V) { vvp g(N),rg(N); rep(i,M){ g[U[i]].pb(V[i],i); rg[V[i]].pb(U[i],i); } vi cnt(N);rep(i,N)cnt[i]=g[i].size(); vb dead(N,false); queue<ll> que; auto f=[&](ll i){ que.push(i); while(!que.empty()){ ll t=que.front();que.pop(); for(auto x:rg[t])if(!dead[x.fi]){ cnt[x.fi]--; if(cnt[x.fi]==0){ dead[x.fi]=true;que.push(x.fi); } } } }; auto loop=[&](ll s,vp v){ vi ver; ver.pb(s); for(auto x:v)ver.pb(x.fi); ll i=0; while(ver[i]!=ver.back())i++; vi res; rep(j,i)res.pb(v[j].se); REP(j,i,v.size())res.pb(v[j].se); for(ll j=i-1;j>=0;j--)res.pb(v[j].se); return res; }; auto rloop=[&](ll s,vp v){ vi ver; ver.pb(s); for(auto x:v)ver.pb(x.fi); ll i=0; while(ver[i]!=ver.back())i++; vi res; rep(j,i)res.pb(v[j].se); for(ll j=v.size()-1;j>=i;j--)res.pb(v[j].se); for(ll j=i-1;j>=0;j--)res.pb(v[j].se); return res; }; rep(i,N)if(!dead[i]&&cnt[i]==0){ dead[i]=true;f(i); } if(dead[0])return false; ll cur=0; vi ans; while(true){ vp nex; for(auto x:g[cur])if(!dead[x.fi])nex.pb(x); if(nex.size()==0)return false; if(nex.size()>1){ vb seen1(N,false),seen2(N,false); vp a1,a2; { a1.pb(nex[0]); seen1[cur]=true; ll i=nex[0].fi; while(!seen1[i]){ seen1[i]=true; ll j=0;while(dead[g[i][j].fi])j++; a1.pb(g[i][j]); i=g[i][j].fi; } } { a2.pb(nex[1]); seen2[cur]=true; ll i=nex[1].fi; while(!seen1[i]&&!seen2[i]){ seen2[i]=true; ll j=0;while(dead[g[i][j].fi])j++; a2.pb(g[i][j]); i=g[i][j].fi; } if(!seen2[i]){ vi ver; vp prev(N,P(-1,-1)); ver.pb(cur); for(auto x:a1)ver.pb(x.fi); ll l=0; while(ver[l]!=ver.back())l++; REP(i,l,ver.size()-1)prev[ver[i]]=P(ver[i+1],a1[i].se); if(prev[a2.back().fi]!=P(-1,-1)){ rep(i,ver.size()-l-1){ a2.pb(prev[a2.back().fi]); } } else{ ll idx=0; while(a1[idx].fi!=a2.back().fi)idx++; REP(i,idx+1,a1.size())a2.pb(a1[i]); } vi r1=loop(cur,a1),r2=rloop(cur,a2); vector<int> res; for(ll x:ans)res.pb(x); for(ll x:r1)res.pb(x); for(ll x:r2)res.pb(x); reverse(all(ans)); for(ll x:ans)res.pb(x); return res; } else{ vi r1=loop(cur,a1),r2=loop(cur,a2),r3=rloop(cur,a1),r4=rloop(cur,a2); vector<int> res; for(ll x:ans)res.pb(x); for(ll x:r1)res.pb(x); for(ll x:r2)res.pb(x); for(ll x:r3)res.pb(x); for(ll x:r4)res.pb(x); reverse(all(ans)); for(ll x:ans)res.pb(x); return res; } } } dead[cur]=true;f(cur); cur=nex[0].fi;ans.pb(nex[0].se); } }
#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...