-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtractor.cpp
73 lines (66 loc) · 1.7 KB
/
tractor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <map>
#include <fstream>
using namespace std;
typedef pair<int,int> pii;
vector<pii> dsu;
map<int,vector<pii>> adj;
set<int,greater<int>> blocks;
vector<int> sizes;
int sch[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
bool good(int n, int i, int j) {
return (i>=0&&i<n&&j>=0&&j<n);
}
int find(int n) {
if (n != dsu[n].first)
return dsu[n].first = find(dsu[n].first);
return n;
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (dsu[a].second < dsu[b].second)
swap(a, b);
dsu[b].first = a;
if (dsu[a].second == dsu[b].second)
dsu[a].second++;
sizes[a] += sizes[b];
blocks.insert(sizes[a]);
}
}
int main() {
ifstream fin("tractor.in");
ofstream fout("tractor.out");
int n; fin >> n;
vector<vector<int>> grd(n, vector<int>(n));
for (auto& v : grd) for (int& i : v) fin >> i;
for (int i=0; i<n*n; i++) dsu.push_back({i, 0});
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
for (int s=0; s<4; s++) {
int ii = i+sch[s][0], jj = j+sch[s][1];
if (good(n, ii, jj)) {
adj[abs(grd[i][j]-grd[ii][jj])].push_back({i*n + j, ii*n + jj});
}
}
}
}
for (int i=0; i<n*n; i++) {
blocks.insert(1);
sizes.push_back(1);
}
int res = adj.begin()->first;
auto it = adj.begin();
while (*blocks.begin() < (n*n+1)/2) {
res = it->first;
for (pii pr : it->second) {
unite(pr.first, pr.second);
}
it++;
}
fout << res << '\n';
}