제출 #1074488

#제출 시각아이디문제언어결과실행 시간메모리
1074488Zicrus수천개의 섬 (IOI22_islands)C++17
8.40 / 100
44 ms9688 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); queue<ll> q; q.push(0); while (!q.empty()) { ll cur = q.front(); q.pop(); if (vst[cur]) continue; vst[cur] = true; if (sz[lnk[cur]] >= 2) return true; for (auto &e : adj[cur]) { q.push(e.second); } } return false; } #ifdef TEST #include "grader.cpp" #endif
#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...