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 <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
struct Edge
{
unsigned u, v, i, j, k; // u <-> i, v <-> j;
Edge() {}
Edge(unsigned u_, unsigned v_, unsigned i_, unsigned j_, unsigned k_)
{
u = u_, v = v_, i = i_, j = j_, k = k_;
}
};
vector<vector<Edge>> g;
vector<Edge> edges;
vector<bool> visited;
void delete_edge(Edge e)
{
swap(g[e.u][e.i], g[e.u].back());
swap(g[e.v][e.j], g[e.v].back());
g[e.u].erase(g[e.u].end() - 1);
g[e.v].erase(g[e.v].end() - 1);
if (e.i < g[e.u].size())
{
g[e.u][e.i].i = e.i;
edges[g[e.u][e.i].k] = g[e.u][e.i];
g[g[e.u][e.i].v][g[e.u][e.i].j].j = e.i;
edges[g[g[e.u][e.i].v][g[e.u][e.i].j].k] = g[g[e.u][e.i].v][g[e.u][e.i].j];
}
if (e.j < g[e.v].size())
{
g[e.v][e.j].i = e.j;
edges[g[e.v][e.j].k] = g[e.v][e.j];
g[g[e.v][e.j].v][g[e.v][e.j].j].j = e.j;
edges[g[g[e.v][e.j].v][g[e.v][e.j].j].k] = g[g[e.v][e.j].v][g[e.v][e.j].j];
}
}
unsigned get_next_edge(unsigned u, unsigned p)
{
if (g[u][0].v == p)
return 1;
return 0;
}
unsigned find_circuit(unsigned u, unsigned p = -1)
{
if (visited[u])
return u;
while (!g[u].empty())
{
visited[u] = 1;
Edge e = g[u][get_next_edge(u, p)];
unsigned x = find_circuit(e.v, u);
visited[u] = 0;
if (x != UINT_MAX)
{
cout << u + 1 << ' ';
delete_edge(edges[e.k]);
if (x == u)
{
cout << '\n';
x = UINT_MAX;
}
return x;
}
}
return UINT_MAX;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m;
cin >> n >> m;
g = vector<vector<Edge>>(n);
visited = vector<bool>(n, 0);
edges = vector<Edge>(m);
for (size_t i = 0; i < m; i++)
{
unsigned u, v;
cin >> u >> v;
u--, v--;
g[u].emplace_back(u, v, g[u].size(), g[v].size(), i);
g[v].emplace_back(v, u, g[v].size(), g[u].size() - 1, i);
edges[i] = g[u].back();
}
for (unsigned u = 0; u < n; u++)
{
find_circuit(u);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |