Time limit: 1.7s
Memory limit: 512MB
Input:
Output:
Consider a connected undirected graph with nodes and edges. Initially every node has a color , encoded by an integer between and . You can repeatedly modify node colors by assigning , where and are connected by an edge.
Task
Given a destination coloring , determine whether you can transform into .
Input data
There are several test cases per input file and you should answer each of them separately.
The first line contains the number of test cases. Each test case is structured as
N M
a[1] a[2] ... a[N]
b[1] b[2] ... b[N]
u1 v1
u2 v2
...
uM vM
Output data
For every test case you should print, on a separate line, 1
if can be transformed into using the above-mentioned operation and 0
otherwise.
Constraints and clarifications
- For all test cases, and .
- For every input file, the sum of all is and the sum of all is .
- for all .
# | Points | Constraints |
---|---|---|
1 | 15 | The graph is a star ( and one node is connected to every other node). The sum of among all test cases in an input file is . |
2 | 7 | The graph is complete. . The sum of among all test cases in an input file is . |
3 | 8 | The graph is a chain ( and the edges form a single path). The sum of among all test cases in an input file is . |
4 | 15 | The graph is a chain. |
5 | 7 | The graph is a tree. The sum of among all test cases in an input file is . |
6 | 16 | The graph is a tree and the coloring is a permutation of . |
7 | 10 | The sum of among all test cases in an input file is . |
8 | 22 | No additional constraints. |
Example
stdin
2
4 4
3 3 2 1
2 1 2 1
1 2
2 3
3 4
4 2
4 4
3 3 2 1
1 2 2 1
1 2
2 3
3 4
4 2
stdout
1
0
Explanation
For the first graph, the operations needed are: