#P3637. 方程组

方程组

Description

Initially, xht has NN variables, denoted by x1,x2,,xnx_1,x_2,\cdots,x_n. There is also a constant KK, and MM equations, each of the form xaxbc(modK)x_a-x_b≡c\pmod K.

Since the problem may change, xht sometimes needs to add a new equation or delete an existing one.

At the same time, xht will give you some queries: set the variable xa=cx_a=c, and find the value of another variable xbmodKx_b \bmod K. Of course, sometimes xbx_b cannot be determined due to insufficient conditions; in that case, output 1-1.

The testdata guarantees that at any time there is at most one equation between any two variables. It is guaranteed that no contradictory systems will appear, and no redundant conditions will appear (no equation can be derived from some others).

Description

Input Format

The first line contains four integers N,M,K,QN,M,K,Q, as described above. QQ is the number of operations.

The next MM lines each contain three integers a,b,ca,b,c, representing the equation xaxbc(modK)x_a-x_b≡c\pmod K.

The next QQ lines, each starting with an integer tt indicating the type of operation:

  • t=1t=1: followed by a,b,ca,b,c, meaning to add an equation xaxbc(modK)x_a-x_b≡c\pmod K;
  • t=2t=2: followed by a,ba,b, meaning to delete the equation between aa and bb. If this equation does not exist, do nothing;
  • t=3t=3: followed by a,b,ca,b,c, meaning a query: set xa=cx_a=c, and ask for the value of xbmodKx_b \bmod K.

Output Format

For each operation of type 33 (query), output one integer xx (0x<K0\le x<K), representing xbmodKx_b \bmod K. If the conditions are insufficient, output 1-1.

3 2 100 3
1 2 1
2 3 2
3 1 3 0
2 1 2
3 1 3 0
97
-1

Hint

Explanation of the sample:

Initially there are two equations: x1x2=1x_1-x_2=1, x2x3=2x_2-x_3=2.

In the first query, set x1=0x_1=0, and obtain x3=(3)mod100=97x_3=(-3)\bmod100=97.

In the second query, the second equation has been deleted, leading to insufficient conditions, so x3x_3 cannot be determined. Output 1-1.

For 40%40\% of the testdata, there are only query operations.

For 100%100\% of the testdata, 1M<N1051\le M<N\le10^5, 1Q1051\le Q\le10^5, 2K1032\le K\le10^3, 1a,bN1\le a,b\le N, 0c<K0\le c<K.

It is guaranteed that all aba\ne b.

Translated by ChatGPT 5