개발일지

[백준] 키 순서(2458) 본문

알고리즘

[백준] 키 순서(2458)

devbh 2020. 3. 19. 06:33



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

public class Main {
    static int N;
    static boolean[][] check;
    static int map[][];
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());//학생수
        int M = Integer.parseInt(st.nextToken());//비교횟수
        map = new int[N][N];
        check = new boolean[N][N];
        int answer = 0;
        for(int i=0;i<M;i++) {
            st = new StringTokenizer(br.readLine(), " ");
            map[Integer.parseInt(st.nextToken())-1][Integer.parseInt(st.nextToken())-1] = 1;
        }
        for(int i=0;i<N;i++) {
            check[i][i]=true;
            upCheck(i,i);
            downCheck(i,i);
        }
        for(int i=0;i<N;i++) {
            boolean flag = true;
            for(int j=0;j<N;j++) {
                if(!check[i][j]) {
                    flag = false;
                    break;
                }
            }
            if(flag) {
                answer++;
            }
        }
        System.out.println(answer);
    }
    static void upCheck(int n,int c) {
        for(int i=0;i<N;i++) {
            if(!check[i][c]&&map[n][i]==1) {
                check[i][c]=true;
                upCheck(i,c);
            }
        }
    }
    static void downCheck(int n,int c) {
        for(int i=0;i<N;i++) {
            if(!check[i][c]&&map[i][n]==1) {
                check[i][c]=true;
                downCheck(i,c);
            }
        }
    }
}

'알고리즘' 카테고리의 다른 글

[백준] 교수님은 기다리지 않는다(3830)  (0) 2020.03.19
[백준] 게임 개발(1516)  (0) 2020.03.19
[백준] LCA2(11438)  (0) 2020.03.03
[백준] 네트워크 연결 (1922)  (0) 2020.03.03
[백준] 줄 세우기(2252)  (0) 2020.03.03
Comments