Colors

Time limit: 1.7s Memory limit: 512MB Input: Output:

Consider a connected undirected graph with NN nodes and MM edges. Initially every node uu has a color a[u]a[u], encoded by an integer between 11 and NN. You can repeatedly modify node colors by assigning a[u]=min(a[u],a[v])a[u] = \min(a[u], a[v]), where uu and vv are connected by an edge.

Task

Given a destination coloring b[1]b[N]b[1] \dots b[N], determine whether you can transform aa into bb.

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 aa can be transformed into bb using the above-mentioned operation and 0 otherwise.

Constraints and clarifications

  • For all test cases, N150 000N ≤ 150 \ 000 and M200 000M ≤ 200 \ 000.
  • For every input file, the sum of all NN is 300 000≤ 300 \ 000 and the sum of all MM is 400 000≤ 400 \ 000.
  • 1a[i],b[i]N1 ≤ a[i], b[i] ≤ N for all 1iN1 ≤ i ≤ N.
# Points Constraints
1 15 The graph is a star (M=N1M = N - 1 and one node is connected to every other node). The sum of N2N^2 among all test cases in an input file is 5 000 000≤ 5 \ 000 \ 000.
2 7 The graph is complete. N50N ≤ 50. The sum of NMN \cdot M among all test cases in an input file is 12 000 000≤ 12 \ 000 \ 000.
3 8 The graph is a chain (M=N1M = N - 1 and the edges form a single path). The sum of N2N^2 among all test cases in an input file is 5 000 000≤ 5 \ 000 \ 000.
4 15 The graph is a chain.
5 7 The graph is a tree. The sum of N2N^2 among all test cases in an input file is 5 000 000≤ 5 \ 000 \ 000.
6 16 The graph is a tree and the coloring aa is a permutation of {1,2,,N}\{1, 2, \dots, N\}.
7 10 The sum of NMN \cdot M among all test cases in an input file is 5 000 000≤ 5 \ 000 \ 000.
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:

  • a[2]=min(a[2],a[3])=2a[2] = \min(a[2], a[3]) = 2
  • a[1]=min(a[1],a[2])=2a[1] = \min(a[1], a[2]) = 2
  • a[2]=min(a[2],a[4])=1a[2] = \min(a[2], a[4]) = 1

Log in or sign up to be able to send submissions!