

가중치가 있는 그래프

엣지 리스트는 구현하기가 쉽지만 엣지를 중심으로 구현되어 있기 때문에 특정 노드와 관련있는 엣지를 구하는데 효율적이지 않다.


인접 리스트는 ArrayList로 그래프를 표현. 노드 개수만큼 ArrayList를 선언
가중치가 없는 그래프
ArrayList<Integer>[5]로 선언한다. 
가중치가 있는 그래프
ArrayList<Node>[5]와 같이 표현class Node{
int (도착 노드);
int (가중치);
}


import java.util.ArrayList;
import java.util.Scanner;
public class Graph {
static boolean[] visited;
static boolean isEven;
static ArrayList<Integer>[] A;
static int[] check;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int K = input.nextInt();
for(int i=0; i<K; i++){
int V = input.nextInt();
int E = input.nextInt();
A = new ArrayList[V+1];
visited = new boolean[V+1];
isEven = true;
check = new int[V+1];
//노드 개수만큼 ArrayList생성
for(int j=1; j<=V; j++){
A[j] = new ArrayList<Integer>();
}
//엣지 데이터 저장
for(int j=0; j<E; j++){
int start = input.nextInt();
int end = input.nextInt();
A[start].add(end);
A[end].add(start);
}
//모든 노드에서 dfs실행
for(int j=1; j<=V; j++){
if(isEven){
dfs(j);
}else{
break;
}
}
if(isEven){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
public static void dfs(int start){
visited[start] = true;
for(int i : A[start]){
if(!visited[i]){
//직전 노드와 다른 집합으로 분류
check[i] = (check[start] + 1) % 2;
dfs(i);
}else if(check[start]==check[i]){
isEven = false;
}
}
}
}