D - RGB Coloring 2 Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

NN 頂点 MM 辺の単純無向グラフがあります。頂点には 11 から NN までの、辺には 11 から MM までの番号がついています。
ii は頂点 AiA_i と頂点 BiB_i を結んでいます。
このグラフの各頂点を赤、緑、青の 33 色のいずれかで塗る方法であって、以下の条件を満たすものの数を求めてください。

  • 辺で直接結ばれている 22 頂点は必ず異なる色で塗られている

なお、使われない色があっても構いません。

制約

  • 1N201 \le N \le 20
  • 0MN(N1)20 \le M \le \frac{N(N - 1)}{2}
  • 1AiN1 \le A_i \le N
  • 1BiN1 \le B_i \le N
  • 与えられるグラフは単純 (多重辺や自己ループを含まない)

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1 B1B_1
A2A_2 B2B_2
A3A_3 B3B_3
\hspace{15pt} \vdots
AMA_M BMB_M

出力

答えを出力せよ。


入力例 1Copy

Copy
3 3
1 2
2 3
3 1

出力例 1Copy

Copy
6

頂点 1,2,31, 2, 3 の色をそれぞれ c1,c2,c3c_1, c_2, c_3 とし、赤、緑、青をそれぞれ R, G, B で表すと、以下の 66 通りが条件を満たします。

  • c1c2c3=c_1c_2c_3 = RGB
  • c1c2c3=c_1c_2c_3 = RBG
  • c1c2c3=c_1c_2c_3 = GRB
  • c1c2c3=c_1c_2c_3 = GBR
  • c1c2c3=c_1c_2c_3 = BRG
  • c1c2c3=c_1c_2c_3 = BGR

入力例 2Copy

Copy
3 0

出力例 2Copy

Copy
27

辺がないため、各頂点の色を自由に決めることができます。


入力例 3Copy

Copy
4 6
1 2
2 3
3 4
2 4
1 3
1 4

出力例 3Copy

Copy
0

条件を満たす塗り方が存在しない場合もあります。


入力例 4Copy

Copy
20 0

出力例 4Copy

Copy
3486784401

答えは 3232 ビット符号付き整数型に収まらないことがあります。

Score : 400400 points

Problem Statement

We have a simple undirected graph with NN vertices and MM edges. The vertices are numbered 11 through NN, and the edges are numbered 11 through MM.
Edge ii connects Vertex AiA_i and Vertex BiB_i.
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

  • 1N201 \le N \le 20
  • 0MN(N1)20 \le M \le \frac{N(N - 1)}{2}
  • 1AiN1 \le A_i \le N
  • 1BiN1 \le B_i \le N
  • 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:

NN MM
A1A_1 B1B_1
A2A_2 B2B_2
A3A_3 B3B_3
\hspace{15pt} \vdots
AMA_M BMB_M

Output

Print the answer.


Sample Input 1Copy

Copy
3 3
1 2
2 3
3 1

Sample Output 1Copy

Copy
6

Let c1,c2,c3c_1, c_2, c_3 be the colors of Vertices 1,2,31, 2, 3 and R, G, B denote red, green, blue, respectively. There are six ways to satisfy the condition:

  • c1c2c3=c_1c_2c_3 = RGB
  • c1c2c3=c_1c_2c_3 = RBG
  • c1c2c3=c_1c_2c_3 = GRB
  • c1c2c3=c_1c_2c_3 = GBR
  • c1c2c3=c_1c_2c_3 = BRG
  • c1c2c3=c_1c_2c_3 = BGR

Sample Input 2Copy

Copy
3 0

Sample Output 2Copy

Copy
27

Since the graph has no edge, we can freely choose the colors of the vertices.


Sample Input 3Copy

Copy
4 6
1 2
2 3
3 4
2 4
1 3
1 4

Sample Output 3Copy

Copy
0

There may be no way to satisfy the condition.


Sample Input 4Copy

Copy
20 0

Sample Output 4Copy

Copy
3486784401

The answer may not fit into the 3232-bit signed integer type.



2025-03-30 (Sun)
00:11:49 +00:00