#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n"){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 5e5 + 69;
int n, m;
vector<int> graph[N];
int xor_edge[N];
bool banned[N];
vector<int> euler_tour;
void go(int u){
while(graph[u].size()){
int e = graph[u].back(); graph[u].pop_back();
if (banned[e]) continue;
banned[e] = true;
int v = xor_edge[e] ^ u;
go(v);
euler_tour.push_back(u);
}
}
void solve(){
cin >> n >> m;
for(int i = 0; i < m; ++i){
int u, v; cin >> u >> v;
xor_edge[i] = u ^ v;
graph[u].push_back(i);
graph[v].push_back(i);
}
for(int i = 1; i <= n; ++i) if (graph[i].size()){
euler_tour.push_back(i);
go(i);
}
vector<bool> visited(n+1);
vector<vector<int>> ans;
vector<int> st;
for(int i: euler_tour){
if (!visited[i]) {
visited[i] = true;
st.push_back(i);
}
else{
vector<int> cur = {i};
while(st.back() != i){
visited[st.back()] = 0;
cur.push_back(st.back());
st.pop_back();
}
ans.push_back(cur);
}
}
for(auto i: ans){
printArr(i);
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\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... |