# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096995 | thangdz2k7 | City Mapping (NOI18_citymapping) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#include "citymapping.h"
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
ll d[N][N];
ll dist(int x, int y){
if (x > y) swap(x, y);
if (x == y) return 0;
if (!d[x][y]) d[x][y] = get_distance(x, y);
return d[x][y];
}
bool del[N];
int sz[N], heavy[N];
vi adj[N];
void dfs(int u){
sz[u] = 1, heavy[u] = 0;
for (int v : adj[u]) if (!del[v]){
dfs(v); sz[u] += sz[v];
if (sz[v] > sz[heavy[u]]) heavy[u] = v;
}
}
void find_roads(int n, int q, int a[], int b[], int w[]){
vector <int> sr;
for (int i = 2; i < n + 1; ++ i) sr.pb(i);
sort(sr.begin(), sr.end(), [&](int u, int v){
return dist(1, x) < dist(1, y);
});
int nE = 0;
for (int u : sr){
int r = 1;
for (int i = 1; i <= n; ++ i) del[i] = 0;
while (true){
dfs(r); vi path = {r};
while (heavy[r]) r = heavy[r], path.pb(r);
if (path.size() == 1) break;
ll x = dist(1, path.back()) + dist(1, u) - dist(u, r.back()); x /= 2;
for (int u : path) if (dist(1, u) == x) {
r = u; break;
} else del[u] = 1;
}
adj[r].pb(u); a[nE] = r, b[nE] = u, w[nE] = dist(r, u), ++ nE;
}
}
//void solve(){
//
//}
//
//int main(){
// if (fopen("pqh.inp", "r")){
// freopen("pqh.inp", "r", stdin);
// freopen("pqh.out", "w", stdout);
// }
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
//
// int t = 1; // cin >> t;
// while (t --) solve();
// return 0;
//}