1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <cstring>
#include <list>
#include <vector>

#define PLAYER(p) ((int) (p))
#define ENEMY(p) ((int) ((p + 1) % 2))
#define OTHER_PLAYER(p) ENEMY(p)


//decks should be presorted by values to optimize searching
//this would speed up selection via getBestDeck from looping through list to taking first value


enum players
{
	NOT_SET = 2,
	PLAYERS_NUMBER = 2,
	PLAYER1 = 0,
	PLAYER2 = 1,
};



struct DeckPairRule
{
	int i_deck1;
	int i_deck2;
	players p_winner;

	DeckPairRule() : i_deck1(0), p_winner(NOT_SET), i_deck2(0) {};
	DeckPairRule( int _i_deck1, int _i_deck2, players _p_winner ) :	i_deck1( _i_deck1 ), i_deck2( _i_deck2 ), p_winner( _p_winner ) {};

	int getDeck( players _p_player ) const { return ( _p_player == PLAYER1 ? i_deck1 : i_deck2 ); };
};


class Game
{
private:
	int i_decksNumber;
	std::list<DeckPairRule> l_dpr_activeRules;
	std::vector<int> v_i_precalcWeights[2];

	std::list<int> l_i_activeDecks[2];


	int getBestDeck( players _p_player )
	{
		int max = l_i_activeDecks[ PLAYER(_p_player) ].front();

		for ( std::list<int>::const_iterator it = l_i_activeDecks[ PLAYER(_p_player) ].begin(); it != l_i_activeDecks[ PLAYER(_p_player) ].end(); ++it )
		{
			if ( v_i_precalcWeights[ PLAYER(_p_player) ][ *it ] > v_i_precalcWeights[ PLAYER(_p_player) ][ max ] )
			{
				max = *it;
			}
		}

		return max;
	};


	void removeDeck( players _p_player, int _i_deck )
	{
		{	//remove rules for this deck for player
			for ( std::list<DeckPairRule>::iterator it = l_dpr_activeRules.begin(); it != l_dpr_activeRules.end(); it )
			{
				if ( (*it).getDeck(_p_player) == _i_deck )
				{
					//remove weight if we lost winner add if we lost loser
					v_i_precalcWeights[ PLAYER(PLAYER1) ][ (*it).i_deck1 ] += ( (*it).p_winner == PLAYER1 ? -1 : 1 );
					v_i_precalcWeights[ PLAYER(PLAYER2) ][ (*it).i_deck2 ] += ( (*it).p_winner == PLAYER2 ? -1 : 1 );

					l_dpr_activeRules.erase( it++ );
				}
				else
				{
					++it;
				}
			}
		}

		{	//remove deck from player list
			std::list<int>::iterator it = l_i_activeDecks[ PLAYER(_p_player) ].begin();
			while ( (*it) != _i_deck ) ++it;
			l_i_activeDecks[ PLAYER(_p_player) ].erase( it );
		}
	};


public:
	Game() : i_decksNumber(0)
	{};

	void setDeckNumber( int _i_decks )
	{
		i_decksNumber = _i_decks;

		v_i_precalcWeights[0].resize(i_decksNumber);
		v_i_precalcWeights[1].resize(i_decksNumber);
		std::memset( &(v_i_precalcWeights[0][0]), 0, i_decksNumber * sizeof(int) );
		std::memset( &(v_i_precalcWeights[1][0]), 0, i_decksNumber * sizeof(int) );

		for ( int i = 0; i < i_decksNumber; ++i )
		{
			l_i_activeDecks[0].push_back( i );
			l_i_activeDecks[1].push_back( i );
		}
	};

	void addRule( int _i_deck1, int _i_deck2, players _p_winner )
	{
		l_dpr_activeRules.push_back( DeckPairRule( _i_deck1, _i_deck2, _p_winner ) );

		//add weight if winner remove if loser
		v_i_precalcWeights[ PLAYER1 ][ _i_deck1 ] += ( _p_winner == PLAYER1 ? 1 : -1 );
		v_i_precalcWeights[ PLAYER2 ][ _i_deck2 ] += ( _p_winner == PLAYER2 ? 1 : -1 );
	};


	players play( players _p_firstMove )
	{
		//returns winner, NOT_SET for remis

		players p_activePlayer = _p_firstMove;
		int bestDeck;

		while ( ( ( l_i_activeDecks[0].size() > 1 && l_i_activeDecks[1].size() >= 1 ) || ( l_i_activeDecks[0].size() >= 1 && l_i_activeDecks[1].size() > 1 ) ) && l_dpr_activeRules.size() > 0 )
		{
			bestDeck = getBestDeck( (players) ENEMY(p_activePlayer) );
			removeDeck( (players) ENEMY(p_activePlayer), bestDeck );

			p_activePlayer = (players) OTHER_PLAYER(p_activePlayer);
		}


		if ( l_dpr_activeRules.size() > 0 ) //check last decks against renaining rules
		{
			int remainingDeckPlayer1 = l_i_activeDecks[0].front();
			int remainingDeckPlayer2 = l_i_activeDecks[1].front();

			for ( std::list<DeckPairRule>::iterator it = l_dpr_activeRules.begin(); it != l_dpr_activeRules.end(); ++it )
			{
				if ( (*it).i_deck1 == remainingDeckPlayer1 && (*it).i_deck2 == remainingDeckPlayer2 )
				{
					return (*it).p_winner;
				}
			}
		}

		//no rules left or no rule for last decks remains
		return NOT_SET;
	};

};


int main()
{
	std::ios_base::sync_with_stdio(0);

	int t;
	std::cin >> t;

	for ( int i = 0; i < t; ++i )
	{
		Game g_game;

		int n, m;
		int deck1, deck2;
		char sign;

		std::cin >> n >> m;

		g_game.setDeckNumber( n );
		int playerWinOption[ PLAYERS_NUMBER ] = {0};

		for ( int j = 0; j < m; ++j )
		{
			std::cin >> deck1 >> sign >> deck2;
			g_game.addRule( deck1-1, deck2-1, ( sign == '>' ? PLAYER1 : PLAYER2 ) );
		}

		players p_winner;
		p_winner = g_game.play( PLAYER1 );

		if ( p_winner == PLAYER1 )
			std::cout << "WYGRANA" << std::endl;
		else if ( p_winner == PLAYER2 )
			std::cout << "PRZEGRANA" << std::endl;
		else
			std::cout << "REMIS" << std::endl;

	}

	return 0;
};