This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 100010
int n;
const int MOD = 1000000000;
int cnt;
int W[MAXN], N[MAXN];
queue<int> Q;
set<int> E[MAXN];
map<pair<int,int>, int> M;
pair<int,int> dfs(int node, int src) {
int total = W[node], ans = 0;
for (auto next : E[node]) if (src != next) {
pair<int,int> result = dfs(next, node);
ans = (ans + result.first) % MOD;
ans = (ans + ((long long) result.second * (long long) (n - result.second)) % MOD) % MOD;
total += result.second;
}
return { ans, total };
}
int findDist(vector<int> &X, vector<int> &Y) {
cnt = 0; M.clear();
for (int i = 0; i < MAXN; i ++) { E[i].clear(); W[i] = 0; N[i] = -1; }
for (int i = 0; i < n; i ++) M[{ X[i], Y[i] }] = i+1;
for (int i = 0; i < n; i ++) if (N[i] == -1) {
Q.push(i);
while (!Q.empty()) {
int node = Q.front(); Q.pop();
if (N[node] != -1) continue;
N[node] = cnt;
W[cnt] ++;
for (int diff = -1; diff <= 1; diff += 2) {
int next = M[{ X[node], Y[node] + diff }];
if (next != 0) Q.push(next-1);
}
}
cnt ++;
}
for (int i = 0; i < n; i ++) for (int diff = -1; diff <= 1; diff += 2) {
int j = M[{ X[i] + diff, Y[i] }];
if (j != 0) E[N[i]].insert(N[j-1]);
}
return dfs(0, -1).first;
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
vector<int> xs, ys;
for (int i = 0; i < n; i ++) {
xs.push_back(X[i]);
ys.push_back(Y[i]);
}
int ansX = findDist(xs, ys);
int ansY = findDist(ys, xs);
return (ansX + ansY) % MOD;
}
# | 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... |