luogu#P4796. [BalticOI 2018] 路径

    ID: 3820 远端评测题 3000ms 750MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>搜索2018枚举记忆化搜索BalticOI(波罗的海)

[BalticOI 2018] 路径

Problem Description

This problem is translated from BalticOI 2018 Day 2 “Paths”.

Given an undirected graph with NN vertices and MM edges. Each vertex has a color. There are KK different colors in total, numbered 1K1 \ldots K. Find how many simple paths in the graph of length at least 22 satisfy that all vertices on the path have pairwise distinct colors.

Paths with different visiting orders of vertices are considered different paths.

Input Format

The first line contains three integers NN, MM, and KK.

The second line contains NN integers between 11 and KK, representing the color of each vertex.

Each of the next MM lines contains two integers aa and bb, denoting an edge of the graph.

It is guaranteed that the graph has no self-loops and no multiple edges.

Output Format

Output one integer: the number of paths in which all vertices have distinct colors. It is guaranteed that the answer will not exceed 101810^{18}.

4 3 3
1 2 1 3
1 2
2 3
4 2
10


9 11 4
1 2 3 4 1 2 1 2 2
1 2
1 3
2 3
2 4
3 6
6 2
6 5
4 3
4 5
7 8
9 8
70

Hint

Explanation for Sample 1

The graph in Sample 1 is shown in the figure above. The background color of each vertex is white (color 11), gray (color 22), or black (color 33). There are 1010 paths whose vertices all have distinct colors. They are: 1-2, 2-1, 2-3, 3-2, 2-4, 4-2, 1-2-4, 4-2-1, 3-2-4, and 4-2-3.

Note that 1 cannot be considered a path, because a path must connect at least two vertices. Also, 1-2-3 does not satisfy the condition, because two vertices have color 11.

Subtask Score Constraints
11 2323 $1 \leqslant N,M \leqslant 100, 1 \leqslant K \leqslant 4$
22 2020 $1 \leqslant N,M \leqslant 300\,000, 1 \leqslant K \leqslant 3$
33 2727 $1 \leqslant N,M \leqslant 300\,000, 1 \leqslant K \leqslant 4$
44 3030 $1 \leqslant N,M \leqslant 100\,000, 1 \leqslant K \leqslant 5$

Thanks to Hatsune_Miku for providing the translation.

Translated by ChatGPT 5