Skip to content

Commit 1845f61

Browse files
authored
graph
1 parent 800d83e commit 1845f61

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

oj/NOI18_citymapping/citymapping.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//graph
2+
3+
#include "citymapping.h"
4+
#include <vector>
5+
#include <algorithm>
6+
#define ll long long
7+
#define pii pair<int,int>
8+
#define pli pair<ll,int>
9+
using namespace std;
10+
vector<pii > edge[1001];
11+
int sz[1001], p[1001];
12+
ll d[1001][1001];
13+
int dfs(int x) {
14+
if (edge[x].empty()) return x;
15+
int mx = 0;
16+
int nxt = 0;
17+
for (auto p : edge[x]) {
18+
int c = p.first;
19+
if (mx < sz[c]) {
20+
nxt = c;
21+
mx = sz[c];
22+
}
23+
}
24+
return dfs(nxt);
25+
}
26+
int find_par(int root, int x, ll dist) {
27+
//printf("%d,%d,%lld\n",root,x,dist);
28+
if (edge[root].empty()) return root;
29+
30+
int leaf = dfs(root);
31+
ll x2l = get_distance(x, leaf);
32+
int lca = leaf;
33+
while (d[root][leaf] + dist < (d[root][lca] << 1) + x2l)
34+
lca = p[lca];// , printf("%d", lca);
35+
if (edge[lca].size() <= 1) return lca;
36+
//2
37+
int l = edge[lca][0].first, r = edge[lca][1].first;
38+
if (sz[l] < sz[r]) return find_par(l, x, dist - d[root][l]);
39+
else return find_par(r, x, dist - d[root][r]);
40+
}
41+
void find_roads(int N, int Q, int A[], int B[], int W[]) {
42+
ll mx = 0;
43+
int root = 0;
44+
for (int i = 2; i <= N; ++i) {
45+
d[1][i] = d[i][1] = get_distance(1, i);
46+
if (d[1][i] > mx) {
47+
mx = d[1][i];
48+
root = i;
49+
}
50+
}
51+
vector<pli > v;
52+
for (int i = 1; i <= N; ++i) {
53+
if (i == root) continue;
54+
d[root][i] = d[i][root] = get_distance(root, i);
55+
v.push_back({ d[root][i],i });
56+
}
57+
sort(v.begin(), v.end());
58+
sz[root] = 1;
59+
for (auto pr : v) {
60+
61+
int x = pr.second;
62+
ll dist = pr.first;
63+
int par = find_par(root, x, dist);
64+
edge[par].push_back({ x,dist - d[par][root] });
65+
p[x] = par; sz[x] = 1;
66+
while (par) {
67+
++sz[par];
68+
d[par][x] = d[x][par] = d[root][x] - d[root][par];
69+
par = p[par];
70+
}
71+
}
72+
int len = 0;
73+
for (int i = 1; i <= N; ++i) {
74+
for (auto p : edge[i]) {
75+
A[len] = i;
76+
B[len] = p.first;
77+
W[len] = p.second;
78+
++len;
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)