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 ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e5 + 10;
vector<pair<int, int> > g[MAX_N];
bool used[MAX_N];
int n, m;
vector<int> ans;
int cnt[MAX_N];
bool in[MAX_N];
void dfs(int x) {
for(; cnt[x] < g[x].size();) {
auto it = g[x][cnt[x] ++];
if(used[it.second]) {continue;}
used[it.second] = true;
dfs(it.first);
}
ans.push_back(x);
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; i ++) {
int a, b;
cin >> a >> b;
g[a].push_back({b, i});
g[b].push_back({a, i});
}
for(int i = 1; i <= n; i ++) {
if(cnt[i] != g[i].size()) {
dfs(i);
}
}
stack<int> st;
for(auto it : ans) {
if(in[it]) {
cout << it << " ";
while(st.top() != it) {
cout << st.top() << " ";
in[st.top()] = false;
st.pop();
}
cout << endl;
} else {
st.push(it);
in[it] = true;
}
}
return 0;
}
Compilation message (stderr)
postmen.cpp: In function 'void dfs(int)':
postmen.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(; cnt[x] < g[x].size();) {
| ~~~~~~~^~~~~~~~~~~~~
postmen.cpp: In function 'int main()':
postmen.cpp:42:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if(cnt[i] != g[i].size()) {
| ~~~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |