#define NDEBUG #include <vector> #include <set> #include <map> #include <memory> #include <cassert> // #define debug(args ...) fprintf(stderr, args) #define debug(args ...) do { } while(0) class Vertex { int m_id; int m_val; std::map<int, int> m_edges; public: explicit Vertex(int id) : m_id(id), m_val(0) { } int id() const { return m_id; } int val() const { return m_val; } void addWin(int v) { int& e = m_edges[v]; assert(e == 0); e = -1; m_val -= 1; } void addLose(int v) { int& e = m_edges[v]; assert(e == 0); e = 1; m_val += 1; } void rmEdge(int v) { auto I = m_edges.find(v); assert(I != m_edges.end()); if (I->second == 1) m_val -= 1; else if (I->second == -1) m_val += 1; else assert(0); m_edges.erase(I); } typedef std::map<int,int>::iterator edge_iterator; edge_iterator edge_begin() { return m_edges.begin(); } edge_iterator edge_end() { return m_edges.end(); } }; struct VertexComp { bool operator()(const Vertex* lhs, const Vertex* rhs) { if (lhs->val() != rhs->val()) return lhs->val() < rhs->val(); return lhs->id() < rhs->id(); } }; class Player { const char* m_name; std::vector< std::pair< std::unique_ptr<Vertex>, std::set<const Vertex *, VertexComp>::iterator > > m_vertices; std::set<const Vertex *, VertexComp> m_vset; public: explicit Player(const char* name, std::size_t n) : m_name(name) { m_vertices.resize(n); for (std::size_t i = 0; i < n; ++i) { m_vertices[i].first.reset(new Vertex(i)); m_vertices[i].second = m_vset.end(); } } void addWin(int src, int dst) { debug("%s.addWin(%d,%d)\n", m_name, src, dst); assert(m_vset.empty()); m_vertices[src].first->addWin(dst); } void addLose(int src, int dst) { debug("%s.addLose(%d,%d)\n", m_name, src, dst); assert(m_vset.empty()); m_vertices[src].first->addLose(dst); } void buildVSet() { debug("%s.buildVSet\n", m_name); for (auto& p: m_vertices) { assert(p.second == m_vset.end()); auto ret = m_vset.insert(p.first.get()); p.second = ret.first; // keep the iterator } debug("vset.size = %lu\n", m_vset.size()); } std::unique_ptr<Vertex> popWorstVertex() { debug("%s.popWorstVertex -> ", m_name); assert(m_vset.empty() == false); auto I = m_vset.begin(); const Vertex* ptr = *I; m_vset.erase(I); m_vertices[ptr->id()].second = m_vset.end(); // its is gone debug("%d [val=%d]\n", ptr->id(), ptr->val()); return std::move(m_vertices[ptr->id()].first); } void dump_vset() { debug("{"); for (const Vertex *V : m_vset) { debug(" %p[id=%d,val=%d]", V, V->id(), V->val()); } debug("}\n"); } void rmEdge(int src, int dst) { debug("%s.rmEdge(%d,%d)\n", m_name, src, dst); debug("A\n"); assert(!m_vset.empty()); debug("B\n"); Vertex *ptr = m_vertices[src].first.get(); debug("C\n"); std::set<const Vertex *, VertexComp>::iterator I = m_vertices[src].second; debug("D\n"); assert(I != m_vset.end()); debug("E\n"); ptr->rmEdge(dst); debug("F\n"); // dump_vset(); debug("V:%p[id=%d,val=%d]\n", *I, (*I)->id(), (*I)->val()); m_vset.erase(I); debug("G\n"); I = m_vset.insert(ptr).first; m_vertices[src].second = I; debug("H\n"); } }; static int test(int n, int m) { Player A("A", n); Player B("B", n); while (m--) { int a, b; char op[4]; scanf("%d%s%d", &a, op, &b); --a; --b; if (op[0] == '>') { A.addWin(a, b); B.addLose(b, a); } else if (op[0] == '<') { B.addWin(b, a); A.addLose(a, b); } else { assert(0); } } A.buildVSet(); B.buildVSet(); while (n --> 1) { debug("MOVE A\n"); { std::unique_ptr<Vertex> Vb = B.popWorstVertex(); for (auto I = Vb->edge_begin(), E = Vb->edge_end(); I != E; ++I) { int dst = Vb->id(); int src = I->first; assert(I->second == 1 || I->second == -1); A.rmEdge(src, dst); } } debug("MOVE B\n"); { std::unique_ptr<Vertex> Va = A.popWorstVertex(); for (auto I = Va->edge_begin(), E = Va->edge_end(); I != E; ++I) { int dst = Va->id(); int src = I->first; B.rmEdge(src, dst); } } } debug("\n\n\n"); bool isWinA = A.popWorstVertex()->val() < 0; bool isWinB = B.popWorstVertex()->val() < 0; assert(!isWinA || !isWinB); if (isWinA) return 1; if (isWinB) return -1; return 0; } int main() { int t; scanf("%d", &t); while (t--) { int n, m; scanf("%d%d", &n, &m); int r = test(n, m); switch (r) { case 1: printf("WYGRANA\n"); break; case -1: printf("PRZEGRANA\n"); break; case 0: printf("REMIS\n"); break; default: assert(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 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | #define NDEBUG #include <vector> #include <set> #include <map> #include <memory> #include <cassert> // #define debug(args ...) fprintf(stderr, args) #define debug(args ...) do { } while(0) class Vertex { int m_id; int m_val; std::map<int, int> m_edges; public: explicit Vertex(int id) : m_id(id), m_val(0) { } int id() const { return m_id; } int val() const { return m_val; } void addWin(int v) { int& e = m_edges[v]; assert(e == 0); e = -1; m_val -= 1; } void addLose(int v) { int& e = m_edges[v]; assert(e == 0); e = 1; m_val += 1; } void rmEdge(int v) { auto I = m_edges.find(v); assert(I != m_edges.end()); if (I->second == 1) m_val -= 1; else if (I->second == -1) m_val += 1; else assert(0); m_edges.erase(I); } typedef std::map<int,int>::iterator edge_iterator; edge_iterator edge_begin() { return m_edges.begin(); } edge_iterator edge_end() { return m_edges.end(); } }; struct VertexComp { bool operator()(const Vertex* lhs, const Vertex* rhs) { if (lhs->val() != rhs->val()) return lhs->val() < rhs->val(); return lhs->id() < rhs->id(); } }; class Player { const char* m_name; std::vector< std::pair< std::unique_ptr<Vertex>, std::set<const Vertex *, VertexComp>::iterator > > m_vertices; std::set<const Vertex *, VertexComp> m_vset; public: explicit Player(const char* name, std::size_t n) : m_name(name) { m_vertices.resize(n); for (std::size_t i = 0; i < n; ++i) { m_vertices[i].first.reset(new Vertex(i)); m_vertices[i].second = m_vset.end(); } } void addWin(int src, int dst) { debug("%s.addWin(%d,%d)\n", m_name, src, dst); assert(m_vset.empty()); m_vertices[src].first->addWin(dst); } void addLose(int src, int dst) { debug("%s.addLose(%d,%d)\n", m_name, src, dst); assert(m_vset.empty()); m_vertices[src].first->addLose(dst); } void buildVSet() { debug("%s.buildVSet\n", m_name); for (auto& p: m_vertices) { assert(p.second == m_vset.end()); auto ret = m_vset.insert(p.first.get()); p.second = ret.first; // keep the iterator } debug("vset.size = %lu\n", m_vset.size()); } std::unique_ptr<Vertex> popWorstVertex() { debug("%s.popWorstVertex -> ", m_name); assert(m_vset.empty() == false); auto I = m_vset.begin(); const Vertex* ptr = *I; m_vset.erase(I); m_vertices[ptr->id()].second = m_vset.end(); // its is gone debug("%d [val=%d]\n", ptr->id(), ptr->val()); return std::move(m_vertices[ptr->id()].first); } void dump_vset() { debug("{"); for (const Vertex *V : m_vset) { debug(" %p[id=%d,val=%d]", V, V->id(), V->val()); } debug("}\n"); } void rmEdge(int src, int dst) { debug("%s.rmEdge(%d,%d)\n", m_name, src, dst); debug("A\n"); assert(!m_vset.empty()); debug("B\n"); Vertex *ptr = m_vertices[src].first.get(); debug("C\n"); std::set<const Vertex *, VertexComp>::iterator I = m_vertices[src].second; debug("D\n"); assert(I != m_vset.end()); debug("E\n"); ptr->rmEdge(dst); debug("F\n"); // dump_vset(); debug("V:%p[id=%d,val=%d]\n", *I, (*I)->id(), (*I)->val()); m_vset.erase(I); debug("G\n"); I = m_vset.insert(ptr).first; m_vertices[src].second = I; debug("H\n"); } }; static int test(int n, int m) { Player A("A", n); Player B("B", n); while (m--) { int a, b; char op[4]; scanf("%d%s%d", &a, op, &b); --a; --b; if (op[0] == '>') { A.addWin(a, b); B.addLose(b, a); } else if (op[0] == '<') { B.addWin(b, a); A.addLose(a, b); } else { assert(0); } } A.buildVSet(); B.buildVSet(); while (n --> 1) { debug("MOVE A\n"); { std::unique_ptr<Vertex> Vb = B.popWorstVertex(); for (auto I = Vb->edge_begin(), E = Vb->edge_end(); I != E; ++I) { int dst = Vb->id(); int src = I->first; assert(I->second == 1 || I->second == -1); A.rmEdge(src, dst); } } debug("MOVE B\n"); { std::unique_ptr<Vertex> Va = A.popWorstVertex(); for (auto I = Va->edge_begin(), E = Va->edge_end(); I != E; ++I) { int dst = Va->id(); int src = I->first; B.rmEdge(src, dst); } } } debug("\n\n\n"); bool isWinA = A.popWorstVertex()->val() < 0; bool isWinB = B.popWorstVertex()->val() < 0; assert(!isWinA || !isWinB); if (isWinA) return 1; if (isWinB) return -1; return 0; } int main() { int t; scanf("%d", &t); while (t--) { int n, m; scanf("%d%d", &n, &m); int r = test(n, m); switch (r) { case 1: printf("WYGRANA\n"); break; case -1: printf("PRZEGRANA\n"); break; case 0: printf("REMIS\n"); break; default: assert(0); } } } |