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

using namespace std;

int t, n, m, a, b;
char c;

int main() {
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        vector<vector<int>> winA(n), winB(n), failA(n), failB(n);
        vector<int> pointsA(n), pointsB(n);
        set<pair<int, int>> A, B;
        for(int i = 0; i < m; ++i) {
            scanf("%d %c%d", &a, &c, &b);
            --a;
            --b;
            if(c == '>') {
                winA[a].push_back(b);
                failB[b].push_back(a);
            }
            else {
                failA[a].push_back(b);
                winB[b].push_back(a);
            }
        }
        for(int i = 0; i < n; ++i) {
            pointsA[i] += winA[i].size() - failA[i].size();
            pointsB[i] += winB[i].size() - failB[i].size();
        }
        for(int i = 0; i < n; ++i) {
            A.insert(make_pair(pointsA[i], i));
            B.insert(make_pair(pointsB[i], i));
        }

        for(int i = 0; i < n - 1; ++i) {
            auto endB = B.end();
            endB--;
            for(auto &&j : winB[endB->second]) {
                auto it = A.find(make_pair(pointsA[j], j));
                if(it != A.end()) {
                    A.erase(it);
                    ++pointsA[j];
                    A.insert(make_pair(pointsA[j], j));
                }
            }
            for(auto &&j : failB[endB->second]) {
                auto it = A.find(make_pair(pointsA[j], j));
                if(it != A.end()) {
                    A.erase(it);
                    --pointsA[j];
                    A.insert(make_pair(pointsA[j], j));
                }
            }
            B.erase(endB);

            auto endA = A.end();
            endA--;
            for(auto &&j : winA[endA->second]) {
                auto it = B.find(make_pair(pointsB[j], j));
                if(it != B.end()) {
                    B.erase(it);
                    ++pointsB[j];
                    B.insert(make_pair(pointsB[j], j));
                }
            }
            for(auto &&j : failA[endA->second]) {
                auto it = B.find(make_pair(pointsB[j], j));
                if(it != B.end()) {
                    B.erase(it);
                    --pointsB[j];
                    B.insert(make_pair(pointsB[j], j));
                }
            }
            A.erase(endA);
        }
        bool temp = false;
        for(auto &&j : winA[A.begin()->second]) {
            if(j == B.begin()->second)
                temp = true;
        }
        if(temp)
            printf("WYGRANA\n");
        else {
            for(auto &&j : failA[A.begin()->second]) {
                if(j == B.begin()->second)
                    temp = true;
            }
            if(temp)
                printf("PRZEGRANA\n");
            else
                printf("REMIS\n");
        }
        //printf("%d %d\n", A.begin()->second, B.begin()->second);
    }
}