File tree Expand file tree Collapse file tree
tests/library_checker_aizu_tests/graphs Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #pragma once
2+ vector<pii> euler_walk (auto & adj, int m, int s) {
3+ vi vis (m);
4+ vector<pii> path;
5+ auto dfs = [&](auto && self, int u, int e) -> void {
6+ while (!empty (adj[u])) {
7+ auto [v, e_id] = adj[u].back ();
8+ adj[u].pop_back ();
9+ if (vis[e_id]) continue ;
10+ vis[e_id] = 1 ;
11+ self (self, v, e_id);
12+ }
13+ path.push_back ({u, e});
14+ };
15+ dfs (dfs, s, -1 );
16+ ranges::reverse (path);
17+ return path;
18+ }
Original file line number Diff line number Diff line change 1+ #define PROBLEM \
2+ " https://judge.yosupo.jp/problem/eulerian_trail_directed"
3+ #include " ../template.hpp"
4+ #include " ../../../library/graphs/euler_walk.hpp"
5+ int main () {
6+ cin.tie (0 )->sync_with_stdio (0 );
7+ int t;
8+ cin >> t;
9+ while (t--) {
10+ int n, m;
11+ cin >> n >> m;
12+ vector<vector<pii>> adj (n);
13+ vector<int > deg (n);
14+ int s = -1 ;
15+ for (int i = 0 ; i < m; i++) {
16+ int u, v;
17+ cin >> u >> v;
18+ s = u;
19+ adj[u].push_back ({v, i});
20+ deg[u]++;
21+ deg[v]--;
22+ }
23+ if (*max_element (all (deg)) >= 2 ) {
24+ cout << " No" << ' \n ' ;
25+ continue ;
26+ }
27+ if (ranges::count (deg, 1 ) >= 2 ) {
28+ cout << " No" << ' \n ' ;
29+ continue ;
30+ }
31+ auto it = ranges::find (deg, 1 );
32+ if (it != end (deg)) s = it - begin (deg);
33+ else if (s == -1 ) s = 0 ;
34+ vector<pii> res = euler_walk (adj, m, s);
35+ if (ssize (res) != m + 1 ) {
36+ cout << " No" << ' \n ' ;
37+ continue ;
38+ }
39+ cout << " Yes" << ' \n ' ;
40+ for (int i = 0 ; i < ssize (res); i++)
41+ cout << res[i].first << ' ' ;
42+ cout << ' \n ' ;
43+ for (int i = 1 ; i < ssize (res); i++)
44+ cout << res[i].second << ' ' ;
45+ cout << ' \n ' ;
46+ }
47+ return 0 ;
48+ }
Original file line number Diff line number Diff line change 1+ #define PROBLEM \
2+ " https://judge.yosupo.jp/problem/eulerian_trail_undirected"
3+ #include " ../template.hpp"
4+ #include " ../../../library/graphs/euler_walk.hpp"
5+ int main () {
6+ cin.tie (0 )->sync_with_stdio (0 );
7+ int t;
8+ cin >> t;
9+ while (t--) {
10+ int n, m;
11+ cin >> n >> m;
12+ vector<vector<pii>> adj (n);
13+ vector<int > deg (n);
14+ int s = -1 ;
15+ for (int i = 0 ; i < m; i++) {
16+ int u, v;
17+ cin >> u >> v;
18+ s = u;
19+ adj[u].push_back ({v, i});
20+ adj[v].push_back ({u, i});
21+ deg[u] ^= 1 ;
22+ deg[v] ^= 1 ;
23+ }
24+ if (ranges::count (deg, 1 ) > 2 ) {
25+ cout << " No" << ' \n ' ;
26+ continue ;
27+ }
28+ auto it = ranges::find (deg, 1 );
29+ if (it != end (deg)) s = it - begin (deg);
30+ else if (s == -1 ) s = 0 ;
31+ vector<pii> res = euler_walk (adj, m, s);
32+ if (ssize (res) != m + 1 ) {
33+ cout << " No" << ' \n ' ;
34+ continue ;
35+ }
36+ cout << " Yes" << ' \n ' ;
37+ for (int i = 0 ; i < ssize (res); i++)
38+ cout << res[i].first << ' ' ;
39+ cout << ' \n ' ;
40+ for (int i = 1 ; i < ssize (res); i++)
41+ cout << res[i].second << ' ' ;
42+ cout << ' \n ' ;
43+ }
44+ return 0 ;
45+ }
You can’t perform that action at this time.
0 commit comments