Winter Roads
Time limit10sMemory limit128 MB
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 can travel on a road of capacity if and only if .
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 and (, ), where is the number of landmarks and is the number of roads.
The next lines each contain three integers , , and (, ), describing a road between landmarks and with carrying capacity . The roads are numbered in the order they appear.
The next line contains a single integer (), the number of events. Each of the following lines starts with a capital letter followed by integers:
B r c: road breaks down and its capacity decreases to (, ).R r c: road is repaired and its capacity increases to (, ).S a b w: query whether a supply truck of weight can travel from landmark to landmark (, ).
Only the capital letters B, R, and S appear. Events take effect in the order given. There are at most 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 to on which every road has capacity at least , and 0 otherwise. Print each answer on its own line, with no extra spaces and no blank lines between answers.