QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100

#12034. Tree Coloring

Statistics

Given a tree with $n$ vertices, you need to assign a color from $\{1, \dots, m\}$ to each vertex such that no two adjacent vertices in the tree have the same color.

This problem is clearly too simple, so we add $k$ constraints: the $i$-th constraint is given as $(x_i, y_i)$, meaning the color of vertex $x_i$ cannot be $y_i$.

Find the number of valid coloring schemes that satisfy all constraints. Since the answer may be very large, output the result modulo $998244353$.

Input

The first line contains three integers $n, m, k$.

The next $n-1$ lines each contain two positive integers $u, v$ describing a tree edge $(u, v)$. It is guaranteed that the given graph is a tree.

The next $k$ lines each contain two positive integers $x_i, y_i$ describing the $i$-th constraint.

Output

Output a non-negative integer representing the number of valid coloring schemes modulo $998244353$.

Examples

Input 1

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

Output 1

8

Subtasks

Task 1 (13 pts): $n, m \leq 10^3$

Task 2 (9 pts): $k = 0$

Task 3 (25 pts): For every edge $(u, v)$, $|u - v| = 1$

Task 4 (53 pts): No special constraints

For all data, $2 \leq n \leq 2 \times 10^5$, $2 \leq m \leq 10^9$, $0 \leq k \leq 4 \times 10^5$, $1 \leq u < v \leq n$, $1 \leq x_i \leq n$, $1 \leq y_i \leq m$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.