#include using namespace std; vector> create_map(int N, int M, vector A, vector B) { // adjacency matrix vector> adj(N+1, vector(N+1, false)); vector> g(N+1); for (int i = 0; i < M; ++i) { int a = A[i], b = B[i]; adj[a][b] = adj[b][a] = true; g[a].push_back(b); g[b].push_back(a); } // ---------- spanning tree (BFS from 1) ---------- vector vis(N+1, false); vector> tree(N+1); vector> is_tree_edge(N+1, vector(N+1, false)); queue q; q.push(1); vis[1] = true; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : g[u]) { if (!vis[v]) { vis[v] = true; tree[u].push_back(v); tree[v].push_back(u); if (u < v) is_tree_edge[u][v] = true; else is_tree_edge[v][u] = true; q.push(v); } } } // ---------- DFS traversal of the tree (start and end at 1) ---------- vector seq; function dfs = [&](int u, int p) { seq.push_back(u); for (int v : tree[u]) { if (v != p) { dfs(v, u); seq.push_back(u); } } }; dfs(1, 0); int L = seq.size(); // L = 2*N - 1 // ---------- initial two rows ---------- vector> grid; vector> fixed; // which cells are not allowed to be changed // row 0: the DFS sequence grid.push_back(seq); fixed.push_back(vector(L, true)); // row 1: first element 1, then seq[1..] vector row1(L); row1[0] = 1; for (int i = 1; i < L; ++i) row1[i] = seq[i]; grid.push_back(row1); fixed.push_back(vector(L, true)); // ---------- collect non‑tree edges ---------- vector> non_tree; for (int i = 0; i < M; ++i) { int a = A[i], b = B[i]; if (a > b) swap(a, b); if (!is_tree_edge[a][b]) non_tree.emplace_back(A[i], B[i]); } // ---------- place each non‑tree edge ---------- for (auto [u, v] : non_tree) { bool placed = false; while (!placed) { int rows = grid.size(); vector& cur_row = grid.back(); vector& cur_fixed = fixed.back(); vector& prev_row = grid[rows-2]; // row above // try to find two consecutive free cells for (int i = 0; i < L-1; ++i) { if (cur_fixed[i] || cur_fixed[i+1]) continue; if (!adj[prev_row[i]][u]) continue; if (!adj[prev_row[i+1]][v]) continue; if (i > 0 && !adj[cur_row[i-1]][u]) continue; if (i+1 < L-1 && !adj[v][cur_row[i+2]]) continue; // place the edge here cur_row[i] = u; cur_row[i+1] = v; cur_fixed[i] = cur_fixed[i+1] = true; placed = true; break; } if (!placed) { // add a new row (copy of the last row, all cells changeable) grid.push_back(grid.back()); fixed.push_back(vector(L, false)); } } } // ---------- make the grid square ---------- int R = grid.size(); int C = L; int K = max(R, C); // extend rows by duplicating the last row while ((int)grid.size() < K) { grid.push_back(grid.back()); } // extend columns by duplicating the last column of each row for (int i = 0; i < K; ++i) { while ((int)grid[i].size() < K) { grid[i].push_back(grid[i].back()); } } return grid; }