#include <bits/stdc++.h>
using namespace std;
int n;
long long ans = 0, cnt = -1;
typedef pair<long long, long long> ii;
map<ii, bool> vis;
int sz[100005];
int deg[10005];
set<int> adj[100005];
queue<int> leaf;
void dfs(ii u, int p){
if(vis.find(u) == vis.end()) return;
if(vis[u]) return;
ii v = u;
cnt++;
vector<ii> arr;
while(vis.find(v) != vis.end()){
vis[v] = true;
arr.push_back(v);
v.first++;
}
v = ii(u.first-1,u.second);
while(vis.find(v) != vis.end()){
vis[v] = true;
arr.push_back(v);
v.first--;
}
int curcnt = cnt;
sz[curcnt] = arr.size();
if(p != -1){
adj[curcnt].insert(p);
adj[p].insert(curcnt);
deg[curcnt]++;
deg[p]++;
}
for(int i = 0;i < arr.size();i++){
ii u = arr[i];
dfs(ii(u.first,u.second+1),curcnt);
dfs(ii(u.first,u.second-1),curcnt);
}
}
void calculate(int *X, int *Y){
cnt = -1;
fill(sz,sz+n,0);
fill(deg,deg+n,0);
vis.clear();
vector<ii> arr;
for(int i = 0;i < n;i++){
vis[ii(X[i],Y[i])] = false;
adj[i].clear();
}
dfs(ii(X[0],Y[0]),-1);
for(int i = 0;;i++){
if(deg[i] == 0) break;
if(deg[i] == 1) leaf.push(i);
}
while(true){
int u = leaf.front();
leaf.pop();
if(adj[u].size() != 1) break;
int v = *adj[u].begin();
adj[v].erase(u);
ans += sz[u] * (n - sz[u]);
sz[v] += sz[u];
if(adj[v].size() == 1){
leaf.push(v);
}
}
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
calculate(X,Y);
calculate(Y,X);
return ans % 1000000000ll;
}
Compilation message
city.cpp: In function 'void dfs(ii, int)':
city.cpp:40:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < arr.size();i++){
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
6 ms |
4984 KB |
Output is correct |
11 |
Correct |
7 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5240 KB |
Output is correct |
4 |
Correct |
9 ms |
5240 KB |
Output is correct |
5 |
Correct |
10 ms |
5240 KB |
Output is correct |
6 |
Correct |
10 ms |
5320 KB |
Output is correct |
7 |
Correct |
10 ms |
5364 KB |
Output is correct |
8 |
Correct |
10 ms |
5244 KB |
Output is correct |
9 |
Correct |
10 ms |
5240 KB |
Output is correct |
10 |
Correct |
10 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
7288 KB |
Output is correct |
2 |
Correct |
58 ms |
7380 KB |
Output is correct |
3 |
Correct |
160 ms |
10288 KB |
Output is correct |
4 |
Correct |
164 ms |
10232 KB |
Output is correct |
5 |
Incorrect |
384 ms |
15944 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
7640 KB |
Output is correct |
2 |
Correct |
58 ms |
7620 KB |
Output is correct |
3 |
Incorrect |
145 ms |
11256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |