# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1252641 | dreamnguyen | 세계 지도 (IOI25_worldmap) | C++20 | 0 ms | 0 KiB |
#include "worldmap.h"
std::vector<std::vector<int>> create_map(int N, int M, std::vector<int> A, std::vector<int> B) {
const int K = 2 * N;
std::vector<std::vector<int>> grid(K, std::vector<int>(K, 0));
std::vector<std::vector<int>> adj(N + 1);
for (int i = 0; i < M; ++i) {
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
std::vector<std::vector<bool>> visited(K, std::vector<bool>(K, false));
std::vector<bool> country_used(N + 1, false);
std::function<void(int, int, int)> dfs = [&](int x, int y, int c) {
if (x < 0 || y < 0 || x >= K || y >= K || visited[x][y]) return;
visited[x][y] = true;
grid[x][y] = c;
country_used[c] = true;
for (int d = 0; d < 4; ++d) {
int nx = x + (d == 0) - (d == 1);
int ny = y + (d == 2) - (d == 3);
if (nx < 0 || ny < 0 || nx >= K || ny >= K) continue;
if (!visited[nx][ny]) {
for (int nc : adj[c]) {
if (!country_used[nc]) {
dfs(nx, ny, nc);
break;
}
}
}
}
};
dfs(K / 2, K / 2, 1);
for (int i = 1; i <= N; ++i) {
if (!country_used[i]) {
for (int x = 0; x < K; ++x) {
for (int y = 0; y < K; ++y) {
if (grid[x][y] == 0) {
grid[x][y] = i;
goto next;
}
}
}
next:;
}
}
return grid;
}