#P15665. [ICPC 2025 Jakarta R] Travelling Taro Trains

[ICPC 2025 Jakarta R] Travelling Taro Trains

题目描述

Taro is travelling within a country, which consists of NN cities numbered from 11 to NN. There are also KK train companies, numbered from 11 to KK, that operate the trains within the nation. Initially, there are MM one-way train services, each of which can be described as a triple (u,v,k)(u, v, k), meaning that there are trains operated by company kk that goes from city uu to city vv.

Taro is currently in city 11. In the next QQ months, one of the following three events may happen.

  • 1 u v k\texttt{1 u v k}: company kk starts\textbf{starts} a new train service that goes from city uu to city vv. In other words, add (u,v,k)(u, v, k) to the train network.
  • 2 u v k\texttt{2 u v k}: company kk ends\textbf{ends} its train service that goes from city uu to city vv. In other words, remove (u,v,k)(u, v, k) from the train network.
  • 3 k\texttt{3 k}: Taro either stays\textbf{stays} in the city he is currently in, or takes\textbf{takes} one of the trains operated by company kk from the city he is currently in to another city.

It is guaranteed to you that the existing services (u,v,k)(u, v, k) are always distinct throughout the course of events, and event 22 always removes a currently existing service.

Every time an event 33 happens, find the number of different cities Taro can possibly be in, given the course of events so far.

输入格式

The first line contains three integers NN, MM, KK, and QQ (2N300  0002 \le N \le 300\;000; 1M,K,Q300  0001 \le M, K, Q \le 300\;000).

Each of the next MM lines contains three integers uu, vv, kk (1u,vN1 \leq u, v \leq N; 1kK1 \leq k \leq K; uvu \neq v) describing the initial train services.

Each of the next QQ lines contains an event as described above.

The input guarantees that the existing services (u,v,k)(u, v, k) are always distinct throughout the event.

输出格式

For each event 33, output, in a single line, the number of different cities Taro can possibly be in.

6 4 2 5
1 3 1
3 6 2
3 2 2
3 5 2
3 2
3 1
1 1 4 2
2 3 6 2
3 2
1
2
5

提示

Explanation of Sample 1:\textit{Explanation of Sample 1:} Initially, Taro is in city 11.

  • In the first event 33, there are no train services operated by company 22 that departs from city 11, so Taro has to stay in city 11.
  • In the second event 33, Taro can either stay in city 11, or take the service (1,3,1)(1, 3, 1) and end in city 33. Because Taro can be in either city 11 or 33, you have to output 22.
  • In the third event 33, the existing train services are (1,3,1)(1, 3, 1), (1,4,2)(1, 4, 2), (3,2,2)(3, 2, 2), (3,5,2)(3, 5, 2). If Taro is currently in city 11, he can take service (1,4,2)(1, 4, 2) and end in city 44. If Taro is currently in city 33, he can take services (3,2,2)(3, 2, 2) or (3,5,2)(3, 5, 2) and end in city 22 or 55 respectively. Since he can possibly in city 11, 22, 33, 44, and 55, you have to output 55.