其他分享
首页 > 其他分享> > AtCoder-abc259_d Circumferences

AtCoder-abc259_d Circumferences

作者:互联网

Circumferences

并查集

找到点所在的两个圆,然后将所有的圆互相判断一下是否相交,如果相交则并查集连起来,注意同心圆的情况

最后判断所在的两个圆是否连通

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
#include <array>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 3010;
const ll inf = 1e17 + 10;
const double pi = acos(-1.0);
int top[maxn];

struct node
{
    ll x, y, r;
}p[maxn];

ll dis(node a, node b)
{
    ll xx = a.x - b.x;
    ll yy = a.y - b.y;
    return xx * xx + yy * yy;
}

int query(int x)
{
    return x == top[x] ? x : top[x] = query(top[x]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll sx, sy, ex, ey;
    int n;
    cin >> n >> sx >> sy >> ex >> ey;
    for(int i=0; i<n; i++) top[i] = i;
    int f = 0;
    int sa = 0, sb = 0;
    for(int i=0; i<n; i++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        p[i] = {a, b, c};
        int temp = 0;
        if(dis(p[i], node{sx, sy, 0}) == p[i].r * p[i].r)
            sa = i;
        if(dis(p[i], {ex, ey, 0}) == p[i].r * p[i].r)
            sb = i;
    }
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(dis(p[i], p[j]) <= (p[i].r + p[j].r) * (p[i].r + p[j].r))
            {
                if(p[i].x == p[j].x && p[i].y == p[j].y && p[i].r != p[j].r) continue;
                int a = query(i), b = query(j);
                if(a != b)
                {
                    top[a] = b;
                }
            }
        }
    }
    if(query(sa) == query(sb)) f = 1;
    if(f) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

标签:node,AtCoder,int,ll,Circumferences,ex,abc259,include,top
来源: https://www.cnblogs.com/dgsvygd/p/16466851.html