infinite XYZ
Time limit1sMemory limit256 MB
Given a directed graph with x, y, z labeled edges, each query adds one edge and asks whether a walk can repeat the label cycle x, y, z forever.
- Level
Medium7 of 10
- Topics
- Graph, Topological sort, Divide and conquer
- Solved
- No attempts yet
Problem
A directed graph has vertices and one-way edges. Several edges may connect the same start and end vertices, and an edge may start and end at the same vertex.
Each edge is labeled with one of , , or . The edges leaving any one vertex have different labels.
The queries are as follows.
- : Add a new one-way edge from to with label . The new edge may share a label with existing edges. Print 1 if there is a starting vertex from which you can keep moving forever on the graph, otherwise print 0.
Movement follows a fixed rule. After an edge, you must take a edge. After a edge, you must take a edge. After a edge, you must take an edge. In other words, the labels can only follow the pattern , , or .
There are queries. Each query is independent, and edges added by an earlier query do not apply to later queries.
Input
The first line contains , , and (, , ).
The next lines each contain , , and , describing an edge from to with label (, ).
The last lines each contain a query , , and (, ).
Output
Print lines. On line , print the answer to the -th query: 1 if you can move forever, otherwise 0.