Junior Math.

Description

Given a tree or loop-based tree consisting of $N$ points.

Get the equation of linear regression of a set of points, then calculate the minimum distance from every point to the line.

Solution

Write down the expression of the equation of linear regression.

Then split parts out.

Image from @zhuohan123

Construct the sum arrays with doubling algorithm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1e+5 + 5;
const double eps = 1e-7;

struct edge {
int to, nxt;
}e[maxn << 1];
int n, m, ptr, lnk[maxn];
int F[maxn][19], inc[maxn], dep[maxn], w[maxn];
double x[maxn], y[maxn];
struct data
{
int n;
double x, y, x2, y2, xy;
data() {
n = 0;
x = y = x2 = y2 = xy = 0;
}
data(double x_, double y_) {
n = 1;
x = x_; y = y_;
x2 = x * x; y2 = y * y;
xy = x * y;
}
data(int n_, double x_, double y_, double x2_, double y2_, double xy_) {
n = n_; x = x_; y = y_; x2 = x2_; y2 = y2_; xy = xy_;
}
data operator + (const data &rhs) {
return data(n + rhs.n, x + rhs.x, y + rhs.y, x2 + rhs.x2, y2 + rhs.y2, xy + rhs.xy);
}
data operator - (const data &rhs) {
return data(n - rhs.n, x - rhs.x, y - rhs.y, x2 - rhs.x2, y2 - rhs.y2, xy - rhs.xy);
}
inline double AVGx() {
return x / (double)n;
}
inline double AVGy() {
return y / (double)n;
}
inline double A() {
double ax = AVGx();
return x2 - 2.0 * ax * x + n * ax * ax;
}
inline double B() {
double ax = AVGx(), ay = AVGy();
return 2.0 * ay * x + 2.0 * ax * y - 2.0 * xy - 2.0 * n * ax * ay;
}
inline double C() {
double ay = AVGy();
return y2 - 2.0 * ay * y + n * ay * ay;
}
inline double sigma() {
if (n == 1) return 0;
double a = A(), b = B(), c = C();
//printf("%.6lf %.6lf %.6lf\n", a, b, c);
double sq = sqrt(a * a + b * b + c * c - 2 * a * c);
double sig1 = (a + c + sq) / 2, sig2 = (a + c - sq) / 2;
if (sig1 > sig2) swap(sig1, sig2);
return sig1 > -eps ? sig1 : sig2;
}
}d[maxn];

inline void add(int bgn, int end) {
e[++ptr] = (edge){end, lnk[bgn]};
lnk[bgn] = ptr;
}
inline int rd() {
register int x = 0, f = 0, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = 1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return f ? -x : x;
}

namespace SolveTree {
void dfs(int x) {
dep[x] = dep[F[x][0]] + 1;
for (int i = 1; i <= 17; ++i)
F[x][i] = F[F[x][i - 1]][i - 1];
for (int p = lnk[x]; p; p = e[p].nxt) {
int y = e[p].to;
if (y == F[x][0]) continue;
d[y] = d[y] + d[x];
F[y][0] = x;
dfs(y);
}
}
inline int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
for (int i = 17; ~i; --i)
if (dep[F[y][i]] >= dep[x]) y = F[y][i];
if (x == y) return x;
for (int i = 17; ~i; --i)
if (F[y][i] ^ F[x][i]) y = F[y][i], x = F[x][i];
return F[x][0];
}
void main() {
dfs(1);
int Q = rd(), u, v;
while (Q--) {
u = rd(); v = rd(); int LCA = lca(u, v);
data res = d[u] + d[v] - d[LCA] - d[F[LCA][0]];
printf("%.5lf\n", res.sigma());
}
}
}
namespace SolveCycle {
int cyc[maxn << 1], cptr, dfn, rev[maxn];
int cid[maxn << 1];
data cyd[maxn << 1];
bool findloop(int x, int fa) {
rev[++dfn] = x; inc[x] = 1;
for (int p = lnk[x]; p; p = e[p].nxt) {
int y = e[p].to;
if (y == fa) continue;
if (inc[y]) {
while (rev[dfn] != y) {
cyc[++cptr] = rev[dfn];
cid[rev[dfn]] = cptr;
--dfn;
}
cyc[++cptr] = y;
cid[y] = cptr;
return true;
} else if (findloop(y, x)) return true;
}
dfn--;
return (inc[x] = false);
}
void dfs(int x, int bel) {
w[x] = bel;
dep[x] = dep[F[x][0]] + 1;
for (int i = 1; i <= 17; ++i)
F[x][i] = F[F[x][i - 1]][i - 1];
for (int p = lnk[x]; p; p = e[p].nxt) {
int y = e[p].to;
if (y != F[x][0] && !cid[y]) {
d[y] = d[y] + d[x];
F[y][0] = x;
dfs(y, bel);
}
}
}
inline int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
for (int i = 17; ~i; --i)
if (dep[F[y][i]] >= dep[x]) y = F[y][i];
if (x == y) return x;
for (int i = 17; ~i; --i)
if (F[y][i] ^ F[x][i]) y = F[y][i], x = F[x][i];
return F[x][0];
}
void main() {
findloop(1, 0);
for (int i = 1; i <= cptr; ++i) dfs(cyc[i], cyc[i]);
for (int i = 1; i <= cptr; ++i) cyc[i + cptr] = cyc[i];
for (int i = 1; i <= 2 * cptr; ++i) cyd[i] = cyd[i - 1] + d[cyc[i]];
int Q = rd(), u, v;
while (Q--) {
u = rd(); v = rd();
if (w[u] == w[v]) {
int LCA = lca(u, v);
data res = d[u] + d[v] - d[LCA] - d[F[LCA][0]];
printf("%.6lf\n", res.sigma());
} else {
int anu = w[u], anv = w[v];
data resu = d[u] - d[anu], resv = d[v] - d[anv];
if (cid[anu] > cid[anv]) swap(anu, anv);
data pre1 = cyd[cid[anv]] - cyd[cid[anu] - 1];
data pre2 = cyd[cid[anu] + cptr] - cyd[cid[anv] - 1];
printf("%.5lf\n", min((resu + resv + pre1).sigma(), (resu + resv + pre2).sigma()));
}
}
}
}

int main() {
n = rd(); m = rd();
for (int i = 1; i <= n; ++i) {
x[i] = rd();
y[i] = rd();
d[i] = data(x[i], y[i]);
}
int u, v;
for (int i = 1; i <= m; ++i) {
u = rd(); v = rd();
add(u, v);
add(v, u);
}
if (m == n - 1) {
SolveTree::main();
} else {
SolveCycle::main();
}
return 0;
}