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

황주영 / 10월 1주차 / 목 #267

Merged
merged 1 commit into from
Oct 12, 2023
Merged

Conversation

Hot-ttu
Copy link
Contributor

@Hot-ttu Hot-ttu commented Oct 5, 2023


🎈boj 00000 - 문제이름


🗨 해결방법 :


📝메모 :


✔코드 :

package BOJ;

import java.io.*;
import java.util.*;

public class boj02660 {
    private static int N;
    private static ArrayList<ArrayList<Integer>> graph;
    private static boolean[] visited;
    private static int[] score;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        N = Integer.parseInt(br.readLine());

        graph = new ArrayList<>();
        score = new int[N+1];

        for (int i = 0; i <= N; i++) {
            graph.add(new ArrayList<>());
        }

        while (true) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int A = Integer.parseInt(st.nextToken());
            int B = Integer.parseInt(st.nextToken());

            if (A == -1 && B == -1) {
                break;
            }

            graph.get(A).add(B);
            graph.get(B).add(A);
        }

        for (int i = 1; i < N+1; i++) {
            visited = new boolean[N+1];
            BFS(i);
        }

        StringBuilder sb = new StringBuilder();
        int cnt = 0;
        int minScore = Integer.MAX_VALUE;
        ArrayList<Integer> chairMan = new ArrayList<>();;
        for (int i = 1; i < N+1; i++) {
            if (minScore > score[i]) {
                minScore = score[i];
                cnt = 1;
                chairMan.clear();
                chairMan.add(i);
            } else if (minScore == score[i]) {
                cnt++;
                chairMan.add(i);
            }
        }

        sb.append(minScore).append(" ").append(cnt).append("\n");
        for (int i = 0; i < chairMan.size(); i++) {
            sb.append(chairMan.get(i)).append(" ");
        }
        System.out.println(sb.toString());
    } // end of main

    private static void BFS(int i) {
        Queue<Integer> queue = new ArrayDeque<>();

        queue.add(i);
        visited[i] = true;

        int cnt = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int j = 0; j < size; j++) {
                int v = queue.poll();

                ArrayList<Integer> tmp = graph.get(v);
                for (Integer integer : tmp) {
                    if (!visited[integer]) {
                        queue.add(integer);
                        visited[integer] = true;
                    }
                }

            }
            boolean flag = false;
            for (int k = 1; k < N+1; k++) {
                if (!visited[k]) {
                    flag = true;
                    break;
                }
            }

            cnt++;
            if (!flag) {
                score[i] = cnt;
                return;
            }
        }
    }
} // end of class

@Hot-ttu Hot-ttu requested review from ComelyU and devjy39 October 5, 2023 10:55
graph.get(B).add(A);
}

for (int i = 1; i < N+1; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i <= N 로 바꾸는 것을 추천드립니다!

@Hot-ttu Hot-ttu merged commit bb0dc68 into SSAFY-10th-Seoul17:main Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants