#include <bits/stdc++.h>
using namespace std;
const int64_t mod = 1e9;
vector<vector<int>> adj;
vector<int64_t> d, c, ans;
vector<pair<int, int>> del = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
void dfs(int node, int par){
for(int v : adj[node]){
if(v == par)
continue;
dfs(v, node);
c[node] += c[v] + 1;
d[node] += d[v] + c[v] + 1;
}
}
void calc(int node, int par, int N){
if(node == 0) ans[node] = d[node];
else ans[node] = ans[par] - 2*c[node] + N - 2;
for(int v : adj[node]){
if(v == par) continue;
calc(v, node, N);
}
}
int DistanceSum(int N, int *X, int *Y) {
adj.resize(N);
vector<array<int, 2>> P(N);
for(int i = 0; i < N; i++)
P[i] = {X[i], Y[i]};
sort(P.begin(), P.end());
vector<int> vis(N, 0);
queue<array<int, 3>> q;
q.push({P[0][0], P[0][1], 0});
vis[0] = 1;
while(!q.empty()){
array<int, 3> cur = q.front();
int x = cur[0], y = cur[1], idx = cur[2];
q.pop();
for(auto &[dx, dy] : del){
array<int, 2> t = {x+dx, y+dy};
auto it = lower_bound(P.begin(), P.end(), t);
int tmp = distance(P.begin(), it);
array<int, 2> temp = P[tmp];
if(temp[0] == x+dx && temp[1] == y+dy){
if(!vis[tmp]){
vis[tmp] = 1;
adj[tmp].push_back(idx);
adj[idx].push_back(tmp);
array<int, 3> p = {temp[0], temp[1], tmp};
q.push(p);
}
}
}
}
d.resize(N), ans.resize(N), c.resize(N);
dfs(0, 0);
calc(0, 0, N);
int64_t f = 0;
for(int i = 0; i < N; i++)
(f += ans[i]) %= mod;
return f/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
2540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
2540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |