| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1293823 | 123123123 | 세계 지도 (IOI25_worldmap) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<vector<int>> mstAdj;
vector<bool> used;
vector<int> tour;
void DFS(int v) {
used[v] = true;
tour.push_back(v);
for (int to : mstAdj[v]) {
if (!used[to]) {
DFS(to);
tour.push_back(v);
}
}
}
vector<vector<int>> create_map(int N, int M, vector<int> A, vector<int> B) {
if (M == 0) {
return vector<vector<int>>(1, vector<int>(1, 1));
}
adj.assign(N + 1, {});
mstAdj.assign(N + 1, {});
used.assign(N + 1, false);
for (int i = 0; i < M; i++) {
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
vector<bool> visited(N + 1, false);
queue<int> q;
q.push(1);
visited[1] = true;
while (!q.empty()) {
int v = q.front(); q.pop();
for (int to : adj[v]) {
if (!visited[to]) {
visited[to] = true;
q.push(to);
mstAdj[v].push_back(to);
mstAdj[to].push_back(v);
}
}
}
DFS(1);
int rows = 4 * N;
int cols = 4 * N;
vector<vector<int>> mymap(rows, vector<int>(cols, 0));
int curRow = 0;
map<int,int> firstOcc;
for (int node : tour) {
if (!firstOcc.count(node)) {
firstOcc[node] = curRow;
curRow++;
int r1 = curRow++;
int r2 = curRow++;
for (int j = 0; j < cols; j++) mymap[r2][j] = node;
int col = 0;
for (int nei : adj[node]) {
if (col >= cols) break;
mymap[r1][col++] = node;
if (col >= cols) break;
mymap[r1][col++] = nei;
}
}
}
for (int i = 1; i < rows; i++)
for (int j = 0; j < cols; j++)
if (mymap[i][j] == 0)
mymap[i][j] = mymap[i-1][j];
return mymap;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
for (int i = 0; i < M; i++) cin >> A[i];
for (int i = 0; i < M; i++) cin >> B[i];
vector<vector<int>> grid = create_map(N, M, A, B);
for (auto &row : grid) {
for (int j = 0; j < (int)row.size(); j++) {
cout << row[j];
if (j + 1 < (int)row.size()) cout << " ";
}
cout << "\n";
}
}
