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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    public Solve(){}
    List<int>[] G;
    bool[] us;
    int[] b;
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        int N = int.Parse(str[0]);
        int M = int.Parse(str[1]);
        UnionFind U = new UnionFind(N);
        b = new int[N];
        G = new List<int>[N];
        for(int i=0;i<N;i++){
            G[i] = new List<int>();
        }
        for(int i=0;i<M;i++){
            str = Console.ReadLine().Split(' ');
            int x = int.Parse(str[0])-1;
            int y = int.Parse(str[1])-1;
            G[x].Add(y);
            G[y].Add(x);
            U.Union(x,y);
        }
        us = new bool[N];
        for(int i=0;i<N;i++){
            if(!us[i]){
                if(!dfs(i,true)){
                    b[i] = 3;
                }
            }
        }
        us = new bool[N];
        for(int i=0;i<N;i++){
            if(!us[i] && b[i] == 3){
                dfs2(i);
            }
        }
        int[] par = U.par;
        int[] parN = new int[N];
        int[] paro = new int[N];
        int[] pare = new int[N];
        for(int i=0;i<N;i++){
            if(parN[par[i]] == 0){
                parN[par[i]] = i+1;
            }
            if(b[i] == 1){
                if(paro[par[i]] == 0){
                    paro[par[i]] = i+1;
                }
            }
            if(b[i] == 2){
                if(pare[par[i]] == 0){
                    pare[par[i]] = i+1;
                }
            }
        }
        for(int i=0;i<N;i++){
            if(parN[i] == 0){
                parN[i] = N+1;
            }
            if(paro[i] == 0){
                paro[i] = N+1;
            }
            if(pare[i] == 0){
                pare[i] = N+1;
            }
        }
        int eo = 0;
        int e = 0;
        int o = 0;
        for(int i=0;i<N;i++){
            if(b[i] == 1){
                if(pare[par[i]] != N+1){
                    o++;
                }
                if(paro[par[i]] != N+1){
                    e++;
                }
                if(pare[par[i]] != N+1 && paro[par[i]] != N+1){
                    eo++;
                }
            }
            else if(b[i] == 2){
                if(pare[par[i]] != N+1){
                    e++;
                }
                if(paro[par[i]] != N+1){
                    o++;
                }
                if(pare[par[i]] != N+1 && paro[par[i]] != N+1){
                    eo++;
                }
            }
            else{
                if(parN[par[i]] != N+1){
                    eo++;
                    e++;
                    o++;
                }
            }
        }
        long count = 0;
        for(int i=0;i<N;i++){
            if(b[i] == 1){
                if(pare[par[i]] < i+1 && paro[par[i]] < i+1){
                    count += N-eo;
                }
                else{
                    if(pare[par[i]] < i+1){
                        count += N-o;
                    }
                    else if(paro[par[i]] < i+1){
                        count += N-e;
                    }
                    else{
                        count += N;
                    }
                }
            }
            else if(b[i] == 2){
                if(pare[par[i]] < i+1 && paro[par[i]] < i+1){
                    count += N-eo;
                }
                else{
                    if(pare[par[i]] < i+1){
                        count += N-e;
                    }
                    else if(paro[par[i]] < i+1){
                        count += N-o;
                    }
                    else{
                        count += N;
                    }
                }
            }
            else{
                if(parN[par[i]] < i+1){
                    count += N-eo;
                }
                else{
                    count += N;
                }
            }
        }
        sb.Append(count+"\n");
    }
    bool dfs(int v,bool c){
        us[v] = true;
        if(c){
            b[v] = 1;
        }
        else{
            b[v] = 2;
        }
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!us[t]){
                if(!dfs(t,!c)){
                    return false;
                }
            }
            else if(b[v] == b[t]){
                return false;
            }
        }
        return true;
    }
    void dfs2(int v){
        us[v] = true;
        b[v] = 3;
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!us[t]){
                dfs2(t);
            }
        }
    }
}
class UnionFind{
    public int[] par;
    public UnionFind(int n){
        par = new int[n];
        for(int i=0;i<n;i++){
            par[i] = i;
        }
    }
    public void Union(int x,int y){
        par[Get(x)] = Get(y); 
    }
    public bool Same(int x,int y){
        return Get(x) == Get(y);
    }
    int Get(int x){
        if(x != par[x]){
            par[x] = Get(par[x]);
        }
        return par[x];
    }
}