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<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>E[101000];
int n, SZ[101000], P[101000];
long long Res, D[101000], C[101000];
struct point{
int x, y, num;
bool operator<(const point &p)const{
return x != p.x ? x < p.x : y < p.y;
}
}w[101000], w2[101000], L[101000];
void DFS(int a, int pp){
int i;
C[a] = SZ[a];
for (i = 0; i < E[a].size(); i++){
if (E[a][i] != pp){
DFS(E[a][i], a);
C[a] += C[E[a][i]];
}
}
Res += C[a] * (n-C[a]);
}
void Do(){
int i, cnt = 0, a, b, j, cc = 0;
sort(w, w + n);
for (i = 0; i < n; i++){
if (!i || w[i].x != w[i - 1].x || w[i - 1].y + 1 != w[i].y)SZ[++cnt] = 0;
SZ[cnt]++;
w2[i].num = cnt, w2[i].x = w[i].y, w2[i].y = w[i].x;
}
for (i = 1; i <= cnt; i++)P[i] = 0;
sort(w2, w2 + n);
for (i = 0; i < n - 1; i++){
if (w2[i].x == w2[i + 1].x && w2[i].y + 1 == w2[i + 1].y){
a = w2[i].num, b = w2[i + 1].num;
if (P[a] != b){
P[a] = b;
L[cc].x = a, L[cc].y = b; cc++;
}
}
}
for (i = 0; i < cc; i++){
E[L[i].x].push_back(L[i].y);
E[L[i].y].push_back(L[i].x);
}
DFS(1, 0);
for (i = 1; i <= cnt; i++)E[i].clear();
}
int DistanceSum(int N, int *X, int *Y) {
int i;
n = N;
for (i = 0; i < N; i++){
w[i].x = X[i], w[i].y = Y[i];
}
Do();
for (i = 0; i < N; i++){
w[i].x = Y[i], w[i].y = X[i];
}
Do();
return Res%1000000000;
}
Compilation message (stderr)
city.cpp: In function 'void DFS(int, int)':
city.cpp:17:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < E[a].size(); i++){
~~^~~~~~~~~~~~~
city.cpp: In function 'void Do()':
city.cpp:26:24: warning: unused variable 'j' [-Wunused-variable]
int i, cnt = 0, a, b, j, cc = 0;
^
# | 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... |