

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点 辺の単純無向グラフがあります。頂点には から までの、辺には から までの番号がついています。
辺 は頂点 と頂点 を結んでいます。
このグラフの各頂点を赤、緑、青の 色のいずれかで塗る方法であって、以下の条件を満たすものの数を求めてください。
- 辺で直接結ばれている 頂点は必ず異なる色で塗られている
なお、使われない色があっても構いません。
制約
- 与えられるグラフは単純 (多重辺や自己ループを含まない)
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを出力せよ。
入力例 1Copy
3 3 1 2 2 3 3 1
出力例 1Copy
6
頂点 の色をそれぞれ とし、赤、緑、青をそれぞれ R
, G
, B
で表すと、以下の 通りが条件を満たします。
-
RGB
-
RBG
-
GRB
-
GBR
-
BRG
-
BGR
入力例 2Copy
3 0
出力例 2Copy
27
辺がないため、各頂点の色を自由に決めることができます。
入力例 3Copy
4 6 1 2 2 3 3 4 2 4 1 3 1 4
出力例 3Copy
0
条件を満たす塗り方が存在しない場合もあります。
入力例 4Copy
20 0
出力例 4Copy
3486784401
答えは ビット符号付き整数型に収まらないことがあります。
Score : points
Problem Statement
We have a simple undirected graph with vertices and edges. The vertices are numbered through , and the edges are numbered through .
Edge connects Vertex and Vertex .
Find the number of ways to paint each vertex in this graph red, green, or blue so that the following condition is satisfied:
- two vertices directly connected by an edge are always painted in different colors.
Here, it is not mandatory to use all the colors.
Constraints
- The given graph is simple (that is, has no multi-edges and no self-loops).
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
3 3 1 2 2 3 3 1
Sample Output 1Copy
6
Let be the colors of Vertices and R
, G
, B
denote red, green, blue, respectively. There are six ways to satisfy the condition:
-
RGB
-
RBG
-
GRB
-
GBR
-
BRG
-
BGR
Sample Input 2Copy
3 0
Sample Output 2Copy
27
Since the graph has no edge, we can freely choose the colors of the vertices.
Sample Input 3Copy
4 6 1 2 2 3 3 4 2 4 1 3 1 4
Sample Output 3Copy
0
There may be no way to satisfy the condition.
Sample Input 4Copy
20 0
Sample Output 4Copy
3486784401
The answer may not fit into the -bit signed integer type.