Pagini recente » Cod sursa (job #2189930) | Cod sursa (job #2799196) | Cod sursa (job #3314905) | Cod sursa (job #1107067) | Cod sursa (job #2361706)
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define enter cout << '\n'
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef vector <ll> vl;
typedef vector <pll> vll;
typedef queue <int> qi;
typedef queue <pii> qii;
typedef queue <ll> ql;
typedef queue <pll> qll;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 9;
const double EPSILON = 1e-10;
const int NMAX = 16e3 + 5;
const int MMAX = 1e4 + 5;
const int DX[] = {0, 0, 1, -1};
const int DY[] = {1, -1, 0, 0};
ifstream fin("banana.in");
ofstream fout("banana.out");
int n, k;
int root[NMAX], rang[NMAX];
int m[MMAX][MMAX];
int find_root(int x)
{
int i, aux;
for (i = x; root[i] != i; i = root[i]);
while (root[x] != x)
{
aux = root[x];
root[x] = i;
x = aux;
}
return i;
}
void unite(int x, int y)
{
x = find_root(x);
y = find_root(y);
if (x == y) return;
root[y] = x;
rang[x] += rang[y];
rang[y] = 0;
}
int main()
{
fin >> n >> k;
for (int i = 1; i <= n; ++i)
{
int x, y, xn, yn;
fin >> x >> y;
m[x][y] = i;
root[i] = i;
rang[i] = 1;
for (int dir = 0; dir < 4; ++dir)
{
xn = x + DX[dir];
yn = y + DY[dir];
if (m[xn][yn])
unite(m[xn][yn], i);
}
}
sort(rang + 1, rang + n + 1, greater<int>());
int ans = 0;
for (int i = 1; i <= k; ++i)
ans += rang[i];
fout << ans;
return 0;
}