Come and Go
InterviewTime limit2sMemory limit512 MB
Given a mixed graph of one-way and two-way streets, decide whether every pair of intersections is mutually reachable.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Union-find
- Solved
- No attempts yet
Problem
A city has intersections joined by one-way streets and two-way streets. Many of the streets run through tunnels or over viaducts.
The road network must allow a round trip between any two intersections: for any intersections and , it must be possible to travel from to and from to .
Read the description of a city's road network and decide whether that requirement holds.
Input
The input contains several test cases. The first line of a test case has two integers and separated by a single space, the number of intersections and the number of streets (, ). Each of the next lines describes one street with three integers , and , separated by single spaces. and are distinct intersection numbers (, ), and is 1 or 2. If is 1, the street is one-way and runs from to . If is 2, the street connects and in both directions. No two streets connect the same pair of intersections.
The last test case is followed by a line containing two zeros separated by a single space. Do not process that line.
Output
For each test case, print one line with an integer . is 1 if the connectivity requirement holds, and 0 otherwise.