zoukankan      html  css  js  c++  java
  • [技巧]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;
    }
  • 相关阅读:
    Linux 命令笔记
    MySQL指令笔记
    悲观锁与乐观锁
    缓存在高并发场景下的常见问题
    死锁相关问题
    Java并发性和多线程
    Java同步和异步,阻塞和非阻塞
    内存溢出和内存泄漏
    JavaAndroid项目配置文件笔记
    Maven安装配置
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270359.html
Copyright © 2011-2022 走看看