# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
704193 | boyliguanhan | Senior Postmen (BOI14_postmen) | C++17 | 0 ms | 0 KiB |
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>
#pragma GCC optimize(2)
#define getchar_unlocked _getchar_nolock
using namespace std;
vector<pair<int, int>> adj[500100];
bitset<500100> removed;
int cnt[500100];
stack<int> sol;
int read(){
int res = 0, c = getchar_unlocked();
while(c < '0' || c > '9')
c = getchar_unlocked();
while(isdigit(c))
res = res*10+c - 48, c = getchar_unlocked();
return res;
}
void reverse(){
stack<int> temp;
while(sol.size())
temp.push(sol.top()), sol.pop();
sol = temp;
}
void print(){
stack<int> now;
bitset<500100> appeared;
while(sol.size()){
if(appeared[sol.top()]){
printf("%d", sol.top());
while(now.top()!=sol.top())
printf(" %d", now.top()), appeared[now.top()] = 0, now.pop();
printf("\n");
} else {
appeared[sol.top()] = 1;
now.push(sol.top());
}
sol.pop();
}
}
void dfs(int n){
for(int i = cnt[n]; i < adj[n].size(); i++)
if(!removed[adj[n][i].second])
removed[adj[n][i].second] = 1, cnt[n] = i, dfs(adj[n][i].first);
sol.push(n);
}
int main(){
int n = read(), m = read();
for(int i = 0; i < m; i++) {
int a = read(), b = read();
adj[a].push_back({b, i});
adj[b].push_back({a, i});
}
dfs(1);
reverse();
print();
return 0;
}