#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<std::vector<int>>& out) {
vis[node] = true;
std::vector<int> cols;
for (int v : adj[node]) {
cols.push_back(node);
cols.push_back(v);
}
if (!cols.empty()) {
out.push_back(cols);
out.push_back({node});
}
for (int v : adj[node]) {
if (!vis[v]) {
out.push_back({v});
dfs(v, adj, vis, out);
out.push_back({node});
}
}
}
std::vector<std::vector<int>> create_map(int N, int M, std::vector<int> A, std::vector<int> B) {
if (N == 1) return {{1}};
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<std::vector<int>> ans;
std::vector<bool> vis(N+1, false);
dfs(1, tree, vis, ans);
while (!ans.empty() && ans.back().size() == 1) {
ans.pop_back();
}
size_t k = ans.size();
for (auto &v : ans) {
k = std::max(k, v.size());
}
for (auto &v : ans) {
v.resize(k, v[0]);
}
std::vector<int> filler(k, ans.back()[0]);
while (ans.size() < k) {
ans.push_back(filler);
}
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... |