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;
#define pb push_back
#define sz size
#define all(x) begin(x), end(x)
#define ll long long
#define INF 1e18
#define MOD (int)(1e9 + 7)
int N, M, cnt[500005];
vector<pair<int, int>> adj[500005];
vector<int> cycle;
vector<vector<int>> ans;
bool seen[500005];
void dfs(int v) {
while(adj[v].sz()) {
pair<int, int> p = adj[v].back();
adj[v].pop_back();
int u = p.first, id = p.second;
if(!seen[id]) {
seen[id] = true;
dfs(u);
}
}
cycle.pb(v);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
for(int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
adj[a].pb({b, i});
adj[b].pb({a, i});
}
dfs(1);
// 1 5 8 10 9 2 3 6 7 8 4 7 5 4 3 1
stack<int> st;
for(int i = 0; i < (int)cycle.sz(); ++i) {
int v = cycle[i];
cnt[v]++;
if(cnt[v] > 1) {
vector<int> cyc;
while(st.top() != v) {
cyc.pb(st.top());
cnt[st.top()]--;
st.pop();
}
st.pop();
cnt[v]--;
cyc.pb(v);
ans.pb(cyc);
}
st.push(v);
}
for(auto const &p : ans) {
for(int v : p)
cout << v << ' ';
cout << '\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... |