Wheel Rotation

Interview

Time limit1sMemory limit128 MB

Summary
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 11 in a clockwise direction; it attaches via a belt to pulley 22, pulley 22 attaches via a belt to pulley 33, and so on through a total of NN (2≤N≤1,0002 \le N \le 1{,}000) pulleys (and N−1N-1 belts).

The diagram above shows the two ways a belt can be installed between two gears. Here, pulley 11's belt directly drives pulley 22 (a "straight" connection), so they rotate in the same direction. Pulley 33 drives pulley 44 via a "crossed" belt, which reverses the direction of rotation.

Given the list of belt types connecting the pulleys, and the fact that pulley 11 is driven clockwise by the engine, determine the rotation direction of pulley NN. Each belt is described by three integers:

  • SiS_i — the driving (source) pulley
  • DiD_i — the driven (destination) pulley
  • CiC_i — the connection type (00 = straight, 11 = crossed)

The belts are listed in an arbitrary order.

As an example, consider N=4N = 4 with pulley 11 driven clockwise. Straight belts drive pulley 22 and then pulley 33, so they rotate clockwise. A crossed belt reverses the rotation, so pulley 44 (pulley NN) rotates counterclockwise.

Input

  • Line 11: a single integer NN.
  • Lines 2…N2 \ldots N: each line describes a belt with three integers SiS_i, DiD_i, and CiC_i.

Output

  • Line 11: a single integer giving the rotation direction of pulley NN (00 = clockwise, 11 = counterclockwise).

Examples1

  1. Example 1

    Input
    4
    2 3 0
    3 4 1
    1 2 0
    
    Expected output
    1