#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
//간선 연결후 tree dp해보자
int sz[100005],num[100005],cnt;
ll anw,n;
map<pi,ll> mp;
vector<int> g[100005];
vector<pi> ar;
const ll mod=1e9;
ll dfs(ll a,ll p)
{
ll ret=sz[a];
for(int i=0;i<g[a].size();i++)
if(g[a][i]!=p)
ret+=dfs(g[a][i],a);
anw=(anw+ret*(n-ret))%mod;
return ret;
}
void add(ll a,ll b)
{
g[a].push_back(b),g[b].push_back(a);
}
void calc()
{
sort(ar.begin(),ar.end()); //점 0부터.
for(int i=0;i<=n;i++)
g[i].clear(),sz[i]=0;
cnt=0,mp.clear();
num[0]=++cnt,sz[cnt]=1;
for(int i=1;i<ar.size();i++)
{
if(ar[i-1].f==ar[i].f&&ar[i-1].s+1==ar[i].s) num[i]=cnt;
else num[i]=++cnt;
sz[cnt]++;
}
for(ll j=0,i=0;j<n;)
{
while(i<n&&j<n&&ar[i].f+1!=ar[j].f)
{
if(ar[i].f+1>ar[j].f) j++;
else i++;
}
while(i<n&&j<n&&ar[i].f+1==ar[j].f)
{
if(ar[i].s==ar[j].s)
{
if(!mp[{num[i],num[j]}])
add(num[i],num[j]),mp[{num[i],num[j]}]++;
i++,j++;
}
else if(ar[i].s>ar[j].s) j++;
else i++;
}
}
dfs(1,-1);
}
int DistanceSum (int N, int *x, int *y)
{
n=N;
for(int i=0;i<n;i++)
ar.push_back({x[i],y[i]});
calc();
for(int i=0;i<n;i++)
swap(ar[i].f,ar[i].s);
calc();
return anw;
}
Compilation message
city.cpp: In function 'll dfs(ll, ll)':
city.cpp:20:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[a].size();i++)
~^~~~~~~~~~~~
city.cpp: In function 'void calc()':
city.cpp:39:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<ar.size();i++)
~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
5 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3320 KB |
Output is correct |
2 |
Correct |
14 ms |
3416 KB |
Output is correct |
3 |
Correct |
27 ms |
4088 KB |
Output is correct |
4 |
Correct |
28 ms |
4148 KB |
Output is correct |
5 |
Correct |
52 ms |
5256 KB |
Output is correct |
6 |
Correct |
53 ms |
5232 KB |
Output is correct |
7 |
Correct |
56 ms |
5380 KB |
Output is correct |
8 |
Correct |
53 ms |
5264 KB |
Output is correct |
9 |
Correct |
54 ms |
5488 KB |
Output is correct |
10 |
Correct |
78 ms |
9584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3956 KB |
Output is correct |
2 |
Correct |
17 ms |
3772 KB |
Output is correct |
3 |
Correct |
42 ms |
5848 KB |
Output is correct |
4 |
Correct |
36 ms |
5108 KB |
Output is correct |
5 |
Correct |
88 ms |
8940 KB |
Output is correct |
6 |
Correct |
79 ms |
6640 KB |
Output is correct |
7 |
Correct |
85 ms |
9072 KB |
Output is correct |
8 |
Correct |
84 ms |
7004 KB |
Output is correct |
9 |
Correct |
70 ms |
6252 KB |
Output is correct |
10 |
Correct |
61 ms |
6128 KB |
Output is correct |