Wheel Rotation
InterviewTime limit1sMemory limit128 MB
Given N-1 belts that chain N pulleys from pulley 1, with each belt either straight (same direction) or crossed (reversed), find the rotation direction of pulley N.
- Level
Easy3 of 10
- Topics
- Graph, DFS, Implementation, BFS
- Solved
- No attempts yet
Problem
Farmer John has an old-time thresher (a wheat harvester) that requires belts to be installed on its gears to turn the parts. The engine drives pulley in a clockwise direction; it attaches via a belt to pulley , pulley attaches via a belt to pulley , and so on through a total of () pulleys (and belts).

The diagram above shows the two ways a belt can be installed between two gears. Here, pulley 's belt directly drives pulley (a "straight" connection), so they rotate in the same direction. Pulley drives pulley via a "crossed" belt, which reverses the direction of rotation.
Given the list of belt types connecting the pulleys, and the fact that pulley is driven clockwise by the engine, determine the rotation direction of pulley . Each belt is described by three integers:
- — the driving (source) pulley
- — the driven (destination) pulley
- — the connection type ( = straight, = crossed)
The belts are listed in an arbitrary order.
As an example, consider with pulley driven clockwise. Straight belts drive pulley and then pulley , so they rotate clockwise. A crossed belt reverses the rotation, so pulley (pulley ) rotates counterclockwise.

Input
- Line : a single integer .
- Lines : each line describes a belt with three integers , , and .
Output
- Line : a single integer giving the rotation direction of pulley ( = clockwise, = counterclockwise).