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
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

//#define debug(x...) printf(x)
#define debug(x...)

struct Node {
public:
	Node(int player, int deck){
		this->deckNumber = deck;
		this->playerNumber = player;
		this->score = 0;
	}
	int playerNumber;
	int deckNumber;
	int score;
	std::vector<int> predecessors;
	std::vector<int> successors;
};

struct LessScore{
  bool operator()(Node* a, Node* b) {
	  if (a->score != b->score){
		return a->score > b->score;
	  } else {
		return a > b;
	  }
  }
} lessScoreComparator;

std::vector<Node*> player1List;
std::vector<Node*> player2List;

std::set<Node*, LessScore> player1Hand;
std::set<Node*, LessScore> player2Hand;

void removeFromPlayersHand(Node *node){
	Node *neighbour;
	for (int i = 0; i < node->predecessors.size(); ++i){
		if (node->playerNumber == 1){
			neighbour = player2List[node->predecessors[i]];
			int wasOnHand = player2Hand.erase(neighbour);
			neighbour->score--;
			if (wasOnHand == 1){
				player2Hand.insert(neighbour);
			}
		} else {
			neighbour = player1List[node->predecessors[i]];
			int wasOnHand = player1Hand.erase(neighbour);
			neighbour->score--;
			if (wasOnHand == 1){
				player1Hand.insert(neighbour);
			}
		}
	}
	for (int i = 0; i < node->successors.size(); ++i){
		if (node->playerNumber == 1){
			neighbour = player2List[node->successors[i]];
			int wasOnHand = player2Hand.erase(neighbour);
			neighbour->score++;
			if (wasOnHand == 1){
				player2Hand.insert(neighbour);
			}
		} else {
			neighbour = player1List[node->successors[i]];
			int wasOnHand = player1Hand.erase(neighbour);
			neighbour->score++;
			if (wasOnHand == 1){
				player1Hand.insert(neighbour);
			}
		}
	}
}

/*void printHand(const std::vector<Node*>& hand){
	debug("Printing hand of player %d\n", hand[0]->playerNumber);
	for (int i = 0; i < hand.size(); ++i){
		debug("%d %d %d\n", hand[i]->playerNumber, hand[i]->deckNumber, hand[i]->score);
	}
}*/

bool isSorted(const std::vector<Node*>& hand){
	for (int i = 0; i < hand.size()-1; ++i){
		if (hand[i]->score > hand[i+1]->score){
			return false;
		}
	}
	return true;
}

int main(){
	int t;
	scanf("%d", &t);
	for (int testNo = 1; testNo <= t; ++testNo){
		int n, m;
		scanf("%d %d", &n, &m);
		
		debug("TestNo: %d\n", testNo);

		//create nodes for decks for both players
		for (int i = 0; i < n; ++i){
			player1List.push_back(new Node(1, i));
			player2List.push_back(new Node(2, i));
		}
		debug("After first for\n");
		//read precedence relation
		for (int i = 0; i < m; ++i){
			int player1Deck, player2Deck;
			char relation;
			scanf("%d %c %d", &player1Deck, &relation, &player2Deck);
			//printf("%d %c %d\n", player1Deck, relation, player2Deck);
			if (relation == '<'){
				player1List[player1Deck-1]->predecessors.push_back(player2Deck-1);
				player1List[player1Deck-1]->score--;
				player2List[player2Deck-1]->successors.push_back(player1Deck-1);
				player2List[player2Deck-1]->score++;
			} else {
				player2List[player2Deck-1]->predecessors.push_back(player1Deck-1);
				player2List[player2Deck-1]->score--;
				player1List[player1Deck-1]->successors.push_back(player2Deck-1);
				player1List[player1Deck-1]->score++;
			}
		}
		debug("After second for\n");

		player1Hand = std::set<Node*, LessScore>(player1List.begin(), player1List.end());
		player2Hand = std::set<Node*, LessScore>(player2List.begin(), player2List.end());

		if (player1Hand.size() != n || player2Hand.size() != n){
			printf("dupa!\n");
		}
		
		debug("After hands creation\n");
		
		//printHand(player2Hand);
		//printHand(player1Hand);
		
		debug("Hand1 size: %d\n", player1Hand.size());
		debug("Hand2 size: %d\n", player2Hand.size());
		
		for (int i = 0; i < n-1; ++i){
			debug("dupa\n");
			std::set<Node*, LessScore>::iterator removed2 = player2Hand.begin();
			Node* removedNode2 = *removed2;
			//std::sort(player2Hand.begin(), player2Hand.end(), lessScoreComparator);
			player2Hand.erase(removed2);
			removeFromPlayersHand(removedNode2);
			//removeFromPlayersHand(player2Hand.back());
			
			debug("Hand1 size: %d\n", player1Hand.size());
			debug("Hand2 size: %d\n", player2Hand.size());

			//player2Hand.pop_back();
			//printHand(player2Hand);
			
			//std::sort(player1Hand.begin(), player1Hand.end(), lessScoreComparator);
			std::set<Node*, LessScore>::iterator removed1 = player1Hand.begin();
			Node* removedNode1 = *removed1;
			//removeFromPlayersHand(player1Hand.back());
			player1Hand.erase(removed1);
			removeFromPlayersHand(removedNode1);
			//player1Hand.pop_back();
			
			//printHand(player1Hand);
			debug("Hand1 size: %d\n", player1Hand.size());
			debug("Hand2 size: %d\n", player2Hand.size());
		}
		
		debug("After all computation\n");
		debug("Hand1 size: %d\n", player1Hand.size());
		debug("Hand2 size: %d\n", player2Hand.size());
		if (player1Hand.size() != 1 || player2Hand.size() != 1){
			printf("dupa!\n");
		}
		
		if ((*(player1Hand.begin()))->score > (*(player2Hand.begin()))->score){
			printf("WYGRANA\n");
		} else if ((*player1Hand.begin())->score == (*player2Hand.begin())->score){
			printf("REMIS\n");
		} else {
			printf("PRZEGRANA\n");
		}

		for (int i = 0; i < player1List.size(); ++i) delete player1List[i];
		for (int i = 0; i < player2List.size(); ++i) delete player2List[i];
		player1List.clear();
		player2List.clear();
		player1Hand.clear();
		player2Hand.clear();
	}
	return 0;
}