#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define mod 10000000000LL
int head[maxn];
long long all, now, ans;
long long sum[maxn], val[maxn];
bool vis[maxn];
vector<int> from[maxn];
map<pair<int,int>, int> p;
void dfs(int x,int h) {
int i;
vis[x] = 1; now = (now + val[x]*h)%mod;
for(i=0;i<from[x].size();i++) {
if(!vis[from[x][i]]) {
dfs(from[x][i],h+1);
sum[x] = (sum[x] + sum[from[x][i]])%mod;
}
}
sum[x] = (sum[x] + val[x])%mod;
}
void dfs2(int x) {
int i;
vis[x] = 1;
// printf("dfs2 %d : now = %lld => %lld\n",x,now,now*val[x]);
ans = (ans + now*val[x]);
for(i=0;i<from[x].size();i++) {
if(!vis[from[x][i]]) {
now = (now + all - sum[from[x][i]] - sum[from[x][i]] + mod)%mod;
dfs2(from[x][i]);
now = (now - all + sum[from[x][i]] + sum[from[x][i]] + mod)%mod;
}
}
}
int findhead(int x) {
if(head[x]==x) return x;
return head[x] = findhead(head[x]);
}
void solve(int N, int *X, int *Y,int type) {
int i,x;
pair<int,int> t;
map<pair<int,int>, int>::iterator it;
p.clear();
memset(sum,0,sizeof(sum)); memset(val,0,sizeof(val));
for(i=0;i<N;i++) head[i] = i, from[i].clear();
for(i=0;i<N;i++) {
if(type==0) p[{X[i],Y[i]}] = i;
else p[{Y[i],X[i]}] = i;
}
for(it=p.begin();it!=p.end();++it) {
t = it->first;
if(p.find({t.first,t.second+1})!=p.end()) head[findhead(p[t])] = findhead(p[{t.first,t.second+1}]);
}
for(i=0;i<N;i++) val[findhead(i)]++;
for(it=p.begin();it!=p.end();++it) {
t = it->first;
if(p.find({t.first+1,t.second})!=p.end()) from[findhead(p[t])].push_back(findhead(p[{t.first+1,t.second}]));
if(p.find({t.first-1,t.second})!=p.end()) from[findhead(p[t])].push_back(findhead(p[{t.first-1,t.second}]));
}
for(i=0;i<N;i++) if(val[i]) x = i;
all = N; now = 0;
memset(vis,0,sizeof(vis));
dfs(x,0);
memset(vis,0,sizeof(vis));
dfs2(x);
// printf("ans = %lld\n",ans);
}
int DistanceSum(int N,int* X,int* Y) {
ans = 0;
solve(N,X,Y,0);
solve(N,X,Y,1);
return ans/2;
}
Compilation message
city.cpp: In function 'void dfs(int, int)':
city.cpp:14:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<from[x].size();i++) {
^
city.cpp: In function 'void dfs2(int)':
city.cpp:27:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<from[x].size();i++) {
^
city.cpp: In function 'void solve(int, int*, int*, int)':
city.cpp:65:12: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
dfs2(x);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
6556 KB |
Output is correct |
2 |
Correct |
3 ms |
6556 KB |
Output is correct |
3 |
Correct |
3 ms |
6556 KB |
Output is correct |
4 |
Correct |
0 ms |
6556 KB |
Output is correct |
5 |
Correct |
0 ms |
6556 KB |
Output is correct |
6 |
Correct |
0 ms |
6556 KB |
Output is correct |
7 |
Correct |
3 ms |
6556 KB |
Output is correct |
8 |
Correct |
0 ms |
6556 KB |
Output is correct |
9 |
Correct |
0 ms |
6556 KB |
Output is correct |
10 |
Correct |
3 ms |
6556 KB |
Output is correct |
11 |
Correct |
0 ms |
6556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6688 KB |
Output is correct |
2 |
Correct |
3 ms |
6688 KB |
Output is correct |
3 |
Correct |
6 ms |
6688 KB |
Output is correct |
4 |
Correct |
6 ms |
6688 KB |
Output is correct |
5 |
Correct |
6 ms |
6688 KB |
Output is correct |
6 |
Correct |
3 ms |
6688 KB |
Output is correct |
7 |
Correct |
9 ms |
6688 KB |
Output is correct |
8 |
Correct |
3 ms |
6688 KB |
Output is correct |
9 |
Correct |
3 ms |
6688 KB |
Output is correct |
10 |
Correct |
6 ms |
6688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
8416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
49 ms |
8416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |