Day-6: Mirror World

2.6

7 votes
Graph, Advanced reduction techniques, Algorithms, Medium-Hard, Graph Representation
Problem

It is 2500 and John lives in a city consisting of N buildings numbered 1,2,....,N. The building numbered 1 is John’s school and building numbered H1 is his home. All roads of the city are unidirectional and are given a label L which denotes the road number. There exists M different kinds of label. John wishes to reach his home from his school via some path by travelling on the unidirectional roads.

In a parallel mirror world consisting on K buildings numbered 1,2,...,K and there exists a doppelganger of John whose name is Ron. In this parallel world, building 1 is the school and H2 is his home. Ron is controlled completely by John’s state of mind. Initially both are in school in their respective worlds. Now John starts travelling to his home in the real world. Whenever John takes a path with label Li from the building where he is at currently, Ron also takes the path with the same label from the building where he is at currently in the mirror world.

 

Now the condition is that whenever John reaches his home in the real world, only then Ron should also reach his home in the parallel world. Only then we can say it is a mirror world. For some building that they reach simultaneously, if one of them is able to take a path with label Li but the other isn't able to take the same, then it is not mirror world. Find if this a mirror world or not.

Note that the number of distinct labels are M in both the worlds and there can be at most one path from each building for each label Li(1LiM)

 

INPUT FORMAT

First line contains an integer T denoting the number of test cases.

first line of each test case contains an integer M denoting the number of roads.

the next line contains 3 integers N,E,H1 denoting the number of nodes, number of edges in the first graph and the node number of the home.

Next E lines contain 3 integers A,B,Li denoting that Ron goes from A to B by road Li.

Next line contains 3 integers K,F,H2 denoting the number of nodes, edges in the second graph, and the node number of the home in second world.

Next F lines contain 3 integers A,B,Li denoting that Ron goes from A to B by road Li.

OUTPUT FORMAT

For each of the T testcases output a YES or NO in a separate line according to the answer.

CONSTRAINTS

  • 1T50

  • 1N,K,M105

  • 1E,F104

  • 1H1N

  • 1H2K

SUBTASKS (10 Points)

  • 1T5
Sample Input
1
2
3 3 3
1 2 1
1 3 2
2 3 1
3 2 3
1 3 1
1 2 2
Sample Output
NO
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first graph he can reach home (i.e 3) from 1 using road number 2 and through 2 using road number 1 and then again road number 1.

However, in the second case, he can reach node 3 using node 1 by road 1. So this is not a mirror world. Also it is impossible to reach node 3 (i.e House) thorugh node 2, which is possible in the first world.

So, this is not a mirror world. 

Editor Image

?