Submission #837061

#TimeUsernameProblemLanguageResultExecution timeMemory
837061APROHACKThousands Islands (IOI22_islands)C++17
55 / 100
65 ms38508 KiB
#include "islands.h" #include <bits/stdc++.h> #define pb push_back #define ll long long #define ff first #define ss second #include <variant> #include <vector> using namespace std; vector<pair<int, int > >child[5005]; int devuelta[200005]; vector<pair<int, int > > adj[5005]; vector<int>tempRoute; vector<int>rt; bool dfs(int node, int parent){ //cout << " at node " << node << endl; for(int i = 0 ; i < adj[node].size() ; i ++){ if(adj[node][i].ff == parent)continue; child[node].pb(adj[node][i]); } if(child[node].size() >= 2){ int a = child[node][0].ss; int b = devuelta[child[node][0].ss]; int c = child[node][1].ss; int d = devuelta[c]; tempRoute = {a, b, c, d, b, a, d, c}; return true; } for(auto i : child[node]){ if(dfs(i.ff, node)){ rt.pb(i.ss); return true; } } return false; } int vis[5005]; #define ancestor 1 #define past 2 #define unvisited -1 int used[5005]; bool isCycle[5005]; vector<int>lastNodes; vector<int>ciclo, ejesCiclo[2], cicloCompleto; vector<int>finalRoute; vector<int>finalReal; bool dfs2(int node){ //cout << " at " << node << endl; finalRoute.pb(node); vis[node] = ancestor; for(int i = 0 ; i < adj[node].size() ; i ++){ if(adj[node][i].ss % 2 == 1)continue; if(vis[adj[node][i].ff] == ancestor){ used[node] = adj[node][i].ss; //cout << "found cicle: " << endl; while(finalRoute.back() != adj[node][i].ff){ ciclo.pb(finalRoute.back()); finalRoute.pop_back(); } ciclo.pb(finalRoute.back()); finalRoute.pop_back(); for(auto j : ciclo)isCycle[j] = true; reverse(ciclo.begin(), ciclo.end()); for(auto j : ciclo){ ejesCiclo[0].pb(used[j]); //cout << "eje ciclo " << used[j] << endl; ejesCiclo[1].pb(used[j]+1); } cicloCompleto = ejesCiclo[0]; for(auto j : ejesCiclo[1])cicloCompleto.pb(j); vector<int>aux = ejesCiclo[0]; reverse(aux.begin(), aux.end()); for(auto j : aux)cicloCompleto.pb(j); aux = ejesCiclo[1]; reverse(aux.begin(), aux.end()); for(auto j : aux)cicloCompleto.pb(j); return true; } } for(int i = 0 ; i < adj[node].size() ; i ++){ if(adj[node][i].ss % 2 == 1)continue; if(vis[adj[node][i].ff] == past)continue; // this is unvisited used[node] = adj[node][i].ss; if(dfs2(adj[node][i].ff)){ if(!isCycle[node]){ finalReal.pb(adj[node][i].ss); } return true; } } vis[node] = past; finalRoute.pop_back(); return false; } std::variant<bool, std::vector<int>> find_journey( int N, int M, std::vector<int> U, std::vector<int> V) { int cuenta[N][N]; vector<int>parejas[N][N]; for(int i = 0 ; i < N ; i ++){ for(int j = 0 ; j < N ; j ++){ cuenta[i][j] = 0; } } for(int i = 0 ; i < M ; i ++ ){ cuenta[U[i]][V[i]] ++ ; parejas[U[i]][V[i]].pb(i); adj[U[i]].pb({V[i], i}); } if (N == 2) { if(cuenta[0][1] >= 2 and cuenta[1][0] >= 1){ int a = parejas[0][1][0]; int b = parejas[1][0][0]; int c = parejas[0][1][1]; vector<int>ans = {a, b, c, a, b, c}; return ans; } return false; }else{ bool case3 = true; bool case4 = true; if( M % 2 == 1){ case3 = false; case4 = false; } if(case3 or case4){ for(int i = 0 ; i < M ; i += 2){ if(U[i] != V[i+1] or V[i] != U[i+1] )case3 = false; if(U[i] != U[i+1] or V[i] != V[i+1] )case4 = false; devuelta[i] = i+1; devuelta[i+1] = i; } } if(!case3 and !case4){ //cout << "wrong case : " << endl; int a = parejas[0][1][0]; int b = parejas[1][0][0]; int c = parejas[0][2][0]; int d = parejas[2][0][0]; vector<int>ans = {a, b, c, d, b, a, d, c}; return ans; }else if(case3){ //cout << "case 3 " << endl; if(!dfs(0, -1))return false; vector<int>ans = rt; reverse(ans.begin(), ans.end()); for(auto i : tempRoute)ans.pb(i); for(auto i : rt)ans.pb(i); return ans; }else{ //cout << " ! " << endl; for(int i = 0 ; i < 5005 ; i ++)vis[i] = unvisited; if(!dfs2(0))return false; vector<int>ans; reverse(finalReal.begin(), finalReal.end()); ans = finalReal; for(auto i : cicloCompleto)ans.pb(i); reverse(finalReal.begin(), finalReal.end()); for(auto j : finalReal)ans.pb(j); return ans; } } }

Compilation message (stderr)

islands.cpp: In function 'bool dfs(int, int)':
islands.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |  for(int i = 0 ; i < adj[node].size() ; i ++){
      |                  ~~^~~~~~~~~~~~~~~~~~
islands.cpp: In function 'bool dfs2(int)':
islands.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |  for(int i = 0 ; i < adj[node].size() ; i ++){
      |                  ~~^~~~~~~~~~~~~~~~~~
islands.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |  for(int i = 0 ; i < adj[node].size() ; 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...