이분그래프
https://www.acmicpc.net/problem/17834
import java.io.*;
import java.util.*;
public class Main {
static boolean[] visited;
static int[] colors;
static ArrayList<Integer>[] nodes;
static boolean isPossible = true;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
nodes = new ArrayList[n + 1];
visited = new boolean[n + 1];
colors = new int[n + 1];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
nodes[u].add(v);
nodes[v].add(u);
}
dfs(1, n);
if (isPossible) {
long cnt1 = 0;
long cnt2 = 0;
for (int i = 1; i <= n; i++) {
if (colors[i] % 2 == 0) {
cnt1++;
} else {
cnt2++;
}
}
System.out.println(cnt1 * cnt2 * 2);
} else {
System.out.println(0);
}
}
public static void dfs(int node, int n) {
if (!isPossible) {
return ;
}
visited[node] = true;
for (Integer nextNode : nodes[node]) {
if (!visited[nextNode]) {
colors[nextNode] = (colors[node] + 1) % 2;
dfs(nextNode, n);
} else {
if (colors[node] == colors[nextNode]) {
isPossible = false;
return ;
}
}
}
}
}
2시간