Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[G4] 10216 Count Circle Groups #117

Merged
merged 1 commit into from
Nov 23, 2024
Merged

[G4] 10216 Count Circle Groups #117

merged 1 commit into from
Nov 23, 2024

Conversation

wwan13
Copy link
Owner

@wwan13 wwan13 commented Nov 23, 2024

Problem Info

Code

package boj10216;

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

public class Main {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    static class Enemy {
        final int x;
        final int y;
        final int range;

        Enemy(int x, int y, int range) {
            this.x = x;
            this.y = y;
            this.range = range;
        }
    }

    private static int[] parents;

    public static void main(String[] args) {
        int t = Integer.parseInt(readLine());

        while (t-- > 0) {
            int n = Integer.parseInt(readLine());

            parents = new int[n];
            Enemy[] enemies = new Enemy[n];
            for (int i = 0; i < n; i++) {
                String[] line = readLine().split(" ");
                int x = Integer.parseInt(line[0]);
                int y = Integer.parseInt(line[1]);
                int range = Integer.parseInt(line[2]);

                enemies[i] = new Enemy(x, y, range);
                parents[i] = i;
            }

            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (isConnected(enemies[i], enemies[j])) {
                        union(i, j);
                    }
                }
            }

            int count = 0;
            for (int i = 0; i < n; i++) {
                if (parents[i] == i) {
                    count += 1;
                }
            }

            System.out.println(count);
        }
    }

    private static boolean isConnected(Enemy e1, Enemy e2) {
        int dx = e2.x - e1.x;
        int dy = e2.y - e1.y;
        int range = e1.range + e2.range;
        return (dx * dx + dy * dy) <= range * range;
    }

    private static void union(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            parents[rootY] = parents[rootX];
        }
    }

    private static int find(int x) {
        if (parents[x] != x) {
            parents[x] = find(parents[x]);
        }
        return parents[x];
    }

    private static String readLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Note

@github-actions github-actions bot added data_structures 알고리즘: data_structures disjoint_set 알고리즘: disjoint_set geometry 알고리즘: geometry graph_traversal 알고리즘: graph_traversal graphs 알고리즘: graphs labels Nov 23, 2024
@github-actions github-actions bot changed the title solve : 10216 Count Circle Groups [G4] 10216 Count Circle Groups Nov 23, 2024
@wwan13 wwan13 merged commit 4a6f422 into main Nov 23, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data_structures 알고리즘: data_structures disjoint_set 알고리즘: disjoint_set geometry 알고리즘: geometry graph_traversal 알고리즘: graph_traversal graphs 알고리즘: graphs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant