【Atcoder D - Circumferences】并查集

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int[] rank;
    static int[] parent;

    public static void init(int n) {
        rank = new int[n];
        parent = new int[n];
        for (int i = 0; i < n; i++) {
            rank[i] = 1;
            parent[i] = i;
        }
    }

    public static int find(int x) {
        if (parent[x] == x) {
            return x;
        }
        return find(parent[x]);
    }

    public static void union(int x,int y){
        int rx = find(x);
        int ry = find(y);

        if( rx == ry){
            return;
        }
        if( rank[rx] >= rank[ry]){
            rank[rx] += rank[ry];
            parent[ry] = rx;
        }else{
            rank[ry] += rank[rx];
           parent[rx] = ry;
        }

    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        long sx, sy, tx, ty;
        int n = Integer.valueOf(br.readLine());
        String[] words = br.readLine().split("\\s+");
        sx = Long.valueOf(words[0]);
        sy = Long.valueOf(words[1]);
        tx = Long.valueOf(words[2]);
        ty = Long.valueOf(words[3]);
        long[][] circles = new long[n][3];
        for (int i = 0; i < n; i++) {
            words = br.readLine().split("\\s+");
            long x = Long.valueOf(words[0]);
            long y = Long.valueOf(words[1]);
            long r = Long.valueOf(words[2]);
            circles[i][0] = x;
            circles[i][1] = y;
            circles[i][2] = r;
        }

        init(n);

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                long[] arr = circles[i];
                long[] arr2 = circles[j];

                long distance = (arr[0] - arr2[0]) * (arr[0] - arr2[0]) + (arr[1] - arr2[1]) * (arr[1] - arr2[1]);
                long dis2 = (arr[2] + arr2[2]) * (arr[2] + arr2[2]);
                long dis3 = (arr[2] - arr2[2]) * (arr[2] - arr2[2]);
                if( !(distance > dis2) && !(distance < dis3) ){
                    union(i,j);
                }
            }
        }
        //查找在哪个圆上
        int srcIndex = 0;
        int dstIndex = 0;
        for(int i=0;i<circles.length;i++){
            long[] arr = circles[i];
            long x = arr[0];
            long y = arr[1];
            long r = arr[2];

            if( ( (sx-x)*(sx-x) + (sy-y)*(sy-y)) == r*r){
                srcIndex = i;
            }
            if( ( (tx-x)*(tx-x) + (ty-y)*(ty-y)) == r*r){
                dstIndex = i;
            }
        }
        //
        if( find(srcIndex) == find(dstIndex)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }

        br.close();
    }

}

 

posted @ 2022-07-09 22:14  fishcanfly  阅读(72)  评论(0)    收藏  举报
//雪花飘落效果