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 <bits/stdc++.h>
using namespace std;
typedef int INT;
#define pb push_back
#define int long long
#define fst first
#define snd second
#define FOR(i,l,r) for(int i = (l); i < (r); i++)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int N;
vii points;
vvi adj;
map<ii, int> idx;
vvi rowgr, colgr;
vi rowsz, colsz;
vi row, col;
vi vis;
void flood(int u, int c, bool co){
vis[u] = 1;
if(co) col[u] = c, colsz[c] ++;
else row[u] = c, rowsz[c] ++;
for(int i = co; i < 4; i+= 2) if(adj[u][i] != -1 and !vis[adj[u][i]]) flood(adj[u][i], c, co);
for(int i = 1-co; i < 4; i+= 2) if(adj[u][i] != -1 and vis[adj[u][i]]) {
int v = adj[u][i];
if(v == -1) continue;
if(co) colgr[c].pb(col[v]), colgr[col[v]].pb(c);
else rowgr[c].pb(row[v]), rowgr[row[v]].pb(c);
}
}
map<ii, int> dp[2];
int dfs(int u, int p, bool col){
int &res = dp[col][{u, p}];
if(res != 0) return res;
res = col ? colsz[u] : rowsz[u];
for(int v : (col ? colgr[u] : rowgr[u]) ) if(v != p) res += dfs(v, u, col);
return res;
}
int gettot(int u, int tot){
//cout << u << " " << tot << endl;
int res = tot;
vis[u] = 1;
FOR(i, 0, 4){
int v = adj[u][i];
if(v == -1 || vis[v]) continue;
if(i % 2 == 0) { res += gettot(v, tot - 2*dfs(col[v], col[u], 1) + N); }
else res += gettot(v, tot - 2*dfs(row[v], row[u], 0) + N);
}
return res;
}
int bfs(int u){
vis.assign(N, 0);
int total = 0;
queue<ii> q;
q.push({u, 0});
vis[u] = 1;
while(!q.empty()){
ii p = q.front(); q.pop();
total += p.snd;
for(int v : adj[p.fst])if(v !=-1 and !vis[v]){
q.push({v,p.snd+1});
vis[v] = 1;
}
}
vis.assign(N, 0);
return total;
}
INT DistanceSum(INT n, INT *X, INT *Y) {
N = n;
adj.assign(N, vi(4, -1));
FOR(i, 0, N) {points.pb({X[i], Y[i]}); idx[{X[i], Y[i]}] = i+1;}
FOR(i, 0, N){
int x = X[i], y = Y[i];
int neigh;
if(neigh = idx[{x+1, y}]) adj[i][0] = neigh-1;
if(neigh = idx[{x, y-1}]) adj[i][1] = neigh-1;
if(neigh = idx[{x-1, y}]) adj[i][2] = neigh-1;
if(neigh = idx[{x, y+1}]) adj[i][3] = neigh-1;
}
idx.clear();
int k = 0;
row.assign(N, -1);
col.assign(N, -1);
vis.assign(N, 0);
FOR(i, 0, N) if(!vis[i]){
rowgr.pb(vi(0));
rowsz.pb(0);
flood(i, k++,0);
}
for(auto &v : rowgr){
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
k = 0;
vis.assign(N, 0);
FOR(i, 0, N) if(!vis[i]){
colgr.pb(vi(0));
colsz.pb(0);
flood(i, k++,1);
}
vis.assign(N, 0);
for(auto &v : colgr){
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
/*
for(auto &v : adj) {
for(int k : v) cout << k << " ";
cout<<endl;
}
for(auto &v : colgr) {
for(int k : v) cout << k << " ";
cout<<endl;
}
cout<<endl;
for(auto &v : rowgr) {
for(int k : v) cout << k << " ";
cout<<endl;
}
for(int k : col) cout << k << " ";
cout<<endl;
for(int k : row) cout << k << " ";
cout<<endl;
cout << dfs(1, 0, 0) << endl;
cout << bfs(0) <<endl;
*/
return (gettot(0, bfs(0))/2) % 1000000000;
}
Compilation message (stderr)
city.cpp: In function 'INT DistanceSum(INT, INT*, INT*)':
city.cpp:89:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x+1, y}]) adj[i][0] = neigh-1;
city.cpp:90:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x, y-1}]) adj[i][1] = neigh-1;
city.cpp:91:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x-1, y}]) adj[i][2] = neigh-1;
city.cpp:92:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x, y+1}]) adj[i][3] = neigh-1;
# | 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... |