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 Rust (1.15.1) (judge verdict: Accepted)

use std::collections::VecDeque;
use std::io::*;
use std::str::FromStr;

fn main() {
    let n: usize = read();
    let m: usize = read();
    let mut graph: Vec<Vec<usize>> = vec![vec![]; n];
    for _ in 0..m {
        let (a, b) = (read::<usize>() - 1, read::<usize>() - 1);
        graph[a].push(b);
        graph[b].push(a);
    }
    let mut used = vec![0; n];
    let mut zero: i64 = 0;
    let mut nibu: i64 = 0;
    let mut not_nibu: i64 = 0;
    for i in 0..n {
        if used[i] == 0 {
            if graph[i].len() == 0 {
                zero += 1;
                continue;
            }
            let mut que: VecDeque<usize> = VecDeque::new();
            que.push_back(i);
            let mut nibu_check = true;
            used[i] = 1;
            while !que.is_empty() {
                let q = que.pop_front().unwrap();
                for j in &graph[q] {
                    if used[*j] == 0 {
                        used[*j] = if used[q] == 1 { 2 } else { 1 };
                        que.push_back(*j);
                    }
                    if used[*j] == used[q] {
                        nibu_check = false;
                    }
                }
            }
            if nibu_check {
                nibu += 1;
            } else {
                not_nibu += 1;
            }
        }
    }
    let ans: i64 = zero * zero
        + zero * (n as i64 - zero) * 2
        + nibu * nibu * 2
        + not_nibu * nibu * 2
        + not_nibu * not_nibu;
    println!("{}", ans);
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}