Problem:
Score : 800 points

Problem Statement

Takahashi has received an undirected graph with N vertices, numbered 1, 2, ..., N.
The edges in this graph are represented by (u_i, v_i).
There are no self-loops and multiple edges in this graph.

Based on this graph, Takahashi is now constructing a new graph with N^2 vertices, where each vertex is labeled with a pair of integers (a, b) (1 \leq a \leq N, 1 \leq b \leq N).
The edges in this new graph are generated by the following rule:

Span an edge between vertices (a, b) and (a', b') if and only if both of the following two edges exist in the original graph: an edge between vertices a and a', and an edge between vertices b and b'.

How many connected components are there in this new graph?

Constraints

2 \leq N \leq 100,000

0 \leq M \leq 200,000

1 \leq u_i < v_i \leq N

There exists no pair of distinct integers i and j such that u_i = u_j and v_i = v_j.

Input

The input is given from Standard Input in the following format:

N M
u_1 v_1
u_2 v_2
:
u_M v_M

Output

Print the number of the connected components in the graph constructed by Takahashi.

Sample Input 1

3 1
1 2

Sample Output 1

7

The graph constructed by Takahashi is as follows.

Sample Input 2

7 5
1 2
3 4
3 5
4 5
2 6

Sample Output 2

18

Solution in Kotlin (1.0.0) (judge verdict: Accepted)

import java.util.*

data class Component(val size: Int, val bipartite: Boolean)

// Reads multiple ints in a line, separate by a space.
// It works even if the actual number of ints is smaller than n.
// WARNING: This function does not work for -2^31.
// NOTE: This function does not handle error cases.
fun fastReadInts(n: Int): IntArray {
  val xs = IntArray(n)
  val s = readLine()!!
  var currentValue = 0
  var isNegative = false
  var currentIndex = 0
  for (c in s) {
    if (c == ' ') {
      xs[currentIndex] = currentValue * if (isNegative) -1 else 1
      currentValue = 0
      isNegative = false
      currentIndex++
    } else if (c == '-') {
      isNegative = true
    } else {
      currentValue *= 10
      currentValue += c - '0'
    }
  }
  xs[currentIndex] = currentValue * if (isNegative) -1 else 1
  return xs
}

data class BFSState(val v: Int, val c: Int)

fun main(args: Array<String>) {
  val (n, m) = fastReadInts(2)
  val graph = Array(n, { mutableListOf<Int>() })
  repeat(m) {
    val (v1Raw, v2Raw) = fastReadInts(2)
    val v1 = v1Raw - 1
    val v2 = v2Raw - 1
    graph[v1].add(v2)
    graph[v2].add(v1)
  }

  val colors = IntArray(n, { -1 })
  val components = mutableListOf<Component>()
  var numIsolated = 0
  for (s in 0 until n) {
    if (colors[s] >= 0) continue
    if (graph[s].isEmpty()) {
      colors[s] = 0
      numIsolated += 1
      continue
    }

    var componentSize = 0
    var isBipartite = true

    // BFS
    val queue = ArrayDeque<BFSState>()
    componentSize += 1
    colors[s] = 0
    queue.offerLast(BFSState(s, 0))
    while (queue.isNotEmpty()) {
      val state = queue.pollFirst()
      val v = state.v
      val c = state.c
      val nextC = (c + 1) % 2

      // Bipartite check
      for (u in graph[v]) {
        if (colors[u] >= 0 && colors[u] == c) {
          isBipartite = false
        }
      }

      for (u in graph[v]) {
        if (colors[u] >= 0) continue
        componentSize += 1
        colors[u] = nextC
        queue.offerLast(BFSState(u, nextC))
      }
    }

    components.add(Component(componentSize, isBipartite))
  }

//  println("components: " + components)
//  println("numIsolated: " + numIsolated)

  var ans = 0L
  
  val numBipartite = components.count { it.bipartite }
  // val numNonBipartite = components.count { !it.bipartite }
  ans += components.size.toLong() * components.size.toLong() +
      numBipartite.toLong() * numBipartite.toLong()

  ans += 2L * n.toLong() * numIsolated.toLong() - numIsolated.toLong() * numIsolated.toLong()

  println(ans)
}