#abc199d. D - RGB Coloring 2
D - RGB Coloring 2
Score : points
问题描述
我们有一个包含 个顶点和 条边的简单无向图。顶点编号为从 到 ,边编号为从 到 。 第 条边连接顶点 和顶点 。
找出一种将此图中每个顶点涂成红色、绿色或蓝色的方法的数量,使得满足以下条件:
- 直接通过一条边相连的两个顶点总是被涂成不同的颜色。
这里,并不要求使用所有颜色。
以上为通义千问 qwen-max 翻译,仅供参考。
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 1
3 3
1 2
2 3
3 1
Sample Output 1
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 2
3 0
Sample Output 2
27
Since the graph has no edge, we can freely choose the colors of the vertices.
Sample Input 3
4 6
1 2
2 3
3 4
2 4
1 3
1 4
Sample Output 3
0
There may be no way to satisfy the condition.
Sample Input 4
20 0
Sample Output 4
3486784401
The answer may not fit into the -bit signed integer type.