#include "worldmap.h"
#include <bits/stdc++.h>
using namespace std;
// #define int long long int
using vi = vector<int>;
using vvi = vector<vi>;
vi tin;
int timer = 0;
void dfs(int u, vector<bool> &vis, vvi &adj, vvi &uadj, vi &tour) {
tin[u] = timer++;
vis[u] = true;
tour.push_back(u);
// sort(adj[u].begin(), adj[u].end(), [&](int a, int b) {return adj[a].size() < adj[b].size();});
for (int v : adj[u]) {
if (vis[v]) {
if (tin[v] < tin[u]) uadj[u].push_back(v);
continue;
}
dfs(v, vis, adj, uadj, tour);
tour.push_back(u);
}
}
inline int diagonal_length(int K, int s) {
return (s < K) ? (s + 1) : (2*K - 1 - s);
}
bool check_works(int n, int K) {
int tl_fine = 0;
int cdx = 0;
for (int i = 0; i < n; i++) {
int cnt = 0;
while (cnt < (n-1) && cdx < (2*K-1)) {
cnt += diagonal_length(K, cdx);
cdx+=2;
}
cdx+=2;
if (cdx >= (2*K-1)) return false;
if (cnt >= (n-1)) tl_fine++;
}
return tl_fine >= n;
}
bool validate(int n, int m, vi a, vi b, vvi ret) {
set<pair<int, int>> adjacencies;
set<pair<int, int>> expected;
for (int i = 0; i < m; i++) {
expected.insert({min(a[i], b[i]), max(a[i], b[i])});
}
int k = ret.size();
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
if (i > 0 && ret[i-1][j] != ret[i][j]) adjacencies.insert({min(ret[i][j], ret[i-1][j]), max(ret[i][j], ret[i-1][j])});
if (j > 0 && ret[i][j-1] != ret[i][j]) adjacencies.insert({min(ret[i][j], ret[i][j-1]), max(ret[i][j], ret[i][j-1])});
if (i < k-1 && ret[i+1][j] != ret[i][j]) adjacencies.insert({min(ret[i][j], ret[i+1][j]), max(ret[i][j], ret[i+1][j])});
if (j < k-1 && ret[i][j+1] != ret[i][j]) adjacencies.insert({min(ret[i][j], ret[i][j+1]), max(ret[i][j], ret[i][j+1])});
}
}
for (auto &el : adjacencies) {
if (!expected.count(el)) return false;
}
for (auto &el :expected) {
if (!adjacencies.count(el)) return false;
}
return true;
}
vvi create_map(int n, int m, vi a, vi b) {
vvi adj(n+1, vi()); for (int i = 0; i < m; i++) adj[a[i]].push_back(b[i]), adj[b[i]].push_back(a[i]);
tin.resize(n+1);
int K = 2*n;
vector<set<pair<int, int>>> diags(2*K-1);
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++) {
int md = i + j;
diags[md].insert({i, j});
}
}
vector<bool> vis(n+1, false); vi tour; set<int> seen;
vvi uadj(n+1, vi());
dfs(1, vis, adj, uadj, tour);
vvi ans(K, vi(K, 1)), dgs(n+1);
int cdx = 0;
set<int> used;
for (int i = 0; i < tour.size(); i++) {
for (auto &el : diags[cdx]) {
ans[el.first][el.second] = tour[i];
}
cdx++;
if (used.count(tour[i])) continue;
used.insert(tour[i]);
int cnt = 0;
while (cnt < (uadj[tour[i]].size())) {
cnt += diags[cdx].size();
dgs[tour[i]].push_back(cdx);
cdx++;
for (auto &el : diags[cdx]) {
ans[el.first][el.second] = tour[i];
}
cdx++;
}
}
for (int i = 1; i <= n; i++) {
int aidx = 0;
for (auto &gd : dgs[i]) {
for (auto &el : diags[gd]) {
if (aidx >= uadj[i].size()) {
ans[el.first][el.second] = i;
continue;
}
ans[el.first][el.second] = uadj[i][aidx++];
}
}
}
// cerr << "Is the answer valid? " << validate(n, m, a, b, ans) << "\n";
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... |