#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define fi first #define se second using namespace std; const int MN = 100005; int in[MN][2], out[MN][2], del[MN][2]; vector <int> IN[MN][2], OUT[MN][2]; priority_queue < pair < pair < int, int>, int > > Q1,Q2; int main() { int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d%d", &n, &m); while(m--) { int a, b; char Z[2]; scanf("%d%s%d", &a, Z, &b); if(Z[0] == '<') { IN[a][0].pb(b); ++in[a][0]; OUT[b][1].pb(a); ++out[b][1]; } else { IN[b][1].pb(a); ++in[b][1]; OUT[a][0].pb(b); ++out[a][0]; } } for(int i = 1; i <= n; ++i) { Q1.push(mp(mp(out[i][0], -in[i][0]), i)); Q2.push(mp(mp(out[i][1], -in[i][1]), i)); } for(int i = 1; i < n; ++i) { while(del[Q2.top().se][1] || (-in[Q2.top().se][1] != Q2.top().fi.se || out[Q2.top().se][1] != Q2.top().fi.fi)) Q2.pop(); pair < pair < int, int>, int > curr = Q2.top(); Q2.pop(); del[curr.se][1] = 1; int s = OUT[curr.se][1].size(); for(int j = 0; j < s; ++j) { --in[OUT[curr.se][1][j]][0]; Q1.push(mp(mp(out[OUT[curr.se][1][j]][0], -in[OUT[curr.se][1][j]][0]), OUT[curr.se][1][j])); } s = IN[curr.se][1].size(); for(int j = 0; j < s; ++j) { --out[IN[curr.se][1][j]][0]; Q1.push(mp(mp(out[IN[curr.se][1][j]][0], -in[IN[curr.se][1][j]][0]), IN[curr.se][1][j])); } while(del[Q1.top().se][0] || (-in[Q1.top().se][0] != Q1.top().fi.se || out[Q1.top().se][0] != Q1.top().fi.fi)) Q1.pop(); curr = Q1.top(); Q1.pop(); del[curr.se][0] = 1; s = OUT[curr.se][0].size(); for(int j = 0; j < s; ++j) { --in[OUT[curr.se][0][j]][1]; Q2.push(mp(mp(out[OUT[curr.se][0][j]][1], -in[OUT[curr.se][0][j]][1]), OUT[curr.se][0][j])); } s = IN[curr.se][0].size(); for(int j = 0; j < s; ++j) { --out[IN[curr.se][0][j]][1]; Q2.push(mp(mp(out[IN[curr.se][0][j]][1], -in[IN[curr.se][0][j]][1]), IN[curr.se][0][j])); } } int c1, c2; for(int i = 1; i <= n; ++i) { if(!del[i][0]) c1 = i; if(!del[i][1]) c2 = i; } int w = 1; int s = OUT[c1][0].size(); for(int i = 0; i < s; ++i) if(OUT[c1][0][i] == c2) w = 2; s = OUT[c2][1].size(); for(int i = 0; i < s; ++i) if(OUT[c2][1][i] == c1) w = 0; if(w == 2) printf("WYGRANA"); if(w == 1) printf("REMIS"); if(w == 0) printf("PRZEGRANA"); printf("\n"); while(!Q1.empty()) Q1.pop(); while(!Q2.empty()) Q2.pop(); for(int i = 0; i<=n; ++i) { OUT[i][0].clear(); OUT[i][1].clear(); IN[i][0].clear(); IN[i][1].clear(); in[i][0] = in[i][1] = out[i][0] = out[i][1] = del[i][0] = del[i][1] = 0; } } }
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 | #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define fi first #define se second using namespace std; const int MN = 100005; int in[MN][2], out[MN][2], del[MN][2]; vector <int> IN[MN][2], OUT[MN][2]; priority_queue < pair < pair < int, int>, int > > Q1,Q2; int main() { int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d%d", &n, &m); while(m--) { int a, b; char Z[2]; scanf("%d%s%d", &a, Z, &b); if(Z[0] == '<') { IN[a][0].pb(b); ++in[a][0]; OUT[b][1].pb(a); ++out[b][1]; } else { IN[b][1].pb(a); ++in[b][1]; OUT[a][0].pb(b); ++out[a][0]; } } for(int i = 1; i <= n; ++i) { Q1.push(mp(mp(out[i][0], -in[i][0]), i)); Q2.push(mp(mp(out[i][1], -in[i][1]), i)); } for(int i = 1; i < n; ++i) { while(del[Q2.top().se][1] || (-in[Q2.top().se][1] != Q2.top().fi.se || out[Q2.top().se][1] != Q2.top().fi.fi)) Q2.pop(); pair < pair < int, int>, int > curr = Q2.top(); Q2.pop(); del[curr.se][1] = 1; int s = OUT[curr.se][1].size(); for(int j = 0; j < s; ++j) { --in[OUT[curr.se][1][j]][0]; Q1.push(mp(mp(out[OUT[curr.se][1][j]][0], -in[OUT[curr.se][1][j]][0]), OUT[curr.se][1][j])); } s = IN[curr.se][1].size(); for(int j = 0; j < s; ++j) { --out[IN[curr.se][1][j]][0]; Q1.push(mp(mp(out[IN[curr.se][1][j]][0], -in[IN[curr.se][1][j]][0]), IN[curr.se][1][j])); } while(del[Q1.top().se][0] || (-in[Q1.top().se][0] != Q1.top().fi.se || out[Q1.top().se][0] != Q1.top().fi.fi)) Q1.pop(); curr = Q1.top(); Q1.pop(); del[curr.se][0] = 1; s = OUT[curr.se][0].size(); for(int j = 0; j < s; ++j) { --in[OUT[curr.se][0][j]][1]; Q2.push(mp(mp(out[OUT[curr.se][0][j]][1], -in[OUT[curr.se][0][j]][1]), OUT[curr.se][0][j])); } s = IN[curr.se][0].size(); for(int j = 0; j < s; ++j) { --out[IN[curr.se][0][j]][1]; Q2.push(mp(mp(out[IN[curr.se][0][j]][1], -in[IN[curr.se][0][j]][1]), IN[curr.se][0][j])); } } int c1, c2; for(int i = 1; i <= n; ++i) { if(!del[i][0]) c1 = i; if(!del[i][1]) c2 = i; } int w = 1; int s = OUT[c1][0].size(); for(int i = 0; i < s; ++i) if(OUT[c1][0][i] == c2) w = 2; s = OUT[c2][1].size(); for(int i = 0; i < s; ++i) if(OUT[c2][1][i] == c1) w = 0; if(w == 2) printf("WYGRANA"); if(w == 1) printf("REMIS"); if(w == 0) printf("PRZEGRANA"); printf("\n"); while(!Q1.empty()) Q1.pop(); while(!Q2.empty()) Q2.pop(); for(int i = 0; i<=n; ++i) { OUT[i][0].clear(); OUT[i][1].clear(); IN[i][0].clear(); IN[i][1].clear(); in[i][0] = in[i][1] = out[i][0] = out[i][1] = del[i][0] = del[i][1] = 0; } } } |