This page is still under construction.

Parts of this page are still being built. What you see may change.

Winter Roads

Time limit10sMemory limit128 MB

Summary
After a series of road capacity updates, answer queries asking whether two landmarks stay connected using only roads of capacity at least w.
Level

Hard8 of 10

Topics
Graph, Union-find, DFS, Divide and conquer
Solved
No attempts yet

Problem

Winter is coming, and the citizens of Winterfell are preparing for the long months ahead. One pressing concern is the stability of the roads and bridges: supply lines must stay uninterrupted as the season progresses.

The city's civil engineers want to model scenarios in which roads fail and are repaired. In their model, each road connects two landmarks, and each road has a carrying capacity. Trucks travel from landmark to landmark and may only use sufficiently sturdy roads: a supply truck of weight ww can travel on a road of capacity cc if and only if w≤cw \le c.

Over time a road's capacity may decrease (through wear or accident) or increase (through repair). Throughout these changes, the engineers still need to know whether a truck can get from a source landmark to a destination landmark.

Given a sequence of changes to the roads together with some supply-truck runs, determine for each run whether the supplies can still be delivered.

Input

The input contains several test cases. Each test case begins with a line containing two integers nn and mm (1≤n≤10001 \le n \le 1000, 1≤m≤1000001 \le m \le 100000), where nn is the number of landmarks and mm is the number of roads.

The next mm lines each contain three integers aa, bb, and cc (1≤a,b≤n1 \le a, b \le n, 1≤c≤1091 \le c \le 10^9), describing a road between landmarks aa and bb with carrying capacity cc. The roads are numbered 1,2,…,m1, 2, \dots, m in the order they appear.

The next line contains a single integer ee (1≤e≤1000001 \le e \le 100000), the number of events. Each of the following ee lines starts with a capital letter followed by integers:

  • B r c: road rr breaks down and its capacity decreases to cc (1≤r≤m1 \le r \le m, 1≤c<1091 \le c < 10^9).
  • R r c: road rr is repaired and its capacity increases to cc (1≤r≤m1 \le r \le m, 1<c≤1091 < c \le 10^9).
  • S a b w: query whether a supply truck of weight ww can travel from landmark aa to landmark bb (1≤a,b≤n1 \le a, b \le n, 1≤w≤1091 \le w \le 10^9).

Only the capital letters B, R, and S appear. Events take effect in the order given. There are at most 20002000 breakdown/repair (B/R) events in total. The input ends with a line containing two zeros.

Output

For each S a b w query, in order, output 1 if there is a path from aa to bb on which every road has capacity at least ww, and 0 otherwise. Print each answer on its own line, with no extra spaces and no blank lines between answers.

Examples1

  1. Example 1

    Input
    3 4
    1 2 3
    2 3 3
    2 1 1
    1 2 1
    6
    S 1 2 4
    S 2 3 2
    R 1 4
    S 1 2 4
    B 2 1
    S 2 3 2
    0 0
    
    Expected output
    0
    1
    1
    0