This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "islands.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define LL node * 2
#define RR node * 2 + 1
#define ll long long
#define MAXN 300005
const int modulo = 1e9 + 7;
const ll INF = 2e18 + 7;
vector<pii> in[MAXN], out[MAXN];
int nxt(int a){
if (a % 2 == 0) return a + 1;
return a - 1;
}
int vis[MAXN], done[MAXN];
int dfs(int node, deque<int> &ans, stack<pii> edges){
done[node] = 1;
vis[node] = 1;
for (auto i : out[node]){
if (done[i.st]){
vector<int> tmp;
while(true){
int t = edges.top().st;
tmp.pb(edges.top().nd);
edges.pop();
if (t == i.st) break;
if (edges.empty()) break;
}
reverse(tmp.begin(), tmp.end());
tmp.pb(i.nd);
ans.clear();
for (auto j : tmp) ans.pb(j);
for (auto j : tmp) ans.pb(j + 1);
reverse(tmp.begin(), tmp.end());
for (auto j : tmp) ans.pb(j);
for (auto j : tmp) ans.pb(j + 1);
while(!edges.empty()){
int t = edges.top().nd;
edges.pop();
ans.push_front(t);
ans.pb(t);
}
return 1;
}
else if (vis[i.st]) continue;
else{
edges.push({node, i.nd});
int res = dfs(i.st, ans, edges);
if (res == 1) return 1;
edges.pop();
}
}
done[node] = 0;
return 0;
}
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
for (int i = 0; i < M; i += 2){
in[V[i]].pb({U[i], i});
out[U[i]].pb({V[i], i});
}
deque<int> a;
stack<pii> tmp;
int res = dfs(0, a, tmp);
if (res == 0) return false;
vector<int> b;
for (auto i : a) b.pb(i);
return b;
}
/*
int main() {
fileio();
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> U(M), V(M);
for (int i = 0; i < M; ++i) {
assert(2 == scanf("%d %d", &U[i], &V[i]));
}
std::variant<bool, std::vector<int>> result = find_journey(N, M, U, V);
if (result.index() == 0) {
printf("0\n");
if (std::get<bool>(result)) {
printf("1\n");
} else {
printf("0\n");
}
} else {
printf("1\n");
std::vector<int> &canoes = std::get<std::vector<int>>(result);
printf("%d\n", static_cast<int>(canoes.size()));
for (int i = 0; i < static_cast<int>(canoes.size()); ++i) {
if (i > 0) {
printf(" ");
}
printf("%d", canoes[i]);
}
printf("\n");
}
return 0;
}
*/
# | 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... |