diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/785. Is Graph Bipartite?.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/785. Is Graph Bipartite?.java" new file mode 100644 index 00000000..92462a0f --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/785. Is Graph Bipartite?.java" @@ -0,0 +1,57 @@ +/* + +1. 아이디어 : 인접한 노드는 같은 그룹이 되면 안된다. bfs 로 모든 시작점을 탐색해서, 인접한 노드끼리 같은 그룹일 경우 false + +2. 시간복잡도 : O(N+E) + +3. 자료구조/알고리즘 : BFS + + */ + +class Solution { + private List> g = new ArrayList<>(); + private int n; + private int[] visited; + public boolean isBipartite(int[][] graph) { + + n = graph.length; + visited = new int[n+1]; + + for(int i=0; i()); + + for(int i=0; i dq = new ArrayDeque<>(); + + dq.add(start); + visited[start] = 1; + + while(!dq.isEmpty()) { + int num = dq.poll(); + for(int node : g.get(num)) { + if(visited[node] == 0) { + visited[node] = -visited[num]; + dq.add(node); + } + else if (visited[num] == visited[node]) return false; + } + } + + return true; + } +} \ No newline at end of file