Submission #1077755

#TimeUsernameProblemLanguageResultExecution timeMemory
1077755Faisal_SaqibThousands Islands (IOI22_islands)C++17
10 / 100
124 ms21616 KiB
#include <bits/stdc++.h> #include <variant> #include <vector> using namespace std; const int N=1e3+100; int n,m; vector<int> adj[N]; int color[N],parent[N]; map<pair<int,int>,int> helper; int cycle_start, cycle_end; bool dfs(int v) { color[v] = 1; for (int u : adj[v]) { if(u==parent[v])continue; if (color[u] == 0) { parent[u] = v; if (dfs(u)) return true; } else if (color[u] == 1) { if(u==0) { cycle_end = v; cycle_start = u; } return true; } } color[v] = 2; return false; } vector<int> find_cycle() { for(int i=0;i<n;i++) { color[i]=0; parent[i]=-1; } cycle_start = -1; for (int v = 0; v < n; v++) { if (color[v] == 0 && dfs(v)) break; } if (cycle_start == -1) { return {}; } else { vector<int> cycle; cycle.push_back(cycle_start); for (int v = cycle_end; v!=cycle_start; v = parent[v]) cycle.push_back(v); cycle.push_back(cycle_start); reverse(cycle.begin(), cycle.end()); return cycle; } } std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> v1, std::vector<int> v2) { n=N,m=M; map<pair<int,int>,vector<int>> cnt; for(int j=0;j<m;j++) cnt[{v1[j],v2[j]}].push_back(j); if(n>400) { for(int j=0;j<m;j++) { helper[{v1[j],v2[j]}]=j; adj[v1[j]].push_back(v2[j]); } vector<int> cur=find_cycle(); if(cur.size()==0) { return false; } else { vector<int> ed; vector<int> oth; for(int i=1;i<(cur.size());i++) { ed.push_back(helper[{cur[i-1],cur[i]}]); int fp=ed.back(); oth.push_back(fp+((fp%2==0)?1:-1)); } vector<int> ans; for(auto i:ed) { ans.push_back(i); } reverse(begin(oth),end(oth)); for(auto i:oth) { ans.push_back(i); } reverse(begin(ans),end(ans)); for(auto i:ans){ ans.push_back(i); } reverse(begin(oth),end(oth)); for(auto i:oth) { ans.push_back(i); } // found a cycle start at zero } } else if(n>2) { int edge01=cnt[{0,1}][0]; int edge10=cnt[{1,0}][0]; int edge02=cnt[{0,2}][0]; int edge20=cnt[{2,0}][0]; int edge12=cnt[{1,2}][0]; int edge21=cnt[{2,1}][0]; return vector<int>({edge01,edge12,edge20,edge02,edge21,edge10,edge20,edge12,edge01,edge10,edge21,edge02}); } else if(cnt[{0,1}].size()>=2 and cnt[{1,0}].size()>=1) { int f=cnt[{0,1}][0]; int s=cnt[{0,1}][1]; int th=cnt[{1,0}][0]; return vector<int>({f,th,s,f,th,s}); } else { return false; } }

Compilation message (stderr)

islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:80:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |             for(int i=1;i<(cur.size());i++)
      |                         ~^~~~~~~~~~~~~
islands.cpp:61:36: warning: control reaches end of non-void function [-Wreturn-type]
   61 |     map<pair<int,int>,vector<int>> cnt;
      |                                    ^~~
#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...