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] 4803 트리 #119

Merged
merged 1 commit into from
Nov 25, 2024
Merged

[G4] 4803 트리 #119

merged 1 commit into from
Nov 25, 2024

Conversation

wwan13
Copy link
Owner

@wwan13 wwan13 commented Nov 24, 2024

Problem Info

Code

package boj4803;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static int[] parents;
    private static boolean[] hasCycle;

    public static void main(String[] args) {
        int t = 1;

        while (true) {
            String line = readLine();
            if (line.equals("0 0")) {
                break;
            }

            String[] nm = line.split(" ");
            int n = Integer.parseInt(nm[0]);
            int m = Integer.parseInt(nm[1]);

            parents = new int[n + 1];
            hasCycle = new boolean[n + 1];
            for (int i = 1; i <= n; i++) {
                parents[i] = i;
            }

            for (int i = 0; i < m; i++) {
                String[] elements = readLine().split(" ");
                int a = Integer.parseInt(elements[0]);
                int b = Integer.parseInt(elements[1]);
                if (find(a) == find(b)) {
                    hasCycle[find(a)] = true;
                } else {
                    union(a, b);
                }
            }

            Set<Integer> roots = new HashSet<>();
            int treeCount = 0;
            for (int i = 1; i <= n; i++) {
                int root = find(i);
                if (!roots.contains(root)) {
                    roots.add(root);
                    if (!hasCycle[root]) {
                        treeCount++;
                    }
                }
            }

            if (treeCount == 0) {
                System.out.println("Case " + t + ": No trees.");
            } else if (treeCount == 1) {
                System.out.println("Case " + t + ": There is one tree.");
            } else {
                System.out.println("Case " + t + ": A forest of " + treeCount + " trees.");
            }

            t++;
        }
    }

    private static void union(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            parents[rootY] = 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 dfs 알고리즘: dfs disjoint_set 알고리즘: disjoint_set graph_traversal 알고리즘: graph_traversal graphs 알고리즘: graphs trees 알고리즘: trees labels Nov 24, 2024
@github-actions github-actions bot changed the title solve : 4803 트리 [G4] 4803 트리 Nov 24, 2024
@wwan13 wwan13 merged commit 7612539 into main Nov 25, 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 dfs 알고리즘: dfs disjoint_set 알고리즘: disjoint_set graph_traversal 알고리즘: graph_traversal graphs 알고리즘: graphs trees 알고리즘: trees
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant