Submission #706645

#TimeUsernameProblemLanguageResultExecution timeMemory
706645danikoynovThousands Islands (IOI22_islands)C++17
100 / 100
167 ms29964 KiB
#include "islands.h" #include <variant> #include <vector> #include <queue> #include <iostream> using namespace std; const int maxn = 1e5 + 10; int seen[maxn], in_deg[maxn], out_deg[maxn]; int n, m; vector < int > links[maxn], rev_links[maxn]; vector < pair < int, int > > graph[maxn]; vector < int > path[3], cycle[3]; int dfs(int node, int id, int root) { if (seen[node] != 0) return node; ///cout << node << " : " << id << " : " << root << endl; seen[node] = 1; for (pair < int, int > dest : graph[node]) { if (seen[dest.first] >= 0) { path[id].push_back(dest.second); int up = dfs(dest.first, id, root); if (id == 0) { if (up >= 0) { path[id].pop_back(); cycle[id].push_back(dest.second); seen[node] = 2; } else seen[node] = 0; if (node != root) { if (up == node) return - 1; else return up; } } if (up >= 0 && seen[up] == 1) { path[id].pop_back(); cycle[id].push_back(dest.second); if (node != root) { if (up == node) return -1; else return up; } } if (node != root || id == 1) return up; id ++; } } return -1; } int clear_useless() { queue < int > q; for (int i = 0; i < n; i ++) { if (!out_deg[i]) q.push(i); } int start = 0; while(!q.empty() || out_deg[start] <= 1) { if (!q.empty()) { int node = q.front(); q.pop(); seen[node] = -1; for (int dest : rev_links[node]) { out_deg[dest] --; if (out_deg[dest] == 0) q.push(dest); } } while(out_deg[start] <= 1) { seen[start] = -1; out_deg[start] = 0; for (int dest : rev_links[start]) { out_deg[dest] --; if (out_deg[dest] == 0) q.push(dest); } pair < int, int > nxt = make_pair(-1, 0); for (pair < int, int > cur : graph[start]) { if (seen[cur.first] != -1) { nxt = cur; break; } } if (nxt.first == -1) return -1; start = nxt.first; path[2].push_back(nxt.second); } } return start; } void append_to_end(vector < int > &base, vector < int > &to_add, int type) { if (type == 0) { for (int i = 0; i < to_add.size(); i ++) base.push_back(to_add[i]); } else { for (int i = (int)(to_add.size()) - 1; i >= 0; i --) base.push_back(to_add[i]); } } std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V) { n = N; m = M; for (int i = 0; i < M; i ++) { links[U[i]].push_back(V[i]); rev_links[V[i]].push_back(U[i]); in_deg[V[i]] ++; out_deg[U[i]] ++; graph[U[i]].push_back({V[i], i}); } int root = clear_useless(); if (root < 0) return false; dfs(root, 0, root); /** for (int i = 0; i < cycle[1].size(); i ++) cout << cycle[1][i] << " "; cout << endl; exit(0);*/ vector < int > ans; append_to_end(ans, path[2], 0); append_to_end(ans, path[0], 0); append_to_end(ans, cycle[0], 1); append_to_end(ans, path[0], 1); append_to_end(ans, path[1], 0); if (cycle[1].empty()) { int ver = V[path[1].back()]; int idx = 0; while(V[cycle[0][idx]] != ver) idx ++; ///cout << "here " << U[cycle[0][idx]] << " " << V[] for (int i = idx; i < cycle[0].size(); i ++) ans.push_back(cycle[0][i]); for (int i = 0; i < idx; i ++) ans.push_back(cycle[0][i]); } else append_to_end(ans, cycle[1], 1); append_to_end(ans, path[1], 1); if (!cycle[1].empty()) { append_to_end(ans, path[0], 0); append_to_end(ans, cycle[0], 0); append_to_end(ans, path[0], 1); append_to_end(ans, path[1], 0); append_to_end(ans, cycle[1], 0); append_to_end(ans, path[1], 1); } append_to_end(ans, path[2], 1); return ans; }

Compilation message (stderr)

islands.cpp: In function 'void append_to_end(std::vector<int>&, std::vector<int>&, int)':
islands.cpp:127:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |         for (int i = 0; i < to_add.size(); i ++)
      |                         ~~^~~~~~~~~~~~~~~
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:174:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |         for (int i = idx; i < cycle[0].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...