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

호석이두마리치킨 #96

Closed
dduckmane opened this issue Feb 11, 2023 · 1 comment
Closed

호석이두마리치킨 #96

dduckmane opened this issue Feb 11, 2023 · 1 comment

Comments

@dduckmane
Copy link

dduckmane commented Feb 11, 2023

  • 저는 호석이두마리치킨 문제를 강의시간에 배운 조합 + multisourceBfs 로 풀었습니다.
  • 그 이유는 집을 2개 선택할 수 있고 그 선택된 2개에 대해서 multisourceBfs 를 하면
  • 각각의 시작점에서의 최단거리가 나와서 이문제를 위의 로직대로 풀었습니다.
  • 그리고 그 때의 시간복잡도도 (100개중 2개고르는 시간) * (O(m)) 이라고 생각했습니다.
  • 하지만 문제에서 부분점수만 받았습니다.
  1. 그 이유가 궁금합니다.

  2. 그리고 강사님은 조합을 하셨는데 시간복잡도를 n의 제곱이라고 설명하셨습니다.
    image

  • 조합은 nC2 인데 n의 제곱과는 차이가 많다고 생각합니다.
  • 조합의 시간 복잡도는 n의 제곱인지 nC2인지 궁금합니다.
package codingTest.two;
import java.io.*;
import java.util.*;

public class 호석이두마리치킨 {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static class Ans implements Comparable<Ans> {
        public int a;
        public int b;

        public Ans(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public int compareTo(Ans o) {
            if (this.a != o.a) return this.a - o.a;
            return this.b - o.b;
        }
    }

    static int N;
    static int M;
    static ArrayList<Integer>[] adj;
    static int[] d;
    static boolean[] visit;
    static int[] sel;
    static ArrayList<Ans> ansArrayList = new ArrayList<>();

    static void input() {
        N = scan.nextInt();
        M = scan.nextInt();

        adj = new ArrayList[N + 1];
        visit = new boolean[N + 1];
        d = new int[N + 1];
        sel = new int[4];
        for (int i = 1; i <= N; i++) adj[i] = new ArrayList<>();

        for (int i = 0; i < M; i++) {
            int x = scan.nextInt();
            int y = scan.nextInt();

            adj[x].add(y); adj[y].add(x);
        }
    }
    static int ans = Integer.MAX_VALUE;
    static void bruteForce (int x, int cnt) {
        if (cnt == 3) {
            if (ans >= bfs(sel[1], sel[2])) {
                ans = bfs(sel[1], sel[2]);

                if (sel[1] < sel[2]) {
                    ansArrayList.add(new Ans(sel[1], sel[2]));
                } else {
                    ansArrayList.add(new Ans(sel[2], sel[1]));
                }
            }
            return;
        }
        for (int i = x + 1; i <= N; i++) {
            sel[cnt] = i; cnt += 1;
            bruteForce(i , cnt);
            sel[cnt] = 0; cnt -= 1;
        }
    }

    static int bfs(int x, int y) {
        Queue<Integer> queue = new LinkedList<>();
        //init
        queue.add(x); queue.add(y);

        for (int i = 1; i <= N; i++) {
            visit[i] = false;
            d[i] = 0;
        }
        visit[x] = true; visit[y] = true;

        // start
        while (! queue.isEmpty()) {
            int p = queue.poll();

            for (Integer z : adj[p]) {
                if (visit[z]) continue;

                queue.add(z);
                visit[z] = true;
                d[z] = d[p] + 1;
            }
        }

        int sum = 0;
        for (int i = 1; i <= N; i++) {
            sum += d[i];
        }
        return sum * 2;
    }

    public static void main(String[] args) {
        input();
        bruteForce(0 , 1);
        Collections.sort(ansArrayList);

        Ans ans1 = ansArrayList.get(0);
        sb.append(ans1.a).append(' ').append(ans1.b).append(' ');
        sb.append(ans);

        System.out.println(sb);
    }


    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

@github-actions
Copy link

Stale issue message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant