이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
const int MOD = 1e9+7;
#define f first
#define s second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
const int mxn = 5e5+5;
int n, m;
vector<pair<int, bool*>> adj[mxn];
bool in_stack[mxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
bool* active = new bool{true};
adj[a].pb({b, active});
adj[b].pb({a, active});
}
vector<vi> ans;
stack<int> st;
for(int i = 1; i <= n; i++) {
while(!adj[i].empty() && *adj[i].back().s == false) adj[i].pop_back();
if(adj[i].empty()) continue;
st.push(i);
in_stack[i] = true;
while(!st.empty()) {
int a = st.top();
while(!adj[a].empty() && *adj[a].back().s == false) adj[a].pop_back();
int b = adj[a].back().f;
*adj[a].back().s = false;
adj[a].pop_back();
if(in_stack[b]) {
vi route;
while(st.top() != b) in_stack[st.top()] = false, route.pb(st.top()), st.pop();
while(!adj[b].empty() && *adj[b].back().s == false) adj[b].pop_back();
if(adj[b].empty()) {
in_stack[b] = false;
st.pop();
}
route.pb(b);
ans.pb(route);
}
else {
in_stack[b] = true;
st.push(b);
}
}
}
for(auto v : ans) {
for(int a : v) {
cout << a << " ";
}
cout << "\n";
}
return 0;
}
/*
10 15
1 3
5 1
2 3
9 2
3 4
6 3
4 5
7 4
4 8
5 7
8 5
6 7
7 8
8 10
10 9
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |