zoukankan      html  css  js  c++  java
  • P3958 [NOIP2017 提高组] 奶酪(并查集)

    题目链接

    思路:

    将相交或相切的用并查集维护起来,最后看上表面跟下表面能否在同一个连通块。

    代码:

    
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    const int maxn=5e5+100;
    ll n,h,r;
    struct node{
    	ll x,y,z;	
    }a[maxn];
    
    ll cul(node a,node b){
    	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
    }
    
    int root[maxn];
    
    int Find(int x){
    	if(x!=root[x]) return root[x]=Find(root[x]);
    	return root[x];
    }
    
    int main(){
    	int T=read;
    	while(T--){
    		n=read,h=read,r=read;
    		rep(i,1,n){
    			a[i].x=read,a[i].y=read,a[i].z=read;
    		}
    		rep(i,1,n+2) root[i]=i;
    		rep(i,1,n){
    			if((a[i].z-r)<=0){
    				int fx=Find(i),fy=Find(n+1);
    				if(fx!=fy) root[fx]=fy;
    			}
    			if((a[i].z+r)>=h){
    				int fx=Find(i),fy=Find(n+2);
    				if(fx!=fy) root[fx]=fy;
    			}
    			rep(j,i+1,n){
    				if(cul(a[i],a[j])<=(2*r)*(2*r)){
    					int fx=Find(i),fy=Find(j);
    					if(fx!=fy) root[fx]=fy;
    				}
    			}
    		}
    		if(Find(n+1)==Find(n+2)) puts("Yes");
    		else puts("No");
    	}
    	return 0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    hdu 5387 Clock (模拟)
    CodeForces 300B Coach (并查集)
    hdu 3342 Legal or Not(拓扑排序)
    hdu 3853 LOOPS(概率DP)
    hdu 3076 ssworld VS DDD(概率dp)
    csu 1120 病毒(LICS 最长公共上升子序列)
    csu 1110 RMQ with Shifts (线段树单点更新)
    poj 1458 Common Subsequence(最大公共子序列)
    poj 2456 Aggressive cows (二分)
    HDU 1869 六度分离(floyd)
  • 原文地址:https://www.cnblogs.com/OvOq/p/14864701.html
Copyright © 2011-2022 走看看