[技巧]ARubbish

套树会爆内存,sort PAIR 离散后暴搜染色

/*
	Zeolim - An AC a day keeps the bug away
*/

//#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp(x, y) make_pair(x, y) 
#define pii pair <int, int>
const int INF = 0x3f3f3f3f;
const ld PI = acos(-1.0);
const ld E = exp(1.0);

const int MAXN = 1e6 + 10;

pii ST[MAXN];

int n, x, y, ans = 0;

int coler[MAXN] = {0};

pii now;

void dfs(int x, int y)
{
	for(int i = -1; i <= 1; ++i)
	{
		for(int j = -1; j <= 1; ++j)
		{
			now = mp(x + i, y + j);

			int it = lower_bound(ST, ST + n, now) - ST;

			if(ST[it] == now && !coler[it])
			{
				coler[it] = ans;
				int fst = ST[it].first, lst = ST[it].second;
				dfs(fst, lst);
			}
		}
	}
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);     cout.tie(0);
    //freopen("D://test.in", "r", stdin);
    //freopen("D://test.out", "w", stdout);

	cin >> n;

	for(int i = 0; i < n; ++i)
	{
		cin >> ST[i].first >> ST[i].second;
	}

	sort(ST, ST + n);

	for(int i = 0; i < n; ++i)
	{
		if(!coler[i])
		{
			++ans;
			coler[i] = ans;
			int fst = ST[i].first, lst = ST[i].second;
			dfs(fst, lst);
		}
	}	

	cout << ans << '
';


    return 0;
}
原文地址:https://www.cnblogs.com/zeolim/p/12270359.html