이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "islands.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> adj;
vector<int> visited;
bool ok;
vector<int> find_cycle(int s, set<int>&path)
{
bool skip = false;
vector<int> cycle;
int i = s;
do
{
for (auto node : adj[i])
{
if (!path.count(node.first)) continue;
cycle.push_back(node.second);
i = node.first;
break;
}
}
while (i != s);
const int sz = cycle.size();
for (int i = 0; i < sz; i++)
cycle.push_back(cycle[i] ^ 1);
for (int i = 2 * sz - 1 ; i >= 0; i--)
cycle.push_back(cycle[i] ^ 1);
return cycle;
}
void DFS(int index, set<int>&path, vector<int>&v)
{
visited[index] = 1;
path.insert(index);
for (auto node : adj[index])
{
if (path.count(node.first))
{
ok = true;
vector<int> cycle = find_cycle(node.first, path);
const int sz = cycle.size();
for (int i = 0; i < sz / 4 - 1; i++)
v.pop_back();
v.insert(v.end(), cycle.begin(), cycle.end());
for (int i = v.size() - sz - 1; i >= 0; i--)
v.push_back(v[i]);
return;
}
if (visited[node.first]) continue;
v.push_back(node.second);
DFS(node.first, path, v);
if (ok) return;
v.pop_back();
}
if (ok) return;
path.erase(index);
}
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V)
{
adj = vector<vector<pair<int, int>>>(N);
set<pair<int, int>> s;
for (int i = 0; i < M; i += 2)
{
if (s.count({U[i], V[i]})) continue;
adj[U[i]].push_back({V[i], i});
s.insert({U[i], V[i]});
}
ok = false;
visited = vector<int>(N);
set<int> path;
vector<int> res;
DFS(0, path, res);
if (ok)
return res;
return false;
}
컴파일 시 표준 에러 (stderr) 메시지
islands.cpp: In function 'std::vector<int> find_cycle(int, std::set<int>&)':
islands.cpp:11:8: warning: unused variable 'skip' [-Wunused-variable]
11 | bool skip = false;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |