#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define ll int
#define ld long double
#define pl pair<ll, ll>
#define vi vector<ll>
#define vii vector<vi>
#define vc vector<char>
#define vcc vector<vc>
#define vp vector<pl>
#define mi map<ll,ll>
#define mc map<char,int>
#define sortx(X) sort(X.begin(),X.end());
#define all(X) X.begin(),X.end()
#define allr(X) X.rbegin(),X.rend()
#define ln '\n'
#define YES {cout << "Alice\n"; return;}
#define NO {cout << "Bob\n"; return;}
#define MUN {cout << "-1\n"; return;}
const int MODE = 1e9 + 7;
using namespace std;
/*
eulerian circuit: that path start and end at the same vertex and visits every edge exactly once.
eulerian path: that path may start and end at different vertices and visits every edge exactly once.
in an undirected graph:
- an eulerian circuit exists if and only if every vertex has even degree and the graph is connected.
- an eulerian path if it's eulerian circuit or if it has exactly two vertices with odd degree and the graph is connected.
in directed graph:
- an eulerian circuit exists if and only if every vertex has equal in-degree and out-degree and the graph is strongly connected.
- an eulerian path exists if it has exactly one vertex with (out-degree - in-degree) = 1, exactly one vertex with (in-degree - out-degree) = 1, and all other vertices have equal indegree and out-degree.
*/
class Graph {
public:
int size;
vector<vp> adj;
void EulerPath(ll src, int edsz) {
stack<ll> st;
st.push(src);
vi visted(edsz);
stack<ll> tm;
vi ontm(size + 10);
while (!st.empty())
{
ll m = st.top();
bool isit = 1;
while (!adj[m].empty()) {
pl at = adj[m].back();
adj[m].pop_back();
if (visted[at.second]) continue;
visted[at.second] = 1;
st.push(at.first);
isit = 0;
break;
}
if (isit) {
if (ontm[m]) {
while (ontm[m]) {
ll tp = tm.top();
ontm[tp] = 0;
cout << tp << ' ';
tm.pop();
}
cout << '\n';
}
tm.push(m);
ontm[m] = 1;
st.pop();
}
}
}
void addEdge(int u, int v, int i) {
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
Graph(ll n) {
size = n;
adj.resize(n + 1);
}
};
void solve(int tc) {
ll n, m;
cin >> n >> m;
Graph gr(n+1);
for (int i = 0; i < m; i++)
{
ll u, v; cin >> u >> v;
gr.addEdge(u, v, i);
}
gr.EulerPath(1, m);
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int size = 1;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// cin >> size;
for (int i = 1; i <= size; i++)
solve(i);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |