Submission #1074192

#TimeUsernameProblemLanguageResultExecution timeMemory
1074192ZicrusThousands Islands (IOI22_islands)C++17
3.50 / 100
34 ms8652 KiB
#include <bits/stdc++.h> #include "islands.h" using namespace std; typedef long long ll; int n, m; vector<vector<pair<int, int>>> adj, revAdj; // edgeId, node vector<bool> vst, edgeUsed; stack<int> stk; vector<int> lnk, sz, acc; vector<int> numPaths; void dfs1(int cur) { vst[cur] = true; for (auto &e : adj[cur]) { if (vst[e.second]) continue; dfs1(e.second); } stk.push(cur); } int dfs2(int cur, int rep) { vst[cur] = true; lnk[cur] = rep; sz[rep]++; int last = cur; for (auto &e : revAdj[cur]) { if (vst[e.second]) continue; edgeUsed[e.first] = true; last = dfs2(e.second, rep); } if (cur == rep && cur != last) { for (auto &e : adj[cur]) { if (e.second == last) { edgeUsed[e.first] = true; break; } } } return last; } int dfsPaths(int cur) { vst[cur] = true; int res = 0; for (auto &e : revAdj[cur]) { if (edgeUsed[e.first]) continue; if (vst[e.second]) { res += numPaths[e.second]; } else { res += dfsPaths(e.second); } } return numPaths[cur] = res; } variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) { n = N; m = M; lnk = vector<int>(n); sz = vector<int>(n); acc = vector<int>(n); adj = vector<vector<pair<int, int>>>(n); revAdj = vector<vector<pair<int, int>>>(n); for (int i = 0; i < n; i++) lnk[i] = i; for (int i = 0; i < m; i++) { adj[U[i]].push_back({i, V[i]}); revAdj[V[i]].push_back({i, U[i]}); } edgeUsed = vector<bool>(m); vst = vector<bool>(n); for (int i = 0; i < n; i++) { if (vst[i]) continue; dfs1(i); } vst = vector<bool>(n); while (!stk.empty()) { ll cur = stk.top(); stk.pop(); if (vst[cur]) continue; dfs2(cur, cur); } vst = vector<bool>(n); numPaths = vector<int>(n); vst[0] = true; numPaths[0] = 1; for (int i = 0; i < n; i++) { if (vst[i]) continue; dfsPaths(i); } vector<int> compPaths(n); vector<int> rep(n); int a = -1, b = -1; for (int i = 0; i < n; i++) { if (sz[lnk[i]] <= 1) continue; compPaths[lnk[i]] += numPaths[i]; if (compPaths[lnk[i]] >= 2) { a = rep[lnk[i]]; b = i; break; } rep[lnk[i]] = i; } return a != -1; } #ifdef TEST #include "grader.cpp" #endif

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:95:17: warning: variable 'b' set but not used [-Wunused-but-set-variable]
   95 |     int a = -1, b = -1;
      |                 ^
#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...