https://www.acmicpc.net/problem/1707
1707λ²: μ΄λΆ κ·Έλν
μ λ ₯μ μ¬λ¬ κ°μ ν μ€νΈ μΌμ΄μ€λ‘ ꡬμ±λμ΄ μλλ°, 첫째 μ€μ ν μ€νΈ μΌμ΄μ€μ κ°μ Kκ° μ£Όμ΄μ§λ€. κ° ν μ€νΈ μΌμ΄μ€μ 첫째 μ€μλ κ·Έλνμ μ μ μ κ°μ Vμ κ°μ μ κ°μ Eκ° λΉ μΉΈμ μ¬μ΄μ
www.acmicpc.net
μ΄λΆκ·Έλν
μΈμ ν μ μ λΌλ¦¬ μλ‘ λ€λ₯Έ μμΌλ‘ μΉ ν΄μ λͺ¨λ μ μ μ λ κ°μ§ μμΌλ‘λ§ μΉ ν μ μλ κ·Έλν.
νμ΄λ₯Ό μν μμ΄λμ΄ : μ΄λΆκ·Έλνμ νλμ κ°μ μ μ°κ²°λ λ λ Έλμ μμμ μλ‘ λ¬λΌμΌ νλ€.
λͺ¨λ λ Έλμ λν΄ bfsλ₯Ό μννλ©΄μ μ°κ²°λ λ Έλμ μλ‘ μμμ΄ λ€λ₯Έμ§ νμΈν΄μΌ νλ€.
λ³μ
λ°©λ¬Έμ²λ¦¬μ μμνμΈμ λμμ μννλ int[] chk λ°°μ΄μ λλ€.
- -1 : μμ§ λ°©λ¬Ένμ§ μμ λ Έλ
- 0 : λΉ¨κ°μ
- 1 : νλμ
νμ΄μμ
1. λͺ¨λ λ Έλμ λν΄ λ°λ³΅λ¬Έμ μννλ©° λ°©λ¬Ένμ§ μμμΌλ©΄ bfsν¨μλ₯Ό μν
static boolean solve(){
for(int i=1;i<V+1;i++){
if(chk[i]==-1){
if(!bfs(i)) return false;
}
}
return true;
}
chk[i]κ° -1μΈ κ²½μ°(μμ§ λ°©λ¬Ένμ§ μμ λ Έλ)μ λν΄ bfsν¨μλ₯Ό μννλ κ²μ λ³Ό μ μλ€.
2. bfs μμμ μ 0(λΉ¨κ°μ)μΌλ‘ μμΉ νλ€. 1(νλμ)μΌλ‘ ν΄λ λ¨. μΈμ ν λ Έλμ λν΄μ λ€λ₯Έ μμμ μΉ νκΈ°
static boolean bfs(int node){
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[]{node,0});
chk[node]=0;
while(!q.isEmpty()){
int[] c = q.pollFirst();
int n = c[0];
int color = c[1];
for(int i : graph[n]){
if(chk[i]==color){
return false;
}
if(chk[i]==-1){
chk[i] = (color + 1)%2;
q.offer(new int[]{i,chk[i]});
}
}
}
return true;
}
μλ‘ λ€λ₯Έ μμμ μΉ νκΈ° μν΄ λͺ¨λλ¬ μ°μ°μ μ¬μ©νλ€.
0(λΉ¨κ°μ)μ΄λ©΄ 1(νλμ)
1(νλμ)μ΄λ©΄ 0(λΉ¨κ°μ)
μ°κ²°λ λ Έλμ μμ(chk[i])κ° νμ¬ μμ(color)μ κ°μΌλ©΄ μ΄λΆ κ·Έλνλ₯Ό νμ±ν μ μμΌλ―λ‘ falseλ₯Ό 리ν΄νλ€.
μ 체μ½λ
package com.mincheolsong;
import java.io.*;
import java.util.*;
public class Main {
static int T;
static int V,E;
static List<Integer>[] graph;
static int[] chk;
static boolean bfs(int node){
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[]{node,0});
chk[node]=0;
while(!q.isEmpty()){
int[] c = q.pollFirst();
int n = c[0];
int color = c[1];
for(int i : graph[n]){
if(chk[i]==color){
return false;
}
if(chk[i]==-1){
chk[i] = (color + 1)%2;
q.offer(new int[]{i,chk[i]});
}
}
}
return true;
}
static boolean solve(){
for(int i=1;i<V+1;i++){
if(chk[i]==-1){
if(!bfs(i)) return false;
}
}
return true;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
T = Integer.parseInt(br.readLine());
for(int t=0;t<T;t++){
st = new StringTokenizer(br.readLine());
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
graph = new List[V+1];
chk = new int[V+1];
Arrays.fill(chk,-1);
for(int i=1;i<V+1;i++){
graph[i] = new ArrayList<>();
}
for(int i=0;i<E;i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
graph[a].add(b);
graph[b].add(a);
}
if(solve()){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
'πμ½λ©ν μ€νΈ:CodingTest' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ΄λΆνμ (0) | 2024.03.06 |
---|---|
visitedλ°°μ΄μ΄ 3μ°¨μμΈ bfs μ΅λ¨κ²½λ‘ νμ λ¬Έμ (λ°±μ€ 1600λ²) (2) | 2024.02.28 |
DFS + DP (λ°±μ€ 1520 λ΄λ¦¬λ§κΈΈ) (0) | 2024.02.20 |
ꡬν, λ°±νΈλνΉ (15684 μ¬λ€λ¦¬ μ‘°μ) (0) | 2024.02.16 |
λ°±νΈλνΉ(14890 κ²½μ¬λ‘) (2) | 2024.02.14 |