#include "worldmap.h"
#include <cassert>
#include <queue>
#include <iostream>
std::vector<std::pair<int, int>> spanning_tree(std::vector<std::vector<int>>& adj) {
std::vector<bool> vis(adj.size(), false);
std::vector<std::pair<int, int>> edges;
std::queue<int> q;
q.push(1);
vis[1] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
for (int dest : adj[node]) {
if (!vis[dest]) {
vis[dest] = true;
edges.push_back({node, dest});
q.push(dest);
}
}
}
return edges;
}
void dfs(int node, std::vector<std::vector<int>>& adj, std::vector<bool>& vis, std::vector<int>& order) {
vis[node] = true;
order.push_back(node);
for (int dest : adj[node]) {
if (!vis[dest]) {
dfs(dest, adj, vis, order);
order.push_back(node);
}
}
}
std::vector<std::vector<int>> create_map(int N, int M, std::vector<int> A, std::vector<int> B) {
std::vector<std::vector<int>> adj(N+1, std::vector<int>());
for (int i = 0; i < M; i++) {
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
std::vector<std::pair<int, int>> tree_edges = spanning_tree(adj);
std::vector<std::vector<int>> tree(N+1, std::vector<int>());
for (auto &edge : tree_edges) {
tree[edge.first].push_back(edge.second);
tree[edge.second].push_back(edge.first);
}
std::vector<bool> vis(N+1, false);
std::vector<int> order;
dfs(1, tree, vis, order);
int K = 0, C = 0;
for (int i = 1; i <= N; i++) {
K = std::max(K, 2 * (int) adj[i].size());
}
std::vector<bool> seen(N+1, false);
for (int x : order) {
if (!seen[x]) C += 3;
else ++C;
seen[x] = true;
}
K = std::max(K, C);
std::vector<std::vector<int>> ans(K, std::vector<int>(K));
std::fill(seen.begin(), seen.end(), false);
int ptr = 0;
for (int x : order) {
// fill first row all with the number
for (int j = 0; j < K; j++) {
ans[ptr][j] = x;
}
++ptr;
if (!seen[x]) {
// fill second row in some alternating sequence
for (int j = 0; j < K; j++) {
ans[ptr][j] = (j % 2 == 0 ? x : adj[x][(j/2) % adj[x].size()]);
}
++ptr;
// just do the new row again
for (int j = 0; j < K; j++) {
ans[ptr][j] = x;
}
++ptr;
}
seen[x] = true;
}
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++) {
if (ans[i][j] == 0) ans[i][j] = 1; // fill rest or smth
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |