Flipping Switches

Time limit7sMemory limit512 MB

Summary
Simulate the given local-search procedure: repeatedly flip the lowest-numbered switch that increases the number of shining lights, then print the final setting.
Level

Medium6 of 10

Topics
Simulation, Greedy, Implementation, Array
Solved
No attempts yet

Problem

You moved into a new home and the wiring is a mess. Several lights are controlled by several switches in a peculiar way. Every switch has two states, up and down. Calling them on and off is useless here. Each light is wired to three switches, and the light shines when at least one of those three is in its correct state. The opposite state of the same switch can be the correct state for another light.

Your first idea was to set the switches so that as many lights as possible shine. Your cook, who knows circuits, says that this is quite hard, so you settle for less. Find a setting in which flipping any single switch to its opposite state does not make more lights shine.

Several settings can satisfy that condition, so only the setting produced by the following procedure is accepted.

  1. Put every switch up.
  2. Scan the switches in order from 1 to nn and look for a switch whose flip, on its own, makes more lights shine than shine now.
  3. If such a switch exists, flip the lowest numbered one and go back to step 2. If none exists, stop.

Each flip makes at least one more light shine, so the procedure always terminates.

Input

The input contains several test cases.

The first line contains the number of test cases tt (1≤t≤251 \le t \le 25).

The first line of each test case contains the number of lights mm (1≤m≤40001 \le m \le 4000) and the number of switches nn (3≤n≤40003 \le n \le 4000). Line ii of the next mm lines contains three distinct integers s1s_1, s2s_2, s3s_3 (1≤si≤n1 \le s_i \le n), each preceded by a + or a - character. These values are the switches that control light ii together with their correct states, where + means up and - means down.

Output

For each test case, print on one line the switch setting produced by the procedure above. The setting is a string of nn characters, each + or -, where character ii is the state of switch ii.

Examples2

  1. Example 1

    Input
    2
    5 3
    +1 +2 +3
    +1 -2 +3
    -1 +2 +3
    -1 +2 -3
    -1 -2 +3
    4 4
    +1 +2 +4
    -1 -2 +4
    +2 +3 +4
    -2 -3 +4
    
    Expected output
    +++
    ++++
    
  2. Example 2

    Input
    1
    1 3
    -1 -2 -3
    
    Expected output
    -++