Outsourcing
Time limit1sMemory limit128 MB
Given two directed labeled graphs (factories) with start and final nodes, decide whether the two sets of label sequences realizable as paths from start to final are identical.
- Level
Hard9 of 10
- Topics
- Graph, DFS, String matching, Implementation
- Solved
- No attempts yet
Problem
Mr. Cooper manufactures science-fiction action figures and believes his local factory is too expensive. Having heard that workers abroad are far cheaper and more dedicated, he wants to move production to a low-wage country (to "outsource" it).
Before switching, he must be sure the new factory can build exactly the same kinds of action figures as the current one. Manufacturing is organized around assembly stations and transfer stations. An assembly station takes parts from a transfer station, performs a single operation on them, and delivers the result to a transfer station. Every factory has one starting transfer station that supplies raw parts and one final transfer station that collects finished figures.
A given kind of action figure requires a specific sequence of operations . A factory can build that figure if there exist transfer stations such that is the starting station, is the final station, and for every there is an assembly station that takes parts from , performs operation , and delivers to .
Hence Mr. Cooper wants to know whether the local factory and the foreign factory can produce exactly the same sorts of action figures. He realizes that answering this may be an involved challenge, so he hires you to develop a computer solution. For each pair of factories, decide whether the local factory and the foreign factory can build exactly the same set of action figures.
Input
The first line contains an integer , the number of test cases ().
Each test case begins with a line of six integers : the local factory has assembly stations, transfer stations, and distinct operations, and the foreign factory has assembly stations, transfer stations, and distinct operations (; ; ).
In each factory the transfer stations are numbered to ; station is the starting station and station is the final station. The next lines describe the local factory's assembly stations, one per line, as three integers : the station takes parts from transfer station , delivers the result to transfer station , and performs operation (). For any single transfer station, no two of its outgoing assembly stations perform the same operation. The following lines describe the foreign factory's assembly stations in the same format.
Output
For each test case print one line: eligible if the local and foreign factories can build exactly the same set of action figures, or not eligible otherwise.