#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <stack>
#include <cstdlib>
#include <queue>
#include <cstdio>
#include <limits.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
vector<pair<int,int>> adj[(int)5e5];
bool hasVisited[(int)5e5];
bool okay[(int)5e5];
int main() {
//freopen("balancing.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
scanf("%d%d", &N, &M);
pair<int,int> edges[2 * M];
for (int i = 0; i < M; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
edges[2 * i] = {u, v};
edges[2 * i + 1] = {v, u};
adj[u].emplace_back(v, 2 * i), adj[v].emplace_back(u, 2 * i + 1);
}
for (int i = 0; i < M; i++) {
okay[i] = true;
}
stack<pair<int,int>> nodes;
for (int i = 0; i < N; i++) {
nodes.emplace(i, -1);
while (!nodes.empty()) {
int cur = nodes.top().first;
okay[nodes.top().second] = false;
hasVisited[cur] = true;
bool upd = false;
while (!adj[cur].empty()) {
pair<int,int> pr = adj[cur].back();
int j = adj[cur].back().first;
if (pr.second == nodes.top().second || !okay[pr.second]) {
continue;
}
okay[pr.second] = false;
if (hasVisited[j]) {
while (!nodes.empty() && nodes.top().first != j) {
hasVisited[nodes.top().first] = false;
printf("%d ", nodes.top().first + 1);
nodes.pop();
}
printf("%d\n", j + 1);
} else {
nodes.emplace(j, pr.second);
}
upd = true;
break;
}
if (!upd) {
nodes.pop();
}
}
}
}
Compilation message
postmen.cpp:16: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
16 | #pragma GCC optimization ("O3")
|
postmen.cpp:17: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
17 | #pragma GCC optimization ("unroll-loops")
|
postmen.cpp: In function 'int main()':
postmen.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
postmen.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1096 ms |
37408 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1086 ms |
37744 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1075 ms |
34468 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |