/*
ID: USACO_template
LANG: C++
PROG: https://oj.uz/problem/view/BOI14_postmen
*/
#include <iostream> //cin , cout
#include <fstream> //fin, fout
#include <stdio.h> // scanf , pringf
#include <cstdio>
#include <algorithm> // sort , stuff
#include <stack> // stacks
#include <queue> // queues
#include <map>
#include <string>
#include <string.h>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi; /// adjlist without weight
typedef vector<pii> vii; /// adjlist with weight
typedef vector<pair<int,pii>> vpip; /// edge with weight
typedef long long ll;
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5; //
const ll INF = 1e18; //
#define MAXV 500007
#define MAXE 500007
const int xdir[4] = {1,0,-1,0}, ydir[4] = {0,1,0,-1}; /// 4 directions
struct NODE {
int x, y;
int val;
int visited;
bool operator< (NODE b) const { return (x == b.x) ? (y < b.y) : (x < b.x); }
};
struct EDGE {
int from, to;
ll weight;
bool operator<(EDGE other) const { return weight < other.weight; }
};
bool debug;
int N, M;
vii adjlist[MAXV];
int nxtEdge[MAXV];
int visited[MAXV], walked[MAXE];
vi cycle;
int svx;
void dfs(int u) {
visited[u] = 1;
while(nxtEdge[u] < sz(adjlist[u])) { /// still have an edge to walk
int j = nxtEdge[u];
int v = adjlist[u][j].ff, e = adjlist[u][j].ss;
nxtEdge[u]++;
if(walked[e]) continue;
walked[e] = 1;
if(visited[v]) { /// find a cycle
cycle.pb(u); svx = v; return;
}
dfs(v);
/// backtrack
cycle.pb(u);
if(svx != u) {
if(svx>0) return;
} else {
string ret = "";
for(auto x : cycle) {
ret += to_string(x) + " ";
//cout << x << " ";
visited[x] = 0; // can re-use in other cycle
}
cout << ret << endl;
/// now use u as the starting point of a new cycle
visited[u] = 1;
svx = -1;
vi().swap(cycle);
}
}
}
int main() {
debug = false;
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
for(int i=1; i<=M; i++) {
int a, b; cin >> a >> b;
adjlist[a].pb(mp(b,i)); adjlist[b].pb(mp(a, i));
}
for(int i=1; i<=N; i++)
if(nxtEdge[i] < sz(adjlist[i])) dfs(i);
/// similar to Eulerian tour, prioritize pick edges first
if(debug) cout << endl << "EOL" << endl;
}
/**
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 |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
8 ms |
11996 KB |
Output is correct |
4 |
Correct |
11 ms |
12108 KB |
Output is correct |
5 |
Correct |
9 ms |
12108 KB |
Output is correct |
6 |
Correct |
10 ms |
12236 KB |
Output is correct |
7 |
Correct |
16 ms |
12636 KB |
Output is correct |
8 |
Correct |
9 ms |
12564 KB |
Output is correct |
9 |
Correct |
49 ms |
14796 KB |
Output is correct |
10 |
Correct |
11 ms |
12108 KB |
Output is correct |
11 |
Correct |
11 ms |
12260 KB |
Output is correct |
12 |
Correct |
65 ms |
15172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
9 ms |
11980 KB |
Output is correct |
4 |
Correct |
13 ms |
12108 KB |
Output is correct |
5 |
Correct |
9 ms |
12048 KB |
Output is correct |
6 |
Correct |
10 ms |
12236 KB |
Output is correct |
7 |
Correct |
15 ms |
12616 KB |
Output is correct |
8 |
Correct |
10 ms |
12620 KB |
Output is correct |
9 |
Correct |
49 ms |
14844 KB |
Output is correct |
10 |
Correct |
14 ms |
12196 KB |
Output is correct |
11 |
Correct |
11 ms |
12312 KB |
Output is correct |
12 |
Correct |
54 ms |
15184 KB |
Output is correct |
13 |
Correct |
106 ms |
41472 KB |
Output is correct |
14 |
Correct |
117 ms |
16436 KB |
Output is correct |
15 |
Correct |
120 ms |
15996 KB |
Output is correct |
16 |
Correct |
107 ms |
41496 KB |
Output is correct |
17 |
Correct |
135 ms |
16344 KB |
Output is correct |
18 |
Correct |
139 ms |
22508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
9 ms |
11980 KB |
Output is correct |
3 |
Correct |
9 ms |
12012 KB |
Output is correct |
4 |
Correct |
12 ms |
12108 KB |
Output is correct |
5 |
Correct |
9 ms |
12108 KB |
Output is correct |
6 |
Correct |
10 ms |
12236 KB |
Output is correct |
7 |
Correct |
15 ms |
12660 KB |
Output is correct |
8 |
Correct |
9 ms |
12620 KB |
Output is correct |
9 |
Correct |
47 ms |
14796 KB |
Output is correct |
10 |
Correct |
14 ms |
12196 KB |
Output is correct |
11 |
Correct |
13 ms |
12356 KB |
Output is correct |
12 |
Correct |
55 ms |
15212 KB |
Output is correct |
13 |
Correct |
90 ms |
41444 KB |
Output is correct |
14 |
Correct |
144 ms |
16504 KB |
Output is correct |
15 |
Correct |
124 ms |
16056 KB |
Output is correct |
16 |
Correct |
97 ms |
41476 KB |
Output is correct |
17 |
Correct |
128 ms |
16228 KB |
Output is correct |
18 |
Correct |
122 ms |
22608 KB |
Output is correct |
19 |
Execution timed out |
587 ms |
161496 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |