PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000121. Những cây cầu (Bridges)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những cây cầu (Bridges)

Bridges

Source: APIO

Version: Phuoc Hung OJ Extended

Problem Statement

A city has nn islands, numbered 11 to nn, joined by mm undirected bridges, numbered 11 to mm. Bridge ii joins islands ui,viu_i,v_i and supports vehicles of weight at most did_i. Parallel bridges may exist and have distinct indices; self-loops do not occur.

Process qq operations in order:

  • 1 b r: set the weight limit of bridge bb to exactly rr; this is assignment, not addition.
  • 2 s w: a car of weight ww starts at island ss. Output how many islands are reachable from ss, including ss itself, using only bridges whose weight limits are at least ww.

An update takes effect immediately and influences subsequent queries.

Input

  • The first line contains n,mn,m.
  • Each of the next mm lines contains ui,vi,diu_i,v_i,d_i for bridge ii.
  • The next line contains qq.
  • Each of the next qq lines describes an operation 1 b r or 2 s w, in chronological order.

Output

For every type-2 operation, print one integer on its own line, in chronological order. Type-1 operations produce no output.

Subtasks

  • Subtask 1 (13 points): 1≤n≤10001\le n\le 1000, 0≤m≤10000\le m\le 1000, 1≤q≤100001\le q\le 10000; 1≤ui,vi,sj≤n1\le u_i,v_i,s_j\le n, ui≠viu_i\ne v_i; 1≤di,rj,wj≤1091\le d_i,r_j,w_j\le 10^9; tj∈{1,2}t_j\in\{1,2\}; 1≤bj≤m1\le b_j\le m for type-1 operations; type-1 operations require m≥1m\ge1.
  • Subtask 2 (16 points): 1≤n≤500001\le n\le 50000, 0≤m≤499990\le m\le 49999, 1≤q≤1000001\le q\le 100000; 1≤ui,vi,sj≤n1\le u_i,v_i,s_j\le n, ui≠viu_i\ne v_i; 1≤di,rj,wj≤1091\le d_i,r_j,w_j\le 10^9; tj∈{1,2}t_j\in\{1,2\}; 1≤bj≤m1\le b_j\le m for type-1 operations; type-1 operations require m≥1m\ge1. m=n−1m=n-1, ui=iu_i=i, vi=i+1v_i=i+1 for 1≤i≤m1\le i\le m (a chain).
  • Subtask 3 (17 points): 1≤n≤327671\le n\le 32767, 0≤m≤327660\le m\le 32766, 1≤q≤1000001\le q\le 100000; 1≤ui,vi,sj≤n1\le u_i,v_i,s_j\le n, ui≠viu_i\ne v_i; 1≤di,rj,wj≤1091\le d_i,r_j,w_j\le 10^9; tj∈{1,2}t_j\in\{1,2\}; 1≤bj≤m1\le b_j\le m for type-1 operations; type-1 operations require m≥1m\ge1. n=2k−1n=2^k-1, 1≤k≤151\le k\le15; m=n−1m=n-1; ui=⌊(i+1)/2⌋u_i=\lfloor(i+1)/2\rfloor, vi=i+1v_i=i+1 for 1≤i≤m1\le i\le m (a complete binary tree).
  • Subtask 4 (14 points): 1≤n≤500001\le n\le 50000, 0≤m≤1000000\le m\le 100000, 1≤q≤1000001\le q\le 100000; 1≤ui,vi,sj≤n1\le u_i,v_i,s_j\le n, ui≠viu_i\ne v_i; 1≤di,rj,wj≤1091\le d_i,r_j,w_j\le 10^9; tj∈{1,2}t_j\in\{1,2\}; 1≤bj≤m1\le b_j\le m for type-1 operations; type-1 operations require m≥1m\ge1. All operations have tj=2t_j=2 (no updates).
  • Subtask 5 (13 points): 1≤n≤500001\le n\le 50000, 0≤m≤499990\le m\le 49999, 1≤q≤1000001\le q\le 100000; 1≤ui,vi,sj≤n1\le u_i,v_i,s_j\le n, ui≠viu_i\ne v_i; 1≤di,rj,wj≤1091\le d_i,r_j,w_j\le 10^9; tj∈{1,2}t_j\in\{1,2\}; 1≤bj≤m1\le b_j\le m for type-1 operations; type-1 operations require m≥1m\ge1. m=n−1m=n-1 and the undirected graph initially forms a tree.
  • Subtask 6 (27 points): 1≤n≤500001\le n\le 50000, 0≤m≤1000000\le m\le 100000, 1≤q≤1000001\le q\le 100000; 1≤ui,vi,sj≤n1\le u_i,v_i,s_j\le n, ui≠viu_i\ne v_i; 1≤di,rj,wj≤1091\le d_i,r_j,w_j\le 10^9; tj∈{1,2}t_j\in\{1,2\}; 1≤bj≤m1\le b_j\le m for type-1 operations; type-1 operations require m≥1m\ge1.

Examples

Sample 1

Input

3 4
1 2 5
2 3 2
3 1 4
2 3 8
5
2 1 5
1 4 1
2 2 5
1 1 1
2 3 2

Output

3
2
3

Explanation

In sample 1, a weight-55 car at island 11 can reach all three islands using bridges 1 and 4. After bridge 4 is set to 11, a weight-55 car at island 22 can reach only islands 1,21,2. After bridge 1 is set to 11, a weight-22 car at island 33 can still reach all three islands through bridges 2 and 3.

Sample 2

Input

7 8
1 2 5
1 6 5
2 3 5
2 7 5
3 4 5
4 5 5
5 6 5
6 7 5
12
2 1 6
1 1 1
2 1 2
1 2 3
2 2 2
1 5 2
1 3 1
2 2 4
2 4 2
1 8 1
2 1 1
2 1 3

Output

1
7
7
5
7
7
4

Explanation

In sample 2, every weight-limit update is applied before the following queries. The seven type-2 queries return 1,7,7,5,7,7,41,7,7,5,7,7,4. Initially, a weight-66 car cannot traverse any bridge because all limits are 55, hence the first answer is 11.