Galaxy Interconnection

Time limit3sMemory limit256 MB

Summary
Given a low-degree graph with a proper k-coloring, output -1 if any edge shares a color, otherwise count vertices that start a simple path of k vertices visiting all k colors.
Level

Medium6 of 10

Topics
Graph, DFS, Backtracking
Solved
No attempts yet

Problem

And then, eight hundred and eight years ago, an event took place that opened a new era in the history of mankind. The Era of the Great Circle.

Ivan Yefremov, Andromeda: A Space-Age Tale

More than 1000 years have passed since the first ACM ICPC. Several civilizations of our Galaxy, including the Earth, are united in the Great Circle, whose members exchange and relay scientific and cultural information. Because of the way gravity and dark energy flows are spread across the galaxy, bidirectional communication channels run between some pairs of planets. Civilizations use these channels to spread knowledge across the whole Great Circle.

It is time to divide the research between civilizations. Each planet takes one of kk main research areas, such as Repagular Calculus or Given String Theory.

The number kk is not accidental. The Great Circle grew out of the Initial Circle, a set of kk planets whose channels form a single cycle. New channels were investigated and built later, and new planets joined the Great Circle. Communication channels cost a lot of energy, so every planet of the Great Circle has fewer than kk channels.

Two planets joined by a direct channel do not take the same research area. They are better off taking different areas and sharing their results.

There is one more rule. Civilizations periodically send expeditions to explore new planets and to visit their neighbors in the Great Circle. A ship flies from one planet to another only when a communication channel joins them. One kind of expedition is called a Research Audit: the ship starts at some planet, makes k−1k - 1 jumps along channels, and visits kk planets in total, counting the origin. Those kk planets have to take all kk research areas, one each, so that the expedition checks every part of the research. All kk areas show up along the route, so the visited planets are different from one another.

The Council of the Great Circle has published one assignment: planet ii takes area aia_i. Review it. If a channel joins two planets that took the same area, the assignment already breaks the first rule. Otherwise, count the planets that a Research Audit can start from.

Input

The first line contains the number of planets nn, the number of research areas kk, and the number of communication channels mm (3≤n≤50003 \le n \le 5000, 3≤k≤min⁡(n,10)3 \le k \le \min(n, 10), 1≤m≤100001 \le m \le 10000).

Each of the next mm lines describes one channel by the numbers of the two planets it joins. Planets are numbered from 11 to nn, and planets 11 through kk form the Initial Cycle. Every planet has fewer than kk channels, and any two planets of the Great Circle are reachable from each other along channels.

The last line contains the assignment as nn integers. The ii-th of them, aia_i, is the research area taken by planet ii (1≤ai≤k1 \le a_i \le k).

Output

If a communication channel joins two planets that took the same research area, print −1-1.

Otherwise print how many planets a Research Audit can start from.

Examples2

  1. Example 1

    Input
    5 4 5
    1 2
    2 3
    3 4
    4 1
    5 3
    1 2 3 4 2
    
    Expected output
    5
    
  2. Example 2

    Input
    6 4 9
    1 2
    2 3
    3 4
    4 1
    5 2
    5 3
    6 5
    6 4
    6 1
    1 2 3 4 4 2
    
    Expected output
    6