连接格点
作者:
Lemmon_kk
,
2020-07-20 18:09:10
,
所有人可见
,
阅读 522
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010, M = 1e6;
int n, m, res;
int p[N];
int ids[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int w[4] = {1, 1, 2, 2};
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
void solve(){
for(int k = 0;k < 4;k ++ )
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ ){
int x = dx[k] + i, y = dy[k] + j;
int a = ids[i][j], b = ids[x][y];
a = find(a), b = find(b);
if(a != b){
p[a] = b;
res += w[k];
}
}
}
int main(){
cin >> n >> m;
for(int i = 1,t = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ ,t ++ )
ids[i][j] = t;
for(int i = 1;i <= n * m;i ++ ) p[i] = i;
int x1, y1, x2, y2;
while(cin >> x1 >> y1 >> x2 >> y2){
int a = ids[x1][y1], b = ids[x2][y2];
p[find(a)] = find(b);
}
solve();
cout << res << endl;
return 0;
}