/*
File created on 05/27/2021 at 15:50:04.
Link to problem: https://oj.uz/problem/view/BOI14_postmen
Description: Hierholzer's algorithm for eulerian cycle + separation in "mini" cycles
Time complexity: O(N+M)
Space complexity: O(N+M)
Status: TLE
Copyright: Ⓒ 2021 Francois Vogel
*/
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
using namespace std;
#define pii pair<int, int>
#define pib pair<int, *obj>
#define f first
#define s second
#define pb push_back
#define ins insert
#define int ll
#define ll long long
int encode(int a, int b) { return (min(a, b)*1000000+max(a, b)); }
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, nbEdges;
cin >> n >> nbEdges;
vector<vector<int>> graph(n);
for (int i = 0; i < nbEdges; i++) {
int nodeA, nodeB;
cin >> nodeA >> nodeB;
nodeA--;
nodeB--;
graph[nodeA].pb(nodeB);
graph[nodeB].pb(nodeA);
}
unordered_set<int> usedEdges;
vector<int> circuit;
vector<int> path;
path.pb(0);
while (!path.empty()) {
while (!graph[path.back()].empty() and usedEdges.find(encode(path.back(), graph[path.back()].back())) != usedEdges.end()) graph[path.back()].pop_back();
if (graph[path.back()].empty()) {
circuit.pb(path.back());
path.pop_back();
}
else {
int nn = graph[path.back()].back();
graph[path.back()].pop_back();
usedEdges.insert(encode(path.back(), nn));
path.pb(nn);
}
}
bool as [n];
memset(as, false, n);
vector<int> st;
for (int i : circuit) {
if (as[i]) {
while (st.back() != i) {
as[st.back()] = false;
cout << st.back()+1 << " ";
st.pop_back();
}
st.pop_back();
cout << i+1 << " " << endl;
}
as[i] = true;
st.pb(i);
}
int d = 0;
d++;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
10 ms |
1740 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
65 ms |
9468 KB |
Output is correct |
10 |
Correct |
4 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
72 ms |
9908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
10 ms |
1740 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
64 ms |
9564 KB |
Output is correct |
10 |
Correct |
7 ms |
588 KB |
Output is correct |
11 |
Correct |
5 ms |
588 KB |
Output is correct |
12 |
Correct |
77 ms |
9928 KB |
Output is correct |
13 |
Correct |
76 ms |
14232 KB |
Output is correct |
14 |
Correct |
117 ms |
12068 KB |
Output is correct |
15 |
Correct |
129 ms |
12104 KB |
Output is correct |
16 |
Correct |
88 ms |
14304 KB |
Output is correct |
17 |
Correct |
143 ms |
10640 KB |
Output is correct |
18 |
Correct |
111 ms |
11772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
5 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
10 ms |
1724 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
64 ms |
9544 KB |
Output is correct |
10 |
Correct |
4 ms |
588 KB |
Output is correct |
11 |
Correct |
5 ms |
588 KB |
Output is correct |
12 |
Correct |
78 ms |
9924 KB |
Output is correct |
13 |
Correct |
69 ms |
14304 KB |
Output is correct |
14 |
Correct |
115 ms |
12068 KB |
Output is correct |
15 |
Correct |
122 ms |
12068 KB |
Output is correct |
16 |
Correct |
73 ms |
14276 KB |
Output is correct |
17 |
Correct |
131 ms |
10540 KB |
Output is correct |
18 |
Correct |
114 ms |
11760 KB |
Output is correct |
19 |
Execution timed out |
531 ms |
68580 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |