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 <iostream>
#include <vector>
#define pii pair<int, int>
#define fst first
#define snd second
using namespace std;
const int INF = 1e9;
int N, M;
vector<pii> adj[500001];
bool inStack[500001];
bool used[500001];
int main()
{
cin.tie(0) -> sync_with_stdio(0);
cin >> N >> M;
for (int i = 0; i < M; i++)
{
int u, v; cin >> u >> v;
adj[u - 1].push_back({v - 1, i});
adj[v - 1].push_back({u - 1, i});
}
for (int i = 0; i < N; i++)
{
vector<int> S;
if (adj[i].size())
{
inStack[i] = 1; S.push_back(i);
while (S.size())
{
int u = S.back();
//cerr << "TRV: " << u << "\n";
if (adj[u].size())
{
int v = adj[u].back().fst, id = adj[u].back().snd; adj[u].pop_back();
if (used[id]) continue;
used[id] = 1;
if (inStack[v])
{
//cerr << "CYC: " << v << "\n";
while (S.back() != v)
{
cout << S.back() + 1 << " ";
inStack[S.back()] = 0;
S.pop_back();
}
cout << v + 1 << "\n";
}
else {S.push_back(v); inStack[v] = 1;}
}
else {inStack[i] = 0; S.pop_back();}
}
}
}
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... |