Under construction

Still being built. Things may change

infinite XYZ

Time limit1sMemory limit256 MB

Summary
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 NN vertices and MM 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 xx, yy, or zz. The edges leaving any one vertex have different labels.

The queries are as follows.

  • ss ee cc: Add a new one-way edge from ss to ee with label cc. 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 xx edge, you must take a yy edge. After a yy edge, you must take a zz edge. After a zz edge, you must take an xx edge. In other words, the labels can only follow the pattern xyzxyz⋯xyzxyz\cdots, yzxyzx⋯yzxyzx\cdots, or zxyzxy⋯zxyzxy\cdots.

There are QQ queries. Each query is independent, and edges added by an earlier query do not apply to later queries.

Input

The first line contains NN, MM, and QQ (1≤N≤1000001 \leq N \leq 100000, 1≤M≤3×N1 \leq M \leq 3 \times N, 1≤Q≤1000001 \leq Q \leq 100000).

The next MM lines each contain SiS_i, EiE_i, and CiC_i, describing an edge from SiS_i to EiE_i with label CiC_i (1≤Si,Ei≤N1 \leq S_i, E_i \leq N, Ci∈{x,y,z}C_i \in \{x,y,z\}).

The last QQ lines each contain a query ss, ee, and cc (1≤s,e≤N1 \leq s, e \leq N, c∈{x,y,z}c \in \{x,y,z\}).

Output

Print QQ lines. On line ii, print the answer to the ii-th query: 1 if you can move forever, otherwise 0.

Examples3

  1. Example 1

    Input
    2 1 1
    1 2 x
    1 2 x
    
    Expected output
    0
    
  2. Example 2

    Input
    3 3 3
    1 2 x
    2 3 y
    3 1 z
    1 2 x
    2 3 x
    3 1 x
    
    Expected output
    1
    1
    1
    
  3. Example 3

    Input
    5 2 3
    1 2 x
    2 3 y
    1 2 x
    3 1 z
    3 2 y
    
    Expected output
    0
    1
    0