Gym 102346A. Artwork (并查集)

Posted 吃花椒的妙酱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym 102346A. Artwork (并查集)相关的知识,希望对你有一定的参考价值。

题目大意:给定一个矩形和圆心在矩形内的若干圆,求能否避开圆从左下角走到右下角。

思路:并查集

考虑无法走的情况

 如图四种情况,当上下、左右、左下、右下被联通时,则无法走到终点

每个圆看作点i,只要判断每个圆与四个边的相交情况和圆与圆之间的相交情况即可。

#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <list>
#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define scd(v) scanf("%d",&v)
#define scdd(a,b) scanf("%d %d",&a,&b)
#define endl "\\n"
#define IOS ios::sync_with_stdio(false)
#define pb push_back
#define all(v) v.begin(),v.end()
#define int long long
#define odd(x) x&1
#define mst(v,a) memset(v,a,sizeof(v))
#define lson p<<1 ,l,mid
#define rson p<<1|1,mid+1,r
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define inf 0x3f3f3f3f
const int N=1e5+10;
int n,m,k;
int xx[N],yy[N],zz[N];
int fa[N];
int find(int x){return fa[x] == x?x:find(fa[x]);}
void merge(int x, int y){fa[find(x)]=find(y);}
bool pd(int i ,int j)
{
    if( (zz[i] + zz[j])*(zz[i] + zz[j]) >= (xx[i]-xx[j])*(xx[i]-xx[j]) + (yy[i]-yy[j])*(yy[i]-yy[j])) return true;
    return false;
}
signed main()
{
    //!!
//   freopen("data.txt","r",stdin);
//    !!
    IOS;
    cin>>m>>n>>k;
    _for(i,0,k+10) fa[i]=i;
    _for(i,1,k)
    {
        int x,y,z;
        cin>>x>>y>>z;
        xx[i]=x;
        yy[i]=y;
        zz[i]=z;
        int up = y + z;
        int down = y - z;
        int left = x - z;
        int ri = x + z;
        if( up >= n && n >= down  ) merge(k+1,i);//up
        if( up >= 0 && 0 >= down ) merge(k+2,i);//down
        if( ri >= 0 && 0 >= left ) merge(k+3,i);//left
        if( ri >= m && m>= left ) merge(k+4,i);//right
    }
    _for(i,1,k)
    _for(j,i+1,k) if(pd(i,j)) merge(i,j);
    if( find(k+1)==find(k+2) || find(k+1)==find(k+4) || find(k+2) == find(k+3) || find(k+3) == find(k+4))
    {
        cout<<"N"<<endl;
    }
    else cout<<"S"<<endl;
}



以上是关于Gym 102346A. Artwork (并查集)的主要内容,如果未能解决你的问题,请参考以下文章

Gym - 101550A(Artwork 倒序+并查集)

Artwork Gym - 101550A

2019-2020 ACM-ICPC Brazil Subregional Programming Contest Problem A Artwork (并查集)

[Gym-102346M] 二分答案

Por Costel and the Match Gym - 100923H(经典种类并查集)

Half Nice Years Gym - 101840H (点分治 or 并查集)