Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; struct edge { int to, nxt; }; edge G[maxn]; int head[maxn], cnt; int h[maxn], deg[maxn]; queue<int> Q; int a[maxn]; bool vis[maxn]; int bel[maxn]; int nim[maxn]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", h + i); int u, v; while (m--) { scanf("%d %d", &u, &v); G[++cnt] = {v, head[u]}, head[u] = cnt; deg[v]++; } for (int i = 1; i <= n; i++) if (!deg[i]) Q.push(i); int c = 0; while (!Q.empty()) { u = Q.front(), Q.pop(); a[c++] = u; for (int i = head[u]; i; i = G[i].nxt) if (!(--deg[G[i].to])) Q.push(G[i].to); } int val = 0; while (c--) { for (int i = head[a[c]]; i; i = G[i].nxt) vis[bel[G[i].to]] = true; while (vis[bel[a[c]]]) bel[a[c]]++; if (bel[a[c]] > val) val = bel[a[c]]; for (int i = head[a[c]]; i; i = G[i].nxt) vis[bel[G[i].to]] = false; } for (int i = 1; i <= n; i++) nim[bel[i]] ^= h[i]; int p = -1; for (int i = val; i >= 0; i--) if (nim[i]) { p = i; break; } if (p == -1) { printf("LOSE\n"); return 0; } for (int i = 1; i <= n; i++) if (bel[i] == p && (nim[bel[i]] ^ h[i]) <= h[i]) { h[i] ^= nim[bel[i]], vis[bel[i]] = true; for (int j = head[i]; j; j = G[j].nxt) if (!vis[bel[G[j].to]]) h[G[j].to] ^= nim[bel[G[j].to]], vis[bel[G[j].to]] = true; break; } printf("WIN\n"); for (int i = 1; i <= n; i++) printf("%d ", h[i]); printf("\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m; int a[200000 + 5]; vector<int> E[200000 + 5]; int ind[200000 + 5]; int rk[200000 + 5]; int sg[200000 + 5]; void topo() { static bool vis[200000 + 5]; queue<int> q; vector<int> seq; for (int i = 1; i <= n; i++) if (ind[i] == 0) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(); seq.push_back(x); for (int y : E[x]) { ind[y]--; if (ind[y] == 0) q.push(y); } } for (int i = seq.size() - 1; i >= 0; i--) { int x = seq[i]; for (int y : E[x]) vis[rk[y]] = 1; while (vis[rk[x]]) rk[x]++; for (int y : E[x]) vis[rk[y]] = 0; } } int main() { int u, v; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) { scanf("%d %d", &u, &v); E[u].push_back(v); ind[v]++; } topo(); for (int i = 1; i <= n; i++) sg[rk[i]] ^= a[i]; int pos = -1; for (int i = n; i >= 0; i--) { if (sg[i] != 0) { pos = i; break; } } if (pos == -1) puts("LOSE"); else { puts("WIN"); for (int i = 1; i <= n; i++) { if (rk[i] == pos) { if ((sg[rk[i]] ^ a[i]) <= a[i]) { a[i] ^= sg[rk[i]]; sg[rk[i]] = 0; for (int y : E[i]) { a[y] ^= sg[rk[y]]; sg[rk[y]] = 0; } } } } for (int i = 1; i <= n; i++) printf("%d ", a[i]); } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MaxN = 2e5 + 100; int stack_sizes[MaxN]; vector<int> adj[MaxN]; int N, M; int vertex_group_idx[MaxN]; bool visited[MaxN]; int group_xor[MaxN]; vector<int> vertex_groups[MaxN]; void DfsMakeLayers(int vert) { visited[vert] = true; vector<int> seen_layers; for (int s : adj[vert]) { if (!visited[s]) { DfsMakeLayers(s); } seen_layers.push_back(vertex_group_idx[s]); } sort(seen_layers.begin(), seen_layers.end()); seen_layers.resize(unique(seen_layers.begin(), seen_layers.end()) - seen_layers.begin()); seen_layers.push_back(1e9); while (seen_layers[vertex_group_idx[vert]] == vertex_group_idx[vert]) { ++vertex_group_idx[vert]; } const int group_id = vertex_group_idx[vert]; group_xor[group_id] ^= stack_sizes[vert]; vertex_groups[group_id].push_back(vert); } int main() { ios_base::sync_with_stdio(0); cin >> N >> M; for (int i = 0; i < N; ++i) { cin >> stack_sizes[i]; } for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); } for (int vert = 0; vert < N; ++vert) { if (!visited[vert]) { DfsMakeLayers(vert); } } if (count(group_xor, group_xor + N, 0) == N) { cout << "LOSE\n"; return 0; } int last_idx = N; while (group_xor[last_idx] == 0) --last_idx; int touched_vertex = -1; for (int vert : vertex_groups[last_idx]) { if ((stack_sizes[vert] ^ group_xor[last_idx]) < stack_sizes[vert]) { touched_vertex = vert; break; } } assert(touched_vertex != -1); stack_sizes[touched_vertex] ^= group_xor[last_idx]; group_xor[last_idx] = 0; for (int neigh : adj[touched_vertex]) { const int group_idx = vertex_group_idx[neigh]; stack_sizes[neigh] ^= group_xor[group_idx]; group_xor[group_idx] = 0; } assert(count(group_xor, group_xor + N, 0) == N); cout << "WIN\n"; for (int i = 0; i < N; ++i) cout << stack_sizes[i] << " "; cout << "\n"; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 10; const ll inf_ll = 1e18 + 10; const int N = 2e5 + 5; int h[N], f[N], l[N], v[N]; vector<int> adj[N]; void dfs(int i) { v[i] = 1; vector<int> d; for (auto& j : adj[i]) { if (!v[j]) dfs(j); d.push_back(l[j]); } sort((d).begin(), (d).end()); for (auto& x : d) if (l[i] == x) l[i]++; f[l[i]] ^= h[i]; } void dfs2(int i) { ; for (auto& j : adj[i]) if (l[j] == l[i] - 1) { dfs2(j); break; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> h[i]; while (m--) { int x, y; cin >> x >> y; adj[x - 1].push_back(y - 1); } for (int i = 0; i < n; i++) if (!v[i]) dfs(i); if (*max_element(f, f + n) == 0) cout << "LOSE\n"; else { cout << "WIN\n"; int k = 0; for (int i = 0; i < n; i++) if (f[i] != 0) k = i; int r = 1 << (31 - __builtin_clz(f[k])); ; for (int i = 0; i < n; i++) if (l[i] == k && (r & h[i])) { h[i] ^= f[l[i]]; for (auto& j : adj[i]) h[j] ^= f[l[j]], f[l[j]] = 0; break; } for (int i = 0; i < n; i++) cout << h[i] << " \n"[i == n - 1]; } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; bool t = false; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') t = true, ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return t ? -x : x; } vector<int> E[200200]; int n, m, h[200200], sg[200200], sum[200200]; int dg[200200], Q[200200], vis[200200]; void Topsort() { int h = 1, t = 0; for (int i = 1; i <= n; ++i) if (!dg[i]) Q[++t] = i; while (h <= t) { int u = Q[h++]; for (int v : E[u]) if (!--dg[v]) Q[++t] = v; } } int main() { n = read(); m = read(); for (int i = 1; i <= n; ++i) h[i] = read(); for (int i = 1; i <= m; ++i) { int u = read(), v = read(); E[u].push_back(v); ++dg[v]; } Topsort(); for (int i = n; i; --i) { int u = Q[i]; for (int v : E[u]) vis[sg[v]] = i; while (vis[sg[u]] == i) ++sg[u]; sum[sg[u]] ^= h[u]; } for (int i = n; ~i; --i) if (sum[i]) { int pos; for (int j = 1; j <= n; ++j) if (sg[j] == i && h[j] > (sum[i] ^ h[j])) pos = j; h[pos] ^= sum[i]; for (int v : E[pos]) h[v] ^= sum[sg[v]], sum[sg[v]] = 0; puts("WIN"); for (int j = 1; j <= n; ++j) printf("%d ", h[j]); puts(""); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<vector<int>> adj(n); vector<int> deg(n); for (int i = 0; i < m; ++i) { int from, to; cin >> from >> to; --from; --to; adj[from].push_back(to); ++deg[to]; } vector<int> order; for (int i = 0; i < n; ++i) { if (!deg[i]) { order.push_back(i); } } for (int i = 0; i < n; ++i) { int x = order[i]; for (auto y : adj[x]) { if (!--deg[y]) { order.push_back(y); } } } vector<int> last(n + 1, -1); vector<int> nim(n); vector<int> sg(n); for (int i = n - 1; ~i; --i) { int x = order[i]; for (auto y : adj[x]) { last[sg[y]] = x; } while (last[sg[x]] == x) { ++sg[x]; } nim[sg[x]] ^= a[x]; } int high = -1; for (int i = 0; i < n; ++i) { if (nim[i]) { high = i; } } if (high == -1) { cout << "LOSE" << "\n"; } else { cout << "WIN" << "\n"; for (int i = 0; i < n; ++i) { if (sg[i] == high && a[i] >= (a[i] ^ nim[sg[i]])) { a[i] ^= nim[sg[i]]; nim[sg[i]] = 0; for (auto j : adj[i]) { a[j] ^= nim[sg[j]]; nim[sg[j]] = 0; } break; } } for (int i = 0; i < n; ++i) { if (i) { cout << " "; } cout << a[i]; } cout << "\n"; } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
java
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<27).start(); } void findCol(int i) { HashSet<Integer> set = new HashSet<>(); for(int j : adj[i]) { if(col[j] == -1) findCol(j); set.add(col[j]); } for(int j = 0; ; ++j) { if(!set.contains(j)) { col[i] = j; break; } } } ArrayList<Integer> adj[]; int col[]; public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int h[] = new int[n]; for(int i = 0; i < n; ++i) h[i] = sc.nextInt(); adj = new ArrayList[n]; for(int i = 0; i < n; ++i) adj[i] = new ArrayList<>(); for(int i = 0; i < m; ++i) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; adj[u].add(v); } col = new int[n]; Arrays.fill(col, -1); for(int i = 0; i < n; ++i) { if(col[i] == -1) findCol(i); } int xor[] = new int[n]; for(int i = 0; i < n; ++i) xor[col[i]] ^= h[i]; int curCol = -1; for(int i = 0; i < n; ++i) { if(xor[i] != 0) curCol = i; } if(curCol == -1) w.print("LOSE"); else { w.println("WIN"); int ind = -1; for(int i = 0; i < n; ++i) { if(col[i] == curCol) { if((h[i] ^ xor[curCol]) < h[i]) ind = i; } } h[ind] ^= xor[col[ind]]; xor[col[ind]] = 0; for(int j : adj[ind]) { h[j] ^= xor[col[j]]; xor[col[j]] = 0; } for(int i : h) w.print(i + " "); } w.close(); } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int T, n, m, head[200010], o = 0, deg[200010], id[200010], val[200010], a[200010]; bool vis[200010]; queue<int> q; struct edge { int to, link, w; } e[400010]; void add_edge(int u, int v) { e[++o].to = v, e[o].link = head[u], head[u] = o, e[o].w = 1; e[++o].to = u, e[o].link = head[v], head[v] = o, e[o].w = 0; deg[u]++; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), add_edge(u, v); for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = e[i].link) if (e[i].w) vis[id[e[i].to]] = true; while (vis[id[u]]) id[u]++; for (int i = head[u]; i; i = e[i].link) if (e[i].w) vis[id[e[i].to]] = false; val[id[u]] ^= a[u]; for (int i = head[u]; i; i = e[i].link) if (!e[i].w) { if (!(--deg[e[i].to])) q.push(e[i].to); } } int pos = -1; for (int i = 0; i <= n; i++) if (val[i]) pos = i; if (pos < 0) return puts("LOSE"), 0; for (int i = 1; i <= n; i++) if (id[i] == pos) { if ((val[id[i]] ^ a[i]) >= a[i]) continue; a[i] ^= val[id[i]], val[id[i]] = 0; for (int j = head[i]; j; j = e[j].link) if (e[j].w) { a[e[j].to] ^= val[id[e[j].to]], val[id[e[j].to]] = 0; } break; } puts("WIN"); for (int i = 1; i <= n; i++) printf("%d ", a[i]); puts(""); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, a[200005], in[200005], tag[200005], sg[200005], sum[200005]; vector<int> Ef[200005], Eb[200005], tt[200005]; queue<int> q; void tpsort() { for (int i = 1; i <= n; i++) if (!in[i]) q.push(i), tt[0].push_back(i); while (!q.empty()) { int x = q.front(); q.pop(); for (auto y : Eb[x]) { if (!(--in[y])) { for (auto z : Ef[y]) tag[sg[z]] = y; while (tag[sg[y]] == y) ++sg[y]; q.push(y); tt[sg[y]].push_back(y); } } } } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1, u, v; i <= m; i++) { scanf("%d %d", &u, &v); Ef[u].push_back(v); Eb[v].push_back(u); ++in[u]; } tpsort(); for (int i = 0; i <= n; i++) { for (auto x : tt[i]) sum[i] ^= a[x]; } for (int i = n; ~i; i--) { if (!sum[i]) continue; int id = 0; for (auto x : tt[i]) { if (a[x] > (sum[i] ^ a[x])) { id = x; break; } } a[id] ^= sum[i]; for (auto y : Ef[id]) { a[y] ^= sum[sg[y]]; sum[sg[y]] = 0; } puts("WIN"); for (int j = 1; j <= n; j++) printf("%d ", a[j]); puts(""); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> ve1[maxn], ve2[maxn]; int rd[maxn]; int n, m; int sm[maxn], h[maxn]; queue<int> q; int mx[maxn]; int vis[maxn]; vector<int> veo[maxn]; bool mark[maxn]; void ptans() { for (int i = 1; i <= n; i++) { printf("%d ", h[i]); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &h[i]); } for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); ve1[x].push_back(y); ve2[y].push_back(x); rd[x]++; } for (int i = 1; i <= n; i++) { if (!rd[i]) q.push(i); } while (q.size()) { int x = q.front(); q.pop(); for (auto y : ve1[x]) { vis[mx[y]] = x; } for (auto y : ve2[x]) { if (--rd[y] == 0) q.push(y); } for (mx[x] = 0; vis[mx[x]] == x; ++mx[x]) ; } for (int i = 1; i <= n; i++) { sm[mx[i]] ^= h[i]; veo[mx[i]].push_back(i); } for (int p = n; p >= 0; --p) { if (sm[p]) { puts("WIN"); for (auto x : veo[p]) { if ((h[x] ^ sm[p]) < h[x]) { h[x] ^= sm[p]; for (auto y : ve1[x]) { if (mark[mx[y]]) continue; mark[mx[y]] = 1; h[y] ^= sm[mx[y]]; } ptans(); return 0; } } } } puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int h[200000]; vector<int> node[200000]; int done[200000]; vector<int> topo; void dfs(int p) { if (done[p]++) return; for (int i : node[p]) { dfs(i); } topo.push_back(p); } int grundy[200000]; int grundy2[200000]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> h[i]; while (m--) { int a, b; cin >> a >> b; a--, b--; node[a].push_back(b); } for (int i = 0; i < n; i++) dfs(i); for (int p : topo) { vector<int> v; for (int i : node[p]) v.push_back(grundy[i]); sort(v.begin(), v.end()); int x = 0; while (binary_search(v.begin(), v.end(), x)) x++; grundy[p] = x; grundy2[x] ^= h[p]; } int high = 0; long long lose = 1; for (int i = 0; i < n; i++) if (grundy2[i]) { lose = 0; high = max(high, i); } if (lose) { cout << "LOSE" << endl; return 0; } for (int p = 0; p < n; p++) { int x = grundy[p]; if (x == high && (h[p] >> 31 - __builtin_clz(grundy2[x]) & 1)) { assert(grundy2[x] >> 31 - __builtin_clz(grundy2[x]) & 1); h[p] ^= grundy2[x]; grundy2[x] = 0; for (int i : node[p]) { h[i] ^= grundy2[grundy[i]]; grundy2[grundy[i]] = 0; } cout << "WIN" << endl; for (int i = 0; i < n; i++) cout << h[i] << ' '; cout << endl; return 0; } } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read() { bool f = 1; int x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = 0; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) - 48 + c; c = getchar(); } return f ? x : x * -1; } char cr[200]; int tt; inline void print(register int x, register char k = '\n') { if (!x) putchar('0'); if (x < 0) putchar('-'), x = -x; while (x) cr[++tt] = x % 10 + '0', x /= 10; while (tt) putchar(cr[tt--]); putchar(k); } const int maxn = 2e5 + 7; vector<int> e[maxn]; queue<int> q; int a[maxn], mex[maxn], nim[maxn], n, m; int vis[maxn], wh[maxn]; int d[maxn]; bool dfs(int u) { vis[u] = -1; for (int v : e[u]) { if (vis[v] == -1) return 0; if (vis[v] == 0) if (!dfs(v)) return 0; } vis[u] = 1; q.push(u); return 1; } signed main() { n = read(); m = read(); for (int i = 1; i <= n; i++) { a[i] = read(); } for (int i = 1; i <= m; i++) { int u = read(), v = read(); e[u].push_back(v); } for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i); } for (int i = 1; i <= n; i++) { int u = q.front(); q.pop(); mex[u] = 1; for (int v : e[u]) wh[mex[v]] = 1; while (wh[mex[u]]) { mex[u]++; } nim[mex[u]] ^= a[u]; for (int v : e[u]) wh[mex[v]] = 0; } bool f = 0; int tmp = 0; for (int i = n; i; i--) { if (nim[i]) { f = 1; tmp = i; break; } } if (!f) { puts("LOSE"); return 0; } puts("WIN"); for (int u = 1; u <= n; u++) { if (mex[u] == tmp && (a[u] ^ nim[tmp]) < a[u]) { a[u] = nim[mex[u]] ^= a[u]; d[mex[u]] = 1; for (int v : e[u]) { if (nim[mex[v]] && !d[mex[v]]) { a[v] = nim[mex[v]] ^= a[v]; d[mex[v]] = 1; } } for (int i = 1; i <= n; i++) { print(a[i], ' '); } return 0; } } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int rd() { int x = 0, w = 1; char ch = 0; while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch ^ 48); ch = getchar(); } return x * w; } int to[N], nt[N], hd[N], dg[N], tot = 1; void adde(int x, int y) { ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot; } int n, m, sg[N], sm[N], a[N], sq[N], ts, vs[N], ti; queue<int> q; int main() { n = rd(), m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= m; ++i) { int x = rd(), y = rd(); adde(x, y), ++dg[y]; } for (int i = 1; i <= n; ++i) if (!dg[i]) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(); sq[++ts] = x; for (int i = hd[x]; i; i = nt[i]) { int y = to[i]; --dg[y]; if (!dg[y]) q.push(y); } } for (int i = n; i; --i) { int x = sq[i]; ++ti; for (int j = hd[x]; j; j = nt[j]) vs[sg[to[j]]] = ti; while (vs[sg[x]] == ti) ++sg[x]; } for (int i = 1; i <= n; ++i) sm[sg[i]] ^= a[i]; for (int i = n; ~i; --i) if (sm[i]) { int x = 1; while (sg[x] != i || (a[x] ^ sm[i]) > a[x]) ++x; a[x] = a[x] ^ sm[i]; ++ti; for (int j = hd[x]; j; j = nt[j]) { int y = to[j]; if (sg[y] >= i || vs[sg[y]] == ti) continue; vs[sg[y]] = ti, a[y] = a[y] ^ sm[sg[y]]; } puts("WIN"); for (int j = 1; j <= n; ++j) printf("%d ", a[j]); exit(0); } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxik = 2e5 + 10; int g[maxik]; vector<int> rodes[maxik]; void dfs(int curr) { bool us[rodes[curr].size() + 1]; memset(us, 0, rodes[curr].size() + 1); for (int i = 0; i < rodes[curr].size(); i++) { int neighbour = rodes[curr][i]; if (g[neighbour] == -1) { dfs(neighbour); } if (g[neighbour] <= rodes[curr].size()) { us[g[neighbour]] = 1; } } for (int i = 0; i > -1; i++) { if (us[i] == 0) { g[curr] = i; break; } } } int main() { int n, m; cin >> n >> m; int tax[n], value[n]; for (int i = 0; i < n; i++) { cin >> tax[i]; value[i] = 0; g[i] = -1; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; rodes[u].push_back(v); } for (int i = 0; i < n; i++) { if (g[i] == -1) { dfs(i); } value[g[i]] = value[g[i]] ^ tax[i]; } for (int i = n - 1; i >= 0; i--) { if (value[i]) { int befor = -1; for (int j = 0; j < n; j++) { if (g[j] == i && (value[i] ^ tax[j]) < tax[j]) { befor = j; } } tax[befor] = value[i] ^ tax[befor]; value[i] = 0; for (int j = 0; j < rodes[befor].size(); j++) { int neighbour = rodes[befor][j]; tax[neighbour] = value[g[neighbour]] ^ tax[neighbour]; value[g[neighbour]] = 0; } cout << "WIN\n"; for (int j = 0; j < n; j++) { cout << tax[j] << ' '; } return 0; } } cout << "LOSE"; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int max_N = 202401; int n, m, A[max_N], topo[max_N], mex[max_N], xor_sum[max_N]; std::vector<int> G[max_N]; int read_int() { char c = getchar(); int ans = 0; bool neg = false; while (!isdigit(c)) neg |= (c == '-'), c = getchar(); while (isdigit(c)) ans = 10 * ans + c - '0', c = getchar(); return neg ? -ans : ans; } void write_int(int x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + '0'); else write_int(x / 10), putchar(x % 10 + '0'); } int min(int x, int y) { return x < y ? x : y; } int max(int x, int y) { return x > y ? x : y; } void _min(int &x, int y) { if (x > y) x = y; } void _max(int &x, int y) { if (x < y) x = y; } void topo_sort() { static int deg[max_N]; for (int i = 1; i <= n; i++) for (int j : G[i]) deg[j]++; std::queue<int> Q; for (int i = 1; i <= n; i++) if (!deg[i]) Q.push(i); while (!Q.empty()) { int cur = Q.front(); Q.pop(); topo[++*topo] = cur; for (int i : G[cur]) if (!--deg[i]) Q.push(i); } } int main() { n = read_int(), m = read_int(); for (int i = 1; i <= n; i++) A[i] = read_int(); for (int i = 1, u, v; i <= m; i++) u = read_int(), v = read_int(), G[u].push_back(v); topo_sort(); for (int i = n; i; i--) { static int tmp[max_N]; for (int j : G[topo[i]]) tmp[mex[j]]++; while (tmp[mex[topo[i]]]) mex[topo[i]]++; for (int j : G[topo[i]]) tmp[mex[j]]--; xor_sum[mex[topo[i]]] ^= A[topo[i]]; } if (std::count(xor_sum, xor_sum + n + 1, 0) == n + 1) puts("LOSE"); else { puts("WIN"); int X = n, Y = -1; while (xor_sum[X] == 0) X--; for (int i = 1; i <= n; i++) if (mex[i] == X && (A[i] ^ xor_sum[X]) < A[i]) { Y = i; break; } A[Y] ^= xor_sum[X]; for (int i : G[Y]) if (xor_sum[mex[i]]) A[i] ^= xor_sum[mex[i]], xor_sum[mex[i]] = 0; for (int i = 1; i <= n; i++) write_int(A[i]), putchar(i == n ? '\n' : ' '); } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { long long x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(long long a) { if (a < 0) { a = -a; putchar('-'); } if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(long long a) { write(a); puts(""); } inline void wri(long long a) { write(a); putchar(' '); } inline unsigned long long rnd() { return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; } const int N = 200005; int h[N], dp[N], vis[N], to[N], rd[N], q[N]; vector<int> f[N], g[N]; int main() { int n = read(), m = read(); for (int i = (int)(1); i <= (int)(n); i++) h[i] = read(); for (int i = (int)(1); i <= (int)(m); i++) { int s = read(), t = read(); f[s].push_back(t); g[t].push_back(s); rd[s]++; } for (int i = (int)(1); i <= (int)(n); i++) if (!rd[i]) q[++*q] = i; for (int i = (int)(1); i <= (int)(*q); i++) { int t = q[i]; for (auto j : f[t]) vis[dp[j]] = 1; for (int j = 0;; j++) if (!vis[j]) { dp[t] = j; to[dp[t]] ^= h[t]; break; } for (auto j : f[t]) vis[dp[j]] = 0; for (auto j : g[t]) if (--rd[j] == 0) q[++*q] = j; } for (int i = (int)(n); i >= (int)(0); i--) if (to[i]) { puts("WIN"); for (int j = (int)(1); j <= (int)(n); j++) if (dp[j] == i && (h[j] ^ to[i]) < h[j]) { h[j] ^= to[i]; for (auto k : f[j]) { h[k] ^= to[dp[k]]; to[dp[k]] = 0; } break; } for (int i = (int)(1); i <= (int)(n); i++) wri(h[i]); puts(""); return 0; } puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; vector<int> adj[N], in[N], idx[N]; int mex[N], val[N], h[N], deg[N]; bool used[N]; queue<int> lis; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, i, j, k, l, max1; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> h[i]; } for (i = 1; i <= m; i++) { cin >> j >> k; adj[j].push_back(k); deg[j]++; in[k].push_back(j); } for (i = 1; i <= n; i++) { if (!deg[i]) { lis.push(i); } } while (lis.size()) { int x = lis.front(); lis.pop(); for (i = 0; i < adj[x].size(); i++) { used[mex[adj[x][i]]] = true; } for (i = 0; i < in[x].size(); i++) { deg[in[x][i]]--; if (!deg[in[x][i]]) { lis.push(in[x][i]); } } j = 0; while (used[j]) { j++; } for (i = 0; i < adj[x].size(); i++) { used[mex[adj[x][i]]] = false; } idx[j].push_back(x); mex[x] = j; val[j] ^= h[x]; } for (i = n; i > -1; i--) { if (val[i] > 0) { max1 = i; break; } else { if (i == 0) { cout << "LOSE"; return 0; } } } cout << "WIN\n"; for (i = 0; i < idx[max1].size(); i++) { int x = idx[max1][i]; if (h[x] > (h[x] ^ val[max1])) { h[x] ^= val[max1]; val[max1] = 0; for (j = 0; j < adj[x].size(); j++) { if (val[mex[adj[x][j]]]) { h[adj[x][j]] ^= val[mex[adj[x][j]]]; val[mex[adj[x][j]]] = 0; } } break; } } for (i = 1; i <= n; i++) { cout << h[i] << ' '; } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; struct edge { int to, nxt; }; edge G[maxn]; int head[maxn], cnt; int h[maxn], deg[maxn]; queue<int> Q; int a[maxn]; bool vis[maxn]; int bel[maxn]; int nim[maxn]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", h + i); int u, v; while (m--) { scanf("%d %d", &u, &v); G[++cnt] = {v, head[u]}, head[u] = cnt; deg[v]++; } for (int i = 1; i <= n; i++) if (!deg[i]) Q.push(i); int c = 0; while (!Q.empty()) { u = Q.front(), Q.pop(); a[c++] = u; for (int i = head[u]; i; i = G[i].nxt) if (!(--deg[G[i].to])) Q.push(G[i].to); } int val = 0; while (c--) { for (int i = head[a[c]]; i; i = G[i].nxt) vis[bel[G[i].to]] = true; while (vis[bel[a[c]]]) bel[a[c]]++; if (bel[a[c]] > val) val = bel[a[c]]; for (int i = head[a[c]]; i; i = G[i].nxt) vis[bel[G[i].to]] = false; } for (int i = 1; i <= n; i++) nim[bel[i]] ^= h[i]; int p = -1; for (int i = val; i >= 0; i--) if (nim[i]) { p = i; break; } if (p == -1) { printf("LOSE\n"); return 0; } for (int i = 1; i <= n; i++) if (bel[i] == p && (nim[bel[i]] ^ h[i]) <= h[i]) { h[i] ^= nim[bel[i]], vis[bel[i]] = true; for (int j = head[i]; j; j = G[j].nxt) if (!vis[bel[G[j].to]]) h[G[j].to] ^= nim[bel[G[j].to]], vis[bel[G[j].to]] = true; break; } printf("WIN\n"); for (int i = 1; i <= n; i++) printf("%d ", h[i]); printf("\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 998244353, N = 210000; int n, m, dgr[N], cnt, rev[N], lev[N]; long long a[N], sum[N]; vector<int> to[N], pos[N]; priority_queue<int, vector<int>, greater<int> > que; inline void addEdg(int x, int y) { to[x].push_back(y), ++dgr[y]; return; } void topoSort() { queue<int> que; for (register int i = 1; i <= n; ++i) if (!dgr[i]) que.push(i); while (que.size()) { int now = que.front(); que.pop(), rev[++cnt] = now; for (auto &v : to[now]) if (!--dgr[v]) que.push(v); } return; } inline int addMod(int a, int b) { return (a += b) >= mod ? a - mod : a; } inline long long quickpow(long long base, long long pw) { long long ret = 1; while (pw) { if (pw & 1) ret = ret * base % mod; base = base * base % mod, pw >>= 1; } return ret; } template <class T> inline void read(T &x) { x = 0; char ch = getchar(), w = 0; while (!isdigit(ch)) w = (ch == '-'), ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x = w ? -x : x; return; } int main() { int x, y; read(n), read(m); for (register int i = 1; i <= n; ++i) read(a[i]); for (register int i = 1; i <= m; ++i) read(x), read(y), addEdg(x, y); topoSort(); for (register int i = n; i; --i) { int now = rev[i]; while (que.size()) que.pop(); for (auto &v : to[now]) que.push(lev[v]); while (que.size()) { while (que.size() && que.top() != lev[now]) que.pop(); if (que.size()) ++lev[now]; } sum[lev[now]] ^= a[now]; pos[lev[now]].push_back(now); } for (register int i = n; ~i; --i) { if (!sum[i]) continue; printf("WIN\n"); int maxP = 0; for (auto &v : pos[i]) if ((a[v] & sum[i]) >= (a[maxP] & sum[i])) maxP = v; a[maxP] ^= sum[i]; for (auto &v : to[maxP]) a[v] ^= sum[lev[v]], sum[lev[v]] = 0; for (register int j = 1; j <= n; ++j) printf("%lld ", a[j]); return 0; } printf("LOSE\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 200010; int n, m; int a[N], deg[N], sg[N], ans[N]; vector<int> G[N], vec; inline void topo() { queue<int> q; for (int i = (1); i <= (n); ++i) if (!deg[i]) q.push(i); while (!q.empty()) { int f = q.front(); q.pop(); vec.push_back(f); for (int i = (0); i < (G[f].size()); ++i) { deg[G[f][i]]--; if (!deg[G[f][i]]) q.push(G[f][i]); } } bool vis[N] = {0}; for (int i = (n - 1); i >= (0); --i) { for (int j = (0); j < (G[vec[i]].size()); ++j) vis[sg[G[vec[i]][j]]] = 1; while (vis[sg[vec[i]]]) sg[vec[i]]++; for (int j = (0); j < (G[vec[i]].size()); ++j) vis[sg[G[vec[i]][j]]] = 0; } } int main() { n = read(); m = read(); for (int i = (1); i <= (n); ++i) a[i] = read(); for (int i = (1); i <= (m); ++i) { int u = read(), v = read(); G[u].push_back(v); deg[v]++; } topo(); int maxx = -1, maxy = 0, id = -1; for (int i = (1); i <= (n); ++i) { if (maxx < sg[i]) maxx = sg[i]; ans[sg[i]] ^= a[i]; } int pd = 0; for (int i = (0); i <= (n); ++i) pd |= ans[i], maxx = (ans[i] ? i : maxx); for (int i = (1); i <= (n); ++i) if (sg[i] == maxx && (ans[maxx] ^ a[i]) < a[i]) { id = i; break; } if (!pd) return puts("LOSE"), 0; assert(id != -1); puts("WIN"); a[id] ^= ans[sg[id]]; for (int i = (0); i < (G[id].size()); ++i) a[G[id][i]] ^= ans[sg[G[id][i]]], ans[sg[G[id][i]]] = 0; for (int i = (1); i <= (n); ++i) printf("%d%c", a[i], i == n ? 10 : 32); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010, M = 200010; int n, m; int e, to[M], nxt[M], hd[N], deg[N]; int tag[N], sg[N], val[N], sum[N]; vector<int> vec[N]; void add(int x, int y) { to[++e] = y; nxt[e] = hd[x]; hd[x] = e; } void topo() { queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = hd[u]; i; i = nxt[i]) { int v = to[i]; deg[v]--; if (deg[v] == 0) q.push(v); } for (int i = 0; i < vec[u].size(); i++) { int v = vec[u][i]; tag[sg[v]] = u; } while (tag[sg[u]] == u) sg[u]++; sum[sg[u]] ^= val[u]; } return; } void solve() { for (int i = n; i >= 0; i--) { if (!sum[i]) continue; int pos = 0; for (int j = 1; j <= n; j++) if (sg[j] == i && (val[j] ^ sum[i]) < val[j]) pos = j; val[pos] ^= sum[i]; for (int j = 0; j < vec[pos].size(); j++) { int v = vec[pos][j]; val[v] ^= sum[sg[v]]; sum[sg[v]] = 0; } puts("WIN"); for (int j = 1; j <= n; j++) printf("%d ", val[j]); puts(""); return; } puts("LOSE"); return; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &val[i]); for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); vec[u].push_back(v); add(v, u); deg[u]++; } topo(); solve(); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m; int h[maxn], in[maxn], q[maxn], sg[maxn], sum[maxn], vis[maxn]; vector<int> G[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &h[i]); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); ++in[v]; } int l = 1, r = 0; for (int i = 1; i <= n; ++i) if (!in[i]) q[++r] = i; while (l <= r) { int u = q[l++]; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (!--in[v]) q[++r] = v; } } for (int i = n; i; --i) { int u = q[i]; for (int j = 0; j < G[u].size(); ++j) { int v = G[u][j]; vis[sg[v]] = 1; } for (; vis[sg[u]]; ++sg[u]) ; for (int j = 0; j < G[u].size(); ++j) { int v = G[u][j]; vis[sg[v]] = 0; } } for (int i = 1; i <= n; ++i) sum[sg[i]] ^= h[i]; int p = -1; for (int i = n; ~i; --i) if (sum[i]) { p = i; break; } if (p == -1) { puts("LOSE"); return 0; } puts("WIN"); for (int i = 1; i <= n; ++i) if (sg[i] == p) { if ((sum[p] ^ h[i]) > h[i]) continue; h[i] ^= sum[p]; for (int j = 0; j < G[i].size(); ++j) { int v = G[i][j]; h[v] ^= sum[sg[v]]; sum[sg[v]] = 0; } break; } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m, eid, cnt, p[N], h[N], d[N], vis[N], sg[N], sum[N], b[N]; struct edge { int V, nxt; } e[N << 1]; inline void addedge(int u, int v) { e[++eid] = (edge){v, p[u]}, p[u] = eid; } inline void tuopu() { queue<int> q; while (!q.empty()) q.pop(); for (int i = (1); i <= (n); i++) if (d[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(), b[++cnt] = u; for (int i = p[u], v = e[i].V; i; i = e[i].nxt, v = e[i].V) if (!--d[v]) q.push(v); } } int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (n); i++) scanf("%d", &h[i]); for (int i = (1); i <= (m); i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v), d[v]++; } tuopu(); for (int i = (cnt); i >= (1); i--) { int u = b[i]; for (int j = p[u], v = e[j].V; j; j = e[j].nxt, v = e[j].V) vis[sg[v]] = u; for (int j = (0); j <= (n); j++) if (vis[j] ^ u) { sum[sg[u] = j] ^= h[u]; break; } } for (int i = (n - 1); i >= (0); i--) if (sum[i]) { puts("WIN"); for (int u = (1); u <= (n); u++) if (sg[u] == i && (h[u] ^ sum[i]) < h[u]) { h[u] ^= sum[i]; for (int j = p[u], v = e[j].V; j; j = e[j].nxt, v = e[j].V) h[v] ^= sum[sg[v]], sum[sg[v]] = 0; for (int j = (1); j <= (n); j++) printf("%d ", h[j]); return 0; } } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int w = 1, s = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return w * s; } int n, m, h[1001000], Sum[1010010], Sg[1010010], tag[1010010], du[1010010], Seq[1010000], cnt; vector<int> vec[1010100]; queue<int> q; inline void Top_Sort() { for (register int i = 1; i <= n; ++i) if (!du[i]) q.push(i), Seq[++cnt] = i; while (!q.empty()) { int u = q.front(); q.pop(); for (auto x : vec[u]) { du[x]--; if (!du[x]) q.push(x), Seq[++cnt] = x; } } } int main() { n = read(), m = read(); for (register int i = 1; i <= n; ++i) h[i] = read(); for (register int i = 1; i <= m; ++i) { int u = read(), v = read(); vec[u].push_back(v); du[v]++; } Top_Sort(); for (register int i = n; i >= 1; --i) { int u = Seq[i]; for (auto x : vec[u]) { tag[Sg[x]] = i; } while (tag[Sg[u]] == i) Sg[u]++; Sum[Sg[u]] ^= h[u]; } for (register int i = n; i >= 0; --i) { if (Sum[i]) { int xx = 0; for (register int j = 1; j <= n; ++j) if ((Sg[j] == i) && (h[j] > (h[j] ^ Sum[i]))) xx = j; h[xx] = h[xx] ^ Sum[i]; for (auto x : vec[xx]) h[x] = Sum[Sg[x]] ^ h[x], Sum[Sg[x]] = 0; cout << "WIN\n"; for (register int i = 1; i <= n; ++i) cout << h[i] << " "; exit(0); } } cout << "LOSE\n"; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <class I> inline void gi(I &x) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = (x << 1) + (x << 3) + (c & 15); x *= f; } template <class I> inline void get(I &x) { for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < 'A' || c > 'Z'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) ; x = c; } inline void read(char *s) { for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < 'A' || c > 'Z'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) ; for (; c >= 'A' && c <= 'Z'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) *++s = c; *++s = '\0'; } template <class I> inline void print(I x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::get; using io ::gi; using io ::print; using io ::putc; using io ::read; const int N = 2e5 + 5; vector<int> e[N], v[N]; int deg[N], q[N], num[N], vis[N]; long long h[N], val[N]; int main() { register int n, m, i, x, y, l, r, cnt; gi(n); gi(m); for (i = 1; i <= n; ++i) gi(h[i]); while (m--) gi(x), gi(y), ++deg[x], e[y].push_back(x), v[x].push_back(y); l = 1; r = 0; for (i = 1; i <= n; ++i) if (!deg[i]) q[++r] = i; cnt = 0; while (l <= r) { x = q[l++]; for (auto y : v[x]) vis[num[y]] = 1; while (vis[num[x]]) ++num[x]; for (auto y : v[x]) vis[num[y]] = 0; cnt = max(cnt, num[x]); for (auto y : e[x]) if (!--deg[y]) q[++r] = y; } for (i = 1; i <= n; ++i) val[num[i]] ^= h[i]; for (i = cnt; i >= 0; --i) if (val[i]) { putc('W'); putc('I'); putc('N'); putc('\n'); for (x = 1; x <= n; ++x) if (num[x] == i && h[x] > (h[x] ^ val[i])) { h[x] ^= val[i]; for (auto y : v[x]) h[y] ^= val[num[y]], val[num[y]] = 0; for (i = 1; i <= n; ++i) print(h[i]), putc(' '); putc('\n'); return 0; } } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200200; using namespace std; int n; int q; int h[N]; int m[N]; int t[N]; vector<int> v[N]; void dfs(int x) { if (t[x]) { return; } set<int> s; for (int y : v[x]) { dfs(y); s.insert(t[y]); } t[x] = 1; while (s.find(t[x]) != s.end()) { t[x] += 1; } } int main() { ios_base::sync_with_stdio(0); cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 1; i <= q; i++) { int x, y; cin >> x >> y; v[x].push_back(y); } for (int i = 1; i <= n; i++) { dfs(i); m[t[i]] ^= h[i]; } int last = -1; for (int i = 1; i <= n; i++) { if (m[i]) { last = i; } } if (last == -1) { cout << "LOSE" << "\n"; return 0; } cout << "WIN" << "\n"; for (int i = 1; i <= n; i++) { if (t[i] == last && (m[t[i]] ^ h[i]) < h[i]) { h[i] = m[t[i]] ^ h[i]; for (int j : v[i]) { m[t[j]] ^= h[j]; h[j] = m[t[j]]; m[t[j]] = 0; } break; } } for (int i = 1; i <= n; i++) { cout << h[i] << " \n"[i == n]; } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read(); int n, m; vector<int> e[200005]; int deg[200005]; void add(int f, int t) { e[f].push_back(t), ++deg[t]; } queue<int> q; int h[200005], sg[200005], mx, sum[200005]; vector<int> nx[200005]; void work() { for (int i = 1; i <= n; ++i) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(), sort(nx[u].begin(), nx[u].end()); for (int x : nx[u]) if (x > sg[u]) break; else if (x == sg[u]) ++sg[u]; sg[u] > mx ? mx = sg[u] : 0; for (int v : e[u]) nx[v].push_back(sg[u]), --deg[v] ? void() : q.push(v); } for (int i = 1; i <= n; ++i) sum[sg[i]] ^= h[i]; for (int i = 0; i <= mx; ++i) if (sum[i]) return puts("WIN"), void(); puts("LOSE"), void(); } int vis[200005]; void solve(int d) { int u; for (u = 1; u <= n; ++u) if (sg[u] == d && h[u] > (sum[d] ^ h[u])) break; h[u] = sum[d] ^ h[u]; for (int v = 1; v <= n; ++v) { if (sum[sg[v]]) { int flg = 0; for (int t : e[v]) if (t == u) flg = 1, h[v] = sum[sg[v]] ^ h[v]; if (!flg) continue; sum[sg[v]] = 0; } } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); } int main() { n = read(), m = read(); for (int i = 1; i <= n; ++i) h[i] = read(); for (int i = 1; i <= m; ++i) add(read(), read()); work(); for (int i = mx; i >= 0; --i) if (sum[i]) return solve(i), 0; return 0; } int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') f = (c == '-') ? -1 : f, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, h[N], q[N], d[N], s[N], vis[N], sg[N]; vector<int> g[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); d[v]++; } int ql = 1, qr = 0; for (int i = 1; i <= n; i++) if (!d[i]) q[++qr] = i; while (ql <= qr) { int x = q[ql++]; for (int i = 0; i < g[x].size(); i++) if (!--d[g[x][i]]) q[++qr] = g[x][i]; } for (int i = n; i; i--) { int x = q[i]; for (int j = 0; j < g[x].size(); j++) vis[sg[g[x][j]]] = i; while (vis[sg[x]] == i) sg[x]++; s[sg[x]] ^= h[x]; } for (int i = n; ~i; i--) if (s[i]) { int x; for (int j = 1; j <= n; j++) if (sg[j] == i && h[j] > (s[i] ^ h[j])) x = j; h[x] ^= s[sg[x]]; for (int j = 0; j < g[x].size(); j++) h[g[x][j]] ^= s[sg[g[x][j]]], s[sg[g[x][j]]] = 0; puts("WIN"); for (int j = 1; j <= n; j++) printf("%d ", h[j]); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> E[N]; int n, m, h[N], deg[N], vis[N], sg[N], sum[N], sq[N], tt = 0; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 1, u, v; i <= m; i++) cin >> u >> v, ++deg[v], E[u].push_back(v); queue<int> q; for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); sq[++tt] = u; for (auto v : E[u]) if (!--deg[v]) q.push(v); } for (int i = tt, u; i; i--) { u = sq[i]; for (auto v : E[u]) vis[sg[v]] = u; for (int j = 0;; j++) if (vis[j] ^ u) { sum[sg[u] = j] ^= h[u]; break; } } for (int i = n - 1; ~i; i--) if (sum[i]) { puts("WIN"); for (int u = 1; u <= n; u++) if (sg[u] == i && (h[u] ^ sum[i]) < h[u]) { h[u] ^= sum[i]; for (auto v : E[u]) h[v] ^= sum[sg[v]], sum[sg[v]] = 0; for (int v = 1; v <= n; v++) cout << h[v] << ' '; return 0; } } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int v[500500]; vector<int> L[500500]; int xl[500500]; int lvl[500500]; void go(int x) { if (lvl[x]) return; map<int, int> mp; for (int i = 0; i < L[x].size(); i++) { go(L[x][i]); mp[lvl[L[x][i]]] = 1; } lvl[x] = 1; while (mp[lvl[x]]) lvl[x]++; xl[lvl[x]] ^= v[x]; } int has(int a, int b) { return (a | b) == a; } int bit(int a) { while (a != (a & -a)) a -= a & -a; return a; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", v + i); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b), a--, b--; L[a].push_back(b); } for (int i = 0; i < n; i++) go(i); for (int i = 0; i < n; i++) ("%d ", lvl[i]); ("\n"); int u = n; while (u > 0 && xl[u] == 0) u--; if (u == 0) { printf("LOSE\n"); return 0; } printf("WIN\n"); int vx = 0; while (lvl[vx] != u || !has(v[vx], bit(xl[u]))) vx++; ("u %d vx %d\n", u, vx); v[vx] ^= xl[u]; for (int i = 0; i < L[vx].size(); i++) { int to = L[vx][i]; v[to] ^= xl[lvl[to]]; xl[lvl[to]] = 0; } for (int i = 0; i < n; i++) printf("%d ", v[i]); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 1; int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x; } vector<int> to[maxn]; int h[maxn], SG[maxn], sum[maxn]; int c[maxn], deg[maxn], que[maxn]; int n, m, hd = 1, tl = 1; int main() { n = read(), m = read(); for (int i = 1; i <= n; ++i) h[i] = read(); for (int i = 1; i <= m; ++i) { int u = read(), v = read(); to[u].push_back(v); ++deg[v]; } for (int i = 1; i <= n; ++i) if (!deg[i]) que[tl++] = i; while (hd ^ tl) { int u = que[hd++]; for (auto v : to[u]) if (!--deg[v]) que[tl++] = v; } for (int i = n; i; --i) { int u = que[i]; for (auto v : to[u]) ++c[SG[v]]; while (c[SG[u]]) ++SG[u]; sum[SG[u]] ^= h[u]; for (auto v : to[u]) --c[SG[v]]; } for (int i = n, u; ~i; --i) if (sum[i]) { for (int j = 1; j <= n; ++j) if (SG[j] == i && h[j] > (h[j] ^ sum[i])) u = j; h[u] ^= sum[i]; for (auto v : to[u]) h[v] ^= sum[SG[v]], sum[SG[v]] = 0; puts("WIN"); for (int j = 1; j <= n; ++j) printf("%d ", h[j]); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200500; vector<int> side[maxn]; int n, m, val[maxn]; long long h[maxn], x[maxn], mx; void dfs(int u) { if (val[u] != -1) return; bool vis[705] = {0}; for (int i = 0; i < side[u].size(); i++) { int v = side[u][i]; dfs(v); vis[val[v]] = 1; } for (int i = 0; i < 705; i++) { if (!vis[i]) { val[u] = i; break; } } x[val[u]] ^= h[u]; mx = max(val[u] * 1ll, mx); } int main() { memset(val, -1, sizeof(val)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", &h[i]); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), side[u].push_back(v); for (int i = 1; i <= n; i++) dfs(i); for (int i = mx; i >= 0; i--) { if (x[i]) { printf("WIN\n"); for (int j = 1; j <= n; j++) { if (val[j] == i && (h[j] ^ x[i]) < h[j]) { h[j] = h[j] ^ x[i]; x[i] = 0; for (int k = 0; k < side[j].size(); k++) { int v = side[j][k]; if (!x[val[v]]) continue; h[v] = h[v] ^ x[val[v]]; x[val[v]] = 0; } break; } } for (int j = 1; j <= n; j++) printf("%lld ", h[j]); return 0; } } printf("LOSE\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { register long long x = 0, f = 1; register char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = 0; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch ^ '0'); ch = getchar(); } return f ? x : -x; } const int N = 2e5 + 5; int n, m, id[N], vis[N]; long long h[N], sum[N]; vector<int> E[N]; void dfs(int u) { for (int v : E[u]) if (!id[v]) dfs(v); for (int v : E[u]) vis[id[v]] = 1; for (id[u] = 1; vis[id[u]]; ++id[u]) ; sum[id[u]] ^= h[u]; for (int v : E[u]) vis[id[v]] = 0; } int main() { n = read(), m = read(); for (int i = (1), _ed = (n); i <= _ed; ++i) h[i] = read(); for (int i = (1), _ed = (m); i <= _ed; ++i) { int u = read(), v = read(); E[u].push_back(v); } for (int i = (1), _ed = (n); i <= _ed; ++i) if (!id[i]) dfs(i); int p = 0; for (int i = (n), _ed = (1); i >= _ed; --i) if (sum[i]) { p = i; break; } if (!p) return puts("LOSE"), 0; else puts("WIN"); int rt = 0; for (int i = (1), _ed = (n); i <= _ed; ++i) { if (id[i] ^ p) continue; if (h[i] > (h[i] ^ sum[p])) { rt = i; break; } } h[rt] = h[rt] ^ sum[p], sum[p] = 0; for (int v : E[rt]) { if (!sum[id[v]]) continue; h[v] = h[v] ^ sum[id[v]], sum[id[v]] = 0; } for (int i = (1), _ed = (n); i <= _ed; ++i) printf("%lld%c", h[i], " \n"[i == n]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int n, m, i, j, t, k, s, head[maxn], N, in[maxn], b[maxn], sg[maxn]; long long a[maxn], val[maxn]; bool vis[maxn]; struct Edge { int nxt, aim; } edge[maxn]; inline void add_edge(int x, int y) { edge[++N] = (Edge){head[x], y}; head[x] = N; } queue<int> Q; int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%lld", &a[i]); for (i = 1; i <= m; ++i) { scanf("%d%d", &t, &k); add_edge(t, k); ++in[k]; } for (i = 1; i <= n; ++i) if (!in[i]) Q.push(i); k = 0; while (!Q.empty()) { t = Q.front(); Q.pop(); b[++k] = t; for (i = head[t]; i; i = edge[i].nxt) { int des = edge[i].aim; --in[des]; if (!in[des]) Q.push(des); } } for (i = n; i > 0; --i) { for (j = head[b[i]]; j; j = edge[j].nxt) vis[sg[edge[j].aim]] = 1; t = 0; while (vis[t]) ++t; sg[b[i]] = t; val[t] ^= a[b[i]]; for (j = head[b[i]]; j; j = edge[j].nxt) vis[sg[edge[j].aim]] = 0; } t = -1; for (i = 0; i <= n; ++i) if (val[i] > 0) t = i; if (t == -1) { printf("LOSE\n"); return 0; } printf("WIN\n"); for (i = 1; i <= n; ++i) if (sg[i] == t && (a[i] ^ val[sg[i]]) < a[i]) { a[i] ^= val[sg[i]]; val[sg[i]] = 0; for (j = head[i]; j; j = edge[j].nxt) { a[edge[j].aim] ^= val[sg[edge[j].aim]]; val[sg[edge[j].aim]] = 0; } break; } for (i = 1; i <= n; ++i) printf("%lld%c", a[i], (i == n ? '\n' : ' ')); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> a[200005]; struct edge { int to, nxt; } e[200005]; int hed[200005], cnt; inline void add(int u, int v) { e[++cnt] = (edge){v, hed[u]}; hed[u] = cnt; } int num[200005], ru[200005]; int sg[200005], tag[200005], dep[200005]; inline void topo() { queue<int> q; for (int i = 1; i <= n; ++i) if (!ru[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < a[u].size(); ++i) tag[dep[a[u][i]]] = u; while (tag[dep[u]] == u) dep[u]++; sg[dep[u]] ^= num[u]; for (int i = hed[u]; i; i = e[i].nxt) { int v = e[i].to; if (--ru[v] == 0) q.push(v); } } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", num + i); int u, v; for (int i = 1; i <= m; ++i) { scanf("%d%d", &u, &v); a[u].push_back(v), add(v, u), ru[u]++; } topo(); for (int i = n; i >= 0; --i) { if (sg[i] == 0) continue; for (int j = 1; j <= n; ++j) { if (dep[j] == i && num[j] > (num[j] ^ sg[i])) { u = j; break; } } num[u] ^= sg[i]; for (int j = 0; j < a[u].size(); ++j) { int v = a[u][j]; num[v] ^= sg[dep[v]], sg[dep[v]] = 0; } printf("WIN\n"); for (int j = 1; j <= n; ++j) printf("%d ", num[j]); return 0; } printf("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { int f = 0; x = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) f |= (ch == '-'); for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } const int N = 200005; int h[N], q[N], dp[N], in[N], f[N], vis[N]; int n, m, l, r, u, v, p; vector<int> e[N]; int main() { read(n), read(m), p = n; for (int i = (1); i <= (n); i++) read(h[i]); for (int i = (1); i <= (m); i++) read(u), read(v), e[u].push_back(v), in[v]++; for (int i = (1); i <= (n); i++) if (!in[i]) q[r++] = i; for (int u = q[l++]; l <= r; u = q[l++]) for (auto v : e[u]) if (!(--in[v])) q[r++] = v; for (int i = (n - 1); i >= (0); i--) { int u = q[i]; for (auto v : e[u]) vis[dp[v]] = 1; while (vis[dp[u]]) dp[u]++; for (auto v : e[u]) vis[dp[v]] = 0; f[dp[u]] ^= h[u]; } while (p >= 0 && !f[p]) p--; if (p == -1) return puts("LOSE"), 0; for (int u = (1); u <= (n); u++) if (dp[u] == p && (h[u] ^ f[p]) < h[u]) { h[u] ^= f[p], f[p] = 0; for (auto v : e[u]) h[v] ^= f[dp[v]], f[dp[v]] = 0; } puts("WIN"); for (int i = (1); i <= (n); i++) printf("%d ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Q = 1 << 18; struct gra { int las[Q], e[Q], nn[Q], inc; void ins(int x, int y) { e[++inc] = y; nn[inc] = las[x]; las[x] = inc; } } g, f; int mex[Q]; int vis[Q]; int h[Q], sm[Q]; int q[Q]; int in[Q]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1, x, y; i <= m; i++) scanf("%d%d", &x, &y), g.ins(x, y), f.ins(y, x), ++in[x]; int hd = 1, tl = 0; for (int i = 1; i <= n; i++) if (!in[i]) q[++tl] = i; for (int t = 1; t <= n; t++) { int x = q[hd++]; for (int t = g.las[x]; t; t = g.nn[t]) { int y = g.e[t]; vis[mex[y]] = x; } for (mex[x] = 0; vis[mex[x]] == x; ++mex[x]) ; for (int t = f.las[x]; t; t = f.nn[t]) { int y = f.e[t]; --in[y]; if (!in[y]) q[++tl] = y; } } int flag = 0; for (int i = 1; i <= n; i++) sm[mex[i]] ^= h[i]; for (int p = n; p >= 0; --p) if (sm[p]) { puts("WIN"); for (int i = 1; i <= n; i++) if (mex[i] == p && (h[i] ^ sm[p]) < h[i]) { h[i] ^= sm[p]; for (int t = g.las[i]; t; t = g.nn[t]) { int y = g.e[t]; if (vis[mex[y]] == n + 1) continue; vis[mex[y]] = n + 1; h[y] ^= sm[mex[y]]; } for (int j = 1; j <= n; j++) printf("%d ", h[j]); return 0; } } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &item) { out << '(' << item.first << ", " << item.second << ')'; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { for (const auto &item : v) out << item << ' '; return out; } const int NMAX = 200010; int h[NMAX]; int mex[NMAX], xorMex[NMAX]; vector<int> adj[NMAX]; void dfs(int v) { set<int> s; for (auto u : adj[v]) { if (mex[u] == -1) dfs(u); s.insert(mex[u]); } int i; for (i = 0; s.count(i); ++i) ; mex[v] = i; xorMex[i] ^= h[v]; } int main() { ios_base::sync_with_stdio(false); int i, n, m, a, b; cin >> n >> m; for (i = 1; i <= n; ++i) cin >> h[i]; for (i = 1; i <= m; ++i) { cin >> a >> b; adj[a].push_back(b); } memset(mex, -1, sizeof mex); for (i = 1; i <= n; ++i) if (mex[i] == -1) dfs(i); for (i = n; i >= 0; --i) { if (!xorMex[i]) continue; cout << "WIN\n"; for (int v = 1; v <= n; ++v) if (mex[v] == i && h[v] >= (h[v] ^ xorMex[i])) { h[v] ^= xorMex[i]; for (auto u : adj[v]) { h[u] ^= xorMex[mex[u]]; xorMex[mex[u]] = 0; } break; } for (int v = 1; v <= n; ++v) cout << h[v] << ' '; cout << '\n'; return 0; } cout << "LOSE\n"; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int level[200005]; int h[200005]; int val[200005]; bool vis[200005]; bool num[200005]; vector<int> E[200005]; void dfs(int x) { if (vis[x]) return; vis[x] = true; if (E[x].size() == 0) level[x] = 0; int sz = E[x].size(); for (int v : E[x]) { if (!vis[v]) { dfs(v); } } for (int i = 0; i <= sz + 1; i++) num[i] = true; for (int v : E[x]) num[level[v]] = false; for (int i = 0; i <= sz + 1; i++) { if (num[i]) { level[x] = i; return; } } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); E[u].push_back(v); } for (int i = 1; i <= n; i++) level[i] = -1; for (int i = 1; i <= n; i++) vis[i] = false; for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i); } int mx = 0; for (int i = 0; i <= mx; i++) val[i] = 0; int choose = 0; for (int i = 1; i <= n; i++) { val[level[i]] ^= h[i]; } for (int i = 1; i <= n; i++) { if (val[level[i]] != 0) mx = max(mx, level[i]); } bool flag = true; for (int i = 0; i <= mx; i++) if (val[i] != 0) flag = false; if (flag) { puts("LOSE"); return 0; } for (int i = 1; i <= n; i++) { if (level[i] == mx) { if ((val[mx] ^ h[i]) < h[i]) { choose = i; break; } } } h[choose] = val[mx] ^ h[choose]; for (int v : E[choose]) { int x = level[v]; if (val[x] != 0) { h[v] = val[x] ^ h[v]; val[x] = 0; } } puts("WIN"); for (int i = 1; i <= n; i++) { printf("%d ", h[i]); } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, num[200100], sy[200100], ds[200100], bj[200100], sum[200100]; vector<long long> to[200100], pre[200100]; queue<long long> que; int main() { long long i, j, t, p, q; cin >> n >> m; for (i = 1; i <= n; i++) scanf("%lld", &num[i]); for (i = 1; i <= m; i++) { scanf("%lld%lld", &p, &q); pre[q].push_back(p); to[p].push_back(q); ds[p]++; } for (i = 1; i <= n; i++) if (!ds[i]) que.push(i); for (; !que.empty();) { q = que.front(); que.pop(); for (i = 0; i < pre[q].size(); i++) { t = pre[q][i]; ds[t]--; if (!ds[t]) que.push(t); } for (i = 0; i < to[q].size(); i++) { t = to[q][i]; bj[sy[t]] = q; } for (i = 0; bj[i] == q; i++) ; sy[q] = i; sum[sy[q]] ^= num[q]; } for (i = n; i >= 0; i--) { if (!sum[i]) continue; puts("WIN"); for (j = 1; j <= n; j++) if (sy[j] == i && (num[j] ^ sum[i]) < num[j]) break; q = j, num[q] ^= sum[i]; for (j = 0; j < to[q].size(); j++) { t = to[q][j]; if (sum[sy[t]] && bj[sy[t]] != -1) { bj[sy[t]] = -1; num[t] ^= sum[sy[t]]; } } for (i = 1; i <= n; i++) printf("%lld ", num[i]); return 0; } puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = v * 10 + c - '0'; while (isdigit(c = getchar())) v = v * 10 + c - '0'; x = v * f; } inline void read(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = v * 10 + c - '0'; while (isdigit(c = getchar())) v = v * 10 + c - '0'; x = v * f; } inline void readc(char &x) { char c; while ((c = getchar()) == ' ') ; x = c; } inline void writes(string s) { puts(s.c_str()); } inline void writeln() { writes(""); } inline void writei(int x) { if (x < 0) { putchar('-'); x = abs(x); } if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } inline void writell(long long x) { if (x < 0) { putchar('-'); x = abs(x); } if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } int n, m, i, j, a[200005], f[200005], s[200005], vis[200005], ti, mx; vector<int> e[200005]; int dfs(int x); void mex(int x) { for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) dfs(*it); ti++; for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) vis[dfs(*it)] = ti; for (f[x] = 0; vis[f[x]] == ti; f[x]++) ; } int dfs(int x) { if (f[x] != -1) return f[x]; mex(x); return f[x]; } void solve(int x) { a[x] ^= s[mx]; s[mx] = 0; for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { a[*it] ^= s[f[*it]]; s[f[*it]] = 0; } for (((i)) = (1); ((i)) <= ((n)); ((i))++) printf("%d ", a[i]); } int main() { read(n); read(m); for (((i)) = (1); ((i)) <= ((n)); ((i))++) read(a[i]); for (((i)) = (1); ((i)) <= ((m)); ((i))++) { int x, y; read(x); read(y); e[x].push_back(y); } memset(f, -1, sizeof(f)); for (((i)) = (1); ((i)) <= ((n)); ((i))++) { mx = max(mx, dfs(i)); s[dfs(i)] ^= a[i]; } while (mx >= 0) { for (((i)) = (1); ((i)) <= ((n)); ((i))++) if (f[i] == mx && (s[mx] ^ a[i]) < a[i]) { puts("WIN"); solve(i); return 0; } mx--; } { cout << "LOSE"; return 0; } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, a[200009], h[200009], q[200009], d[200009], s[200009], sg[200009], bo[200009]; vector<int> e[200009]; bool vis[200009]; int read() { int x = 0; char ch = getchar(); bool flag = 0; while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return flag ? -x : x; } long long readll() { long long x = 0; char ch = getchar(); bool flag = 0; while (ch < '0' || ch > '9') { if (ch == '-') flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return flag ? -x : x; } int dfs(int x) { if (vis[x]) return sg[x]; vis[x] = 1; for (int y : e[x]) sg[y] = dfs(y); for (int y : e[x]) bo[sg[y]] = x; for (sg[x] = 0; bo[sg[x]] == x; sg[x]++) ; s[sg[x]] ^= a[x]; return sg[x]; } int main() { scanf("%d%d", &n, &m); int i, x, y; for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); e[x].push_back(y); d[y]++; } for (i = 1; i <= n; i++) sg[i] = dfs(i); for (i = n; i >= 0; i--) if (s[i]) break; if (i == -1) { puts("LOSE"); return 0; } x = i; memset(vis, 0, sizeof(vis)); for (i = 1; i <= n; i++) if (sg[i] == x && (a[i] ^ s[x]) < a[i]) { a[i] ^= s[x]; for (int p : e[i]) { if (vis[sg[p]]) continue; vis[sg[p]] = 1; a[p] ^= s[sg[p]]; } break; } puts("WIN"); for (i = 1; i <= n; i++) printf("%d%c", a[i], i < n ? ' ' : '\n'); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vint = vector<long long>; using pint = pair<long long, long long>; using vpint = vector<pint>; template <typename A, typename B> inline void chmin(A& a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A& a, B b) { if (a < b) a = b; } template <class A, class B> ostream& operator<<(ostream& ost, const pair<A, B>& p) { ost << "{" << p.first << "," << p.second << "}"; return ost; } template <class T> ostream& operator<<(ostream& ost, const vector<T>& v) { ost << "{"; for (long long i = 0; i < v.size(); i++) { if (i) ost << ","; ost << v[i]; } ost << "}"; return ost; } long long N, M; vint G[222222]; long long topbit(long long a) { return 63 - __builtin_clzll(a); } signed main() { scanf("%lld%lld", &N, &M); vint A(N); for (long long i = 0; i < (N); i++) scanf("%lld", &A[i]); for (long long i = 0; i < (M); i++) { long long a, b; scanf("%lld%lld", &a, &b); a--; b--; G[a].push_back(b); } vint ord; vint deg(N); for (long long i = 0; i < (N); i++) for (auto u : G[i]) deg[u]++; for (long long i = 0; i < (N); i++) if (deg[i] == 0) ord.push_back(i); for (long long i = 0; i < (N); i++) { long long v = ord[i]; for (auto u : G[v]) { if (--deg[u] == 0) ord.push_back(u); } } reverse((ord).begin(), (ord).end()); vint q(N); for (auto v : ord) { set<long long> s; for (auto u : G[v]) s.insert(q[u]); q[v] = 0; while (s.find(q[v]) != s.end()) q[v]++; } vint x(N); for (long long i = 0; i < (N); i++) x[q[i]] ^= A[i]; long long w = -1; for (long long i = 0; i < (N); i++) if (x[i]) w = i; if (w == -1) { puts("LOSE"); return 0; } for (long long i = 0; i < (N); i++) { if (q[i] != w) continue; if (!(A[i] >> topbit(x[w]) & 1)) continue; A[i] ^= x[w]; for (auto u : G[i]) { A[u] ^= x[q[u]]; x[q[u]] = 0; } break; } puts("WIN"); for (long long i = 0; i < (N); i++) { if (i) printf(" "); printf("%lld", A[i]); } puts(""); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m; int h[200200]; struct data { int to, nxt; } mp[200200]; int head[200200], cnt; void link(int x, int y) { mp[++cnt].to = y; mp[cnt].nxt = head[x]; head[x] = cnt; } int d[200200]; queue<int> q; vector<int> nx[200200]; int id[200200]; int val[200200]; void Topsort() { for (int i = 1; i <= n; ++i) if (!d[i]) q.push(i); int u, v, la, siz; while (!q.empty()) { u = q.front(); q.pop(); sort(nx[u].begin(), nx[u].end()); id[u] = la = -1; siz = nx[u].size(); for (int i = 0; i < siz; ++i) { if (nx[u][i] > la + 1) { id[u] = la + 1; break; } la = nx[u][i]; } if (id[u] == -1) id[u] = la + 1; val[id[u]] ^= h[u]; for (int i = head[u]; i; i = mp[i].nxt) { v = mp[i].to; d[v]--; nx[v].push_back(id[u]); if (!d[v]) q.push(v); } } } vector<int> ed[200200]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &h[i]); int xx, yy; for (int i = 1; i <= m; ++i) { scanf("%d%d", &xx, &yy); link(yy, xx); d[xx]++; ed[xx].push_back(yy); } Topsort(); int fl = 0; for (int i = 1; i <= n; ++i) if (val[id[i]]) { fl = 1; break; } if (!fl) { puts("LOSE"); return 0; } puts("WIN"); int mv = 0, v; for (int i = 1; i <= n; ++i) if (val[id[i]]) mv = max(mv, id[i]); for (int i = 1; i <= n; ++i) if (id[i] == mv) { if ((h[i] ^ val[mv]) > h[i]) continue; h[i] ^= val[mv]; for (int e = 0; e < ed[i].size(); ++e) { v = ed[i][e]; h[v] ^= val[id[v]]; val[id[v]] = 0; } break; } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); puts(""); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read() { int x = 0; bool flg = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') flg = true; for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); return flg ? -x : x; } int n, m, h[200010], g[200010]; vector<int> e[200010]; int d[200010]; int p[200010], hd, tl; int sg[200010]; int get(int o) { static int vis[200010]; for (const int &v : e[o]) vis[sg[v]] = o; int z = 0; while (vis[z] == o) ++z; return z; } void work(int o) { h[o] ^= g[sg[o]]; g[sg[o]] = 0; for (const int &v : e[o]) { if (sg[v] >= sg[o]) continue; int w = g[sg[v]]; h[v] ^= w; g[sg[v]] ^= w; } for (int i(1), _i(n); i <= _i; i++) printf("%d ", h[i]); puts(""); } void solve() { n = read(), m = read(); for (int i(1), _i(n); i <= _i; i++) h[i] = read(); while (m--) { int u = read(), v = read(); e[u].push_back(v), ++d[v]; } for (int i(1), _i(n); i <= _i; i++) if (!d[i]) p[++tl] = i; while (hd < tl) { int u = p[++hd]; for (const int &v : e[u]) if (!--d[v]) p[++tl] = v; } for (int i(n), _i(1); i >= _i; i--) sg[p[i]] = get(p[i]), g[sg[p[i]]] ^= h[p[i]]; int q = -1; for (int i(n), _i(0); i >= _i; i--) if (g[i]) { q = i; break; } if (!~q) return puts("LOSE"), void(); puts("WIN"); for (int i(1), _i(n); i <= _i; i++) if (sg[i] == q && (g[sg[i]] ^ h[i]) < h[i]) { work(i); return; } assert(0); } int main() { for (int T = 1; T--;) solve(); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5, maxm = 2e5 + 5; int head[maxn], nume; struct edge { int v, w, c, next; } e[maxm]; inline void init_edge() { memset(head, -1, sizeof head); nume = 0; } inline void add_edge(int u, int v, int w = 0, int c = 0) { e[nume].v = v; e[nume].w = w; e[nume].c = c; e[nume].next = head[u]; head[u] = nume++; } int c[maxn], h[maxn]; long long sg[maxn], a[maxn]; void dfs(int u) { if (h[u] >= 0) return; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; dfs(v); } for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; c[h[v]]++; } for (int i = 0;; i++) { if (c[i] == 0) { h[u] = i; break; } } for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; c[h[v]]--; } } int n, m; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } init_edge(); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); } for (int i = 1; i <= n; i++) h[i] = -1; for (int i = 1; i <= n; i++) { if (h[i] == -1) dfs(i); } for (int i = 1; i <= n; i++) { sg[h[i]] ^= a[i]; } for (int i = n; i >= 0; i--) { if (sg[i] != 0) { printf("WIN\n"); for (int j = 1; j <= n; j++) { if (h[j] == i && (a[j] ^ sg[i]) < a[j]) { a[j] ^= sg[i]; sg[i] = 0; for (int k = head[j]; ~k; k = e[k].next) { int v = e[k].v; a[v] ^= sg[h[v]]; sg[h[v]] = 0; } break; } } for (int j = 1; j <= n; j++) printf("%lld ", a[j]); break; } if (i == 0) { printf("LOSE"); break; } } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline T read(register T& t) { register T f = 1; register char ch = getchar(); t = 0; while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar(); t *= f; return t; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } const long long p = 998244353; inline long long power(register long long x, register long long k = p - 2) { register long long re = 1; for (; k; k >>= 1, x = x * x % p) if (k & 1) re = re * x % p; return re; } int n, m; vector<int> to[200005]; int deg[200005]; int st[200005], st_top; int id[200005]; long long xum[200005]; long long h[200005]; int main() { read(n, m); for (int i = 1; i <= n; i++) read(h[i]); for (int i = 1, x, y; i <= m; i++) read(x, y), deg[y]++, to[x].push_back(y); queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] == 0) q.push(i); while (q.size()) { int now = q.front(); q.pop(); st[++st_top] = now; for (int i : to[now]) { deg[i]--; if (deg[i] == 0) q.push(i); } } static bool vis[200005]; for (int tp = n; tp >= 1; tp--) { int i = st[tp]; for (int j : to[i]) vis[id[j]] = 1; while (vis[id[i]]) id[i]++; for (int j : to[i]) vis[id[j]] = 0; xum[id[i]] ^= h[i]; } int pos = -1; for (int i = 0; i <= n; i++) if (xum[i]) pos = i; if (pos == -1) return printf("LOSE\n"), 0; for (int i = 1; i <= n; i++) if (id[i] == pos) { int tp = xum[pos] ^ h[i]; if (tp >= h[i]) continue; h[i] = tp; xum[pos] = 0; for (int j : to[i]) if (xum[id[j]]) { h[j] = xum[id[j]] ^ h[j]; xum[id[j]] = 0; } break; } printf("WIN\n"); for (int i = 1; i <= n; i++) printf("%lld ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; vector<int> G[maxn]; int topo[maxn]; int sg[maxn], deg[maxn], mem[maxn]; int Xor[maxn]; long long h[maxn]; int main(void) { int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) scanf("%lld", &h[i]); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); deg[v]++; } queue<int> q; int cnt = 1; for (int i = 1; i <= n; ++i) if (deg[i] == 0) q.push(i); while (!q.empty()) { int p = q.front(); q.pop(); topo[cnt++] = p; for (auto c : G[p]) { deg[c]--; if (deg[c] == 0) q.push(c); } } for (int i = n; i >= 1; --i) { int u = topo[i]; for (auto c : G[u]) mem[sg[c]] = i; while (mem[sg[u]] == i) sg[u]++; Xor[sg[u]] ^= h[u]; } int index = -1; for (int i = 0; i < maxn; ++i) if (Xor[i] != 0) index = i; if (index == -1) return 0 * puts("LOSE"); for (int i = 1; i <= n; ++i) { if (sg[i] == index && ((h[i] ^ Xor[index]) < h[i])) { h[i] ^= Xor[index]; for (auto c : G[i]) { h[c] ^= Xor[sg[c]]; Xor[sg[c]] = 0; } break; } } puts("WIN"); for (int i = 1; i <= n; ++i) printf("%lld ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> std::vector<std::vector<int>> ad; std::vector<int> fm; std::vector<bool> tmp; int f(int x) { if (fm[x] >= 0) return fm[x]; for (int y : ad[x]) f(y); for (int y : ad[x]) tmp[fm[y]] = true; fm[x] = int(std::find(begin(tmp), end(tmp), false) - begin(tmp)); for (int y : ad[x]) { tmp[fm[y]] = false; } return fm[x]; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int n, m; std::cin >> n >> m; std::vector<int> h(n); for (int& x : h) std::cin >> x; ad.resize(n); for (int z = m; z--;) { int u, v; std::cin >> u >> v; --u; --v; ad[u].push_back(v); } std::vector<int> val(n); fm.assign(n, -1); tmp.resize(n); for (int i = n; i--;) val[f(i)] ^= h[i]; while (!val.empty() && val.back() == 0) val.pop_back(); if (val.empty()) { std::cout << "LOSE\n"; return 0; } std::cout << "WIN\n"; int i = 0; while (fm[i] != val.size() - 1 || (h[i] ^ val.back()) > h[i]) { ++i; } h[i] ^= val.back(); std::vector<bool> ok(val.size() - 1); for (int j : ad[i]) if ((unsigned)fm[j] < ok.size() && !ok[fm[j]]) { ok[fm[j]] = true; h[j] ^= val[fm[j]]; } for (auto x : h) std::cout << x << ' '; std::cout << '\n'; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200500; vector<int> side[maxn]; int n, m, val[maxn]; long long h[maxn], x[maxn], mx; void dfs(int u) { if (val[u] != -1) return; bool vis[705] = {0}; for (int i = 0; i < side[u].size(); i++) { int v = side[u][i]; dfs(v); vis[val[v]] = 1; } for (int i = 0; i < 705; i++) { if (!vis[i]) { val[u] = i; break; } } x[val[u]] ^= h[u]; mx = max(val[u] * 1ll, mx); } int main() { memset(val, -1, sizeof(val)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", &h[i]); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), side[u].push_back(v); for (int i = 1; i <= n; i++) dfs(i); for (int i = mx; i >= 0; i--) { if (x[i]) { printf("WIN\n"); for (int j = 1; j <= n; j++) { if (val[j] == i && (h[j] ^ x[i]) < h[j]) { h[j] = h[j] ^ x[i]; x[i] = 0; for (int k = 0; k < side[j].size(); k++) { int v = side[j][k]; if (!x[val[v]]) continue; h[v] = h[v] ^ x[val[v]]; x[val[v]] = 0; } break; } } for (int j = 1; j <= n; j++) printf("%lld ", h[j]); return 0; } } printf("LOSE\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } template <typename T> struct hash_map_t { vector<T> v, val, nxt; vector<int> head; int mod, tot, lastv; T lastans; bool have_ans; hash_map_t(int md = 0) { head.clear(); v.clear(); val.clear(); nxt.clear(); tot = 0; mod = md; nxt.resize(1); v.resize(1); val.resize(1); head.resize(mod); have_ans = 0; } void clear() { *this = hash_map_t(mod); } bool count(int x) { int u = x % mod; for (register int i = head[u]; i; i = nxt[i]) { if (v[i] == x) { have_ans = 1; lastv = x; lastans = val[i]; return 1; } } return 0; } void ins(int x, int y) { int u = x % mod; nxt.push_back(head[u]); head[u] = ++tot; v.push_back(x); val.push_back(y); } int qry(int x) { if (have_ans && lastv == x) return lastans; count(x); return lastans; } }; const int N = 2e5 + 5; struct edge_t { int u, v, next; } G[N]; int head[N], h[N], sg[N], sum[N], vis[N], deg[N], q[N]; int n, m, tot, dfn, maxn, hd = 1, tl = 0; inline void addedge(int u, int v) { G[++tot] = (edge_t){u, v, head[u]}, head[u] = tot; } int main() { read(n); read(m); for (register int i = 1; i <= n; i++) read(h[i]); for (register int i = 1; i <= m; i++) { int u, v; read(u); read(v); addedge(u, v); ++deg[v]; } for (register int i = 1; i <= n; i++) if (!deg[i]) q[++tl] = i; while (hd <= tl) { int u = q[hd++]; for (register int i = head[u]; i; i = G[i].next) { int v = G[i].v; --deg[v]; if (!deg[v]) q[++tl] = v; } } for (register int j = n; j >= 1; j--) { int u = q[j]; ++dfn; for (register int i = head[u]; i; i = G[i].next) vis[sg[G[i].v]] = dfn; for (register int i = 0; i <= n; i++) { if (vis[i] != dfn) { sg[u] = i; sum[i] ^= h[u]; break; } } maxn = max(maxn, sg[u]); } for (register int i = maxn; i >= 0; i--) { if (sum[i]) { printf("WIN\n"); for (register int j = 1; j <= n; j++) { if (sg[j] == i && (h[j] ^ sum[i]) < h[j]) { h[j] ^= sum[i]; for (register int k = head[j]; k; k = G[k].next) { int v = G[k].v; if (sum[sg[v]]) { h[v] ^= sum[sg[v]]; sum[sg[v]] = 0; } } for (register int k = 1; k <= n; k++) print(h[k], k == n ? '\n' : ' '); break; } } return 0; } } printf("LOSE\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 1000000007; const int MM = 998244353; const long double PI = acos(-1); template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << '(' << p.first << "," << p.second << ')'; } const int N = 200005; vector<int> v[N]; long long h[N]; int mex[N]; bitset<N> vis; vector<int> mexv[N]; void dfs(int s) { vis[s] = 1; set<int> st; for (auto &j : v[s]) { if (!vis[j]) dfs(j); st.insert(mex[j]); } for (int i = 0;; ++i) { if (!st.count(i)) { mex[s] = i; break; } } mexv[mex[s]].push_back(s); } int _runtimeTerror_() { int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> h[i]; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; v[x].push_back(y); } for (int i = 1; i <= n; ++i) { if (!vis[i]) dfs(i); } int win = -1; for (int i = 0;; ++i) { if (mexv[i].empty()) break; long long Xor = 0; for (auto &j : mexv[i]) Xor ^= h[j]; if (Xor != 0) amax(win, i); } if (win == -1) { cout << "LOSE\n"; return 0; } cout << "WIN\n"; vector<long long> ans(n + 1); for (int i = 1; i <= n; ++i) ans[i] = h[i]; long long Xor = 0; for (auto &j : mexv[win]) Xor ^= h[j]; for (auto &j : mexv[win]) { if ((Xor ^ h[j]) < h[j]) { ans[j] = Xor ^ h[j]; vector<int> x(win, -1); for (int i : v[j]) { if (mex[i] > win) continue; if (x[mex[i]] == -1) x[mex[i]] = i; } for (int i = win - 1; i >= 0; --i) { long long y = 0; for (auto &k : mexv[i]) y ^= h[k]; ans[x[i]] = y ^ h[x[i]]; } break; } } for (int i = 1; i <= n; ++i) cout << ans[i] << " "; cout << "\n"; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) _runtimeTerror_(); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200054, M = N; int V, E; int w[N], W[N], deg[N]; int to[M], first[N], next[M]; int topo[N], mark[N], sg[N]; inline void up(int &x, const int y) { x < y ? x = y : 0; } inline void addedge(int u, int v, int id) { to[id] = v, next[id] = first[u], first[u] = id, ++deg[v]; } int main() { int i, j, u, v, x, y, h, t = 0; scanf("%d%d", &V, &E); for (i = 1; i <= V; ++i) scanf("%d", w + i); for (i = 1; i <= E; ++i) scanf("%d%d", &u, &v), addedge(u, v, i); for (i = 1; i <= V; ++i) if (!deg[i]) topo[t++] = i; for (h = 0; h < t; ++h) for (i = first[x = topo[h]]; i; i = next[i]) if (!--deg[y = to[i]]) topo[t++] = y; assert(t == V), v = 0; for (j = V - 1; j >= 0; --j) { for (i = first[x = topo[j]]; i; i = next[i]) mark[sg[y = to[i]]] = x; for (i = 0; mark[i] == x; ++i) ; up(v, sg[x] = i), W[i] ^= w[x]; } for (i = v; i >= 0 && !W[i]; --i) ; if (!~i) return puts("LOSE"), 0; for (x = 1; x <= V; ++x) if (sg[x] == i && (w[x] ^ W[i]) < w[x]) break; assert(x <= V), w[x] ^= W[i], puts("WIN"); for (i = first[x]; i; i = next[i]) y = to[i], j = sg[y], w[y] ^= W[j], W[j] ^= W[j]; for (i = 1; i <= V; ++i) printf("%d%c", w[i], i == V ? 10 : 32); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> inline long long gi() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) f ^= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? x : -x; } int h[200010], SG[200010], sum[200010]; int fir[200010], nxt[200010], dis[200010], id; inline void link(int a, int b) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b; } int que[200010], hd = 1, tl = 1, in[200010]; int c[200010]; int main() { int n = gi(), m = gi(), a, b; for (int i = 1; i <= n; ++i) h[i] = gi(); while (m--) a = gi(), b = gi(), link(a, b), ++in[b]; for (int i = 1; i <= n; ++i) if (!in[i]) que[tl++] = i; while (hd ^ tl) { int x = que[hd++]; for (int i = fir[x]; i; i = nxt[i]) if (!--in[dis[i]]) que[tl++] = dis[i]; } for (int o = n, x; o; --o) { x = que[o]; for (int i = fir[x]; i; i = nxt[i]) ++c[SG[dis[i]]]; while (c[SG[x]]) ++SG[x]; sum[SG[x]] ^= h[x]; for (int i = fir[x]; i; i = nxt[i]) --c[SG[dis[i]]]; } for (int i = n, x; ~i; --i) if (sum[i]) { for (int j = 1; j <= n; ++j) if (SG[j] == i && h[j] > (h[j] ^ sum[i])) x = j; h[x] ^= sum[i]; for (int j = fir[x]; j; j = nxt[j]) h[dis[j]] ^= sum[SG[dis[j]]], sum[SG[dis[j]]] = 0; puts("WIN"); for (int j = 1; j <= n; ++j) printf("%d ", h[j]); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, top, mx; int head[200010], nxt[200010], to[200010], tot; void add(int u, int v) { tot++; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; } long long a[200010], val[200010]; int deg[200010], p[200010], vis[200010], id[200010]; queue<int> q; void top_sort() { for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (q.size()) { int now = q.front(); q.pop(); p[++top] = now; for (int i = head[now]; i; i = nxt[i]) { deg[to[i]]--; if (!deg[to[i]]) q.push(to[i]); } } for (int i = n; i >= 1; i--) { int x = p[i]; for (int j = head[x]; j; j = nxt[j]) vis[id[to[j]]] = 1; while (vis[id[x]]) id[x]++; val[id[x]] ^= a[x]; mx = max(mx, id[x]); for (int j = head[x]; j; j = nxt[j]) vis[id[to[j]]] = 0; } } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= m; i++) { int q = read(), w = read(); add(q, w); deg[w]++; } top_sort(); int pos = -1; for (int i = 0; i <= mx; i++) if (val[i]) pos = i; if (pos == -1) return puts("LOSE"), 0; puts("WIN"); for (int i = 1; i <= n; i++) { if (id[i] == pos) { if ((val[id[i]] ^ a[i]) > a[i]) continue; a[i] ^= val[id[i]]; val[id[i]] = 0; for (int j = head[i]; j; j = nxt[j]) a[to[j]] ^= val[id[to[j]]], val[id[to[j]]] = 0; } } for (int i = 1; i <= n; i++) printf("%lld ", a[i]); puts(""); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename _tp> inline void read(_tp& x) { char ch = getchar(), ob = 0; x = 0; while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') ob = 1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); if (ob) x = -x; } const int N = 201000; struct Edge { int v, nxt; } a[N + N]; int head[N], Head[N]; int h[N], xr[N], deg[N], q[N]; int tng[N], sg[N]; int n, m, _; inline void ad() { static int x, y; read(x), read(y), ++deg[x]; a[++_].v = y, a[_].nxt = head[x], head[x] = _; a[++_].v = x, a[_].nxt = Head[y], Head[y] = _; } int main() { read(n), read(m); for (int i = 1; i <= n; ++i) read(h[i]); for (int i = 1; i <= m; ++i) ad(); int he = 1, ta = 0; for (int x = 1; x <= n; ++x) if (!deg[x]) q[++ta] = x; while (he <= ta) { int x = q[he++]; for (int i = head[x]; i; i = a[i].nxt) tng[sg[a[i].v]] = 1; for (int i = 0; !sg[x]; ++i) if (!tng[i]) sg[x] = i; for (int i = head[x]; i; i = a[i].nxt) tng[sg[a[i].v]] = 0; for (int i = Head[x]; i; i = a[i].nxt) if (!(--deg[a[i].v])) q[++ta] = a[i].v; } m = 0; for (int i = 1; i <= n; ++i) m = max(m, sg[i]); for (int i = 1; i <= n; ++i) xr[sg[i]] ^= h[i]; int d = 0; for (int i = 1; i <= m; ++i) if (xr[i]) d = i; if (!d) return puts("LOSE"), 0; puts("WIN"); for (int x = 1; x <= n; ++x) if (sg[x] == d and (xr[sg[x]] ^ h[x]) < h[x]) { h[x] = (xr[sg[x]] ^ h[x]); xr[sg[x]] = 0; for (int i = head[x]; i; i = a[i].nxt) if (xr[sg[a[i].v]]) { h[a[i].v] ^= xr[sg[a[i].v]]; xr[sg[a[i].v]] = 0; } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); putchar(10); return 0; } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int prime = 999983; const int INF = 0x7FFFFFFF; const long long INFF = 0x7FFFFFFFFFFFFFFF; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-6; const long long mod = 1e9 + 7; long long qpow(long long a, long long b) { long long s = 1; while (b > 0) { if (b & 1) s = s * a % mod; a = a * a % mod; b >>= 1; } return s; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1}; const int maxn = 2e5 + 10; vector<int> G[maxn]; int topo[maxn]; int sg[maxn], deg[maxn], mem[maxn]; int Xor[maxn]; long long h[maxn]; int main(void) { int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) scanf("%lld", &h[i]); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); deg[v]++; } queue<int> q; int cnt = 1; for (int i = 1; i <= n; ++i) if (deg[i] == 0) q.push(i); while (!q.empty()) { int p = q.front(); q.pop(); topo[cnt++] = p; for (auto c : G[p]) { deg[c]--; if (deg[c] == 0) q.push(c); } } for (int i = n; i >= 1; --i) { int u = topo[i]; for (auto c : G[u]) mem[sg[c]] = i; while (mem[sg[u]] == i) sg[u]++; Xor[sg[u]] ^= h[u]; } int index = -1; for (int i = 0; i < maxn; ++i) if (Xor[i] != 0) index = i; if (index == -1) return 0 * puts("LOSE"); for (int i = 1; i <= n; ++i) { if (sg[i] == index && ((h[i] ^ Xor[index]) < h[i])) { h[i] ^= Xor[index]; for (auto c : G[i]) { h[c] ^= Xor[sg[c]]; Xor[sg[c]] = 0; } break; } } puts("WIN"); for (int i = 1; i <= n; ++i) printf("%lld ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int h[212345]; vector<vector<int> > graph; vector<int> mex; vector<int> xors; int get_mex(int v) { if (mex[v] != -1) return mex[v]; vector<bool> seen(graph[v].size(), false); for (auto u : graph[v]) { int val = get_mex(u); if (val < seen.size()) seen[val] = true; } for (mex[v] = 0; mex[v] < seen.size(); mex[v]++) { if (!seen[mex[v]]) break; } return mex[v]; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &h[i]); graph.resize(n); while (m--) { int a, b; scanf("%d%d", &a, &b); a--; b--; graph[a].push_back(b); } mex.resize(n, -1); for (int i = 0; i < n; i++) { get_mex(i); } xors.resize(n, 0); int maxmex = 0; for (int i = 0; i < n; i++) { maxmex = max(maxmex, mex[i]); xors[mex[i]] ^= h[i]; } int non_zero_mex = -1; for (int i = maxmex; i >= 0; i--) { if (xors[i] != 0) { non_zero_mex = i; break; } } if (non_zero_mex == -1) { printf("LOSE\n"); return 0; } int v; for (v = 0; v < n; v++) { if (mex[v] == non_zero_mex) if ((xors[mex[v]] ^ h[v]) < h[v]) break; } h[v] ^= xors[mex[v]]; xors[mex[v]] = 0; for (auto u : graph[v]) { if (xors[mex[u]] != 0) { h[u] ^= xors[mex[u]]; xors[mex[u]] = 0; } } printf("WIN\n"); for (int i = 0; i < n; i++) { printf("%d%c", h[i], (i + 1) == n ? '\n' : ' '); } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr int inf32 = 0x3f3f3f3f; constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; vector<long long> a(n); for (int u = 0; u < (n); ++u) cin >> a[u]; vector<vector<int> > adj(n); for (int i = 0; i < (m); ++i) { int u, v; cin >> u >> v, --u, --v; adj[u].push_back(v); } int k = 1; vector<int> g(n, 0), vis(n, 0); function<void(int)> dfs = [&](int u) { if (vis[u]) return; vis[u] = true; for (const int &v : adj[u]) dfs(v); vector<int> use(k, 0); for (const int &v : adj[u]) use[g[v]] = true; while (g[u] < k && use[g[u]]) ++g[u]; if (g[u] >= k) ++k; }; for (int u = 0; u < (n); ++u) dfs(u); vector<long long> f(k, 0); for (int u = 0; u < (n); ++u) f[g[u]] ^= a[u]; int mx = -1; for (int i = 0; i < (k); ++i) if (f[i]) mx = i; if (!~mx) return !(cout << "LOSE\n"); cout << "WIN\n"; for (int u = 0; u < (n); ++u) if (g[u] == mx && (a[u] ^ f[mx]) < a[u]) { a[u] ^= f[mx], f[mx] = 0; for (const int &v : adj[u]) a[v] ^= f[g[v]], f[g[v]] = 0; break; } for (int u = 0; u < (n); ++u) cout << a[u] << ' '; cout << '\n'; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = (int)998244353; const int MAXN = (int)2e5 + 3; const int infint = (int)1e9 + 3; const long long inf = (long long)1e18; int n, m, M[MAXN], visited[MAXN], h[MAXN], t[MAXN]; vector<int> G[MAXN], topol; void dfs(int u) { visited[u] = 1; for (auto v : G[u]) if (!visited[v]) dfs(v); topol.push_back(u); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u].push_back(v); } for (int i = 1; i <= n; i++) if (!visited[i]) dfs(i); reverse(topol.begin(), topol.end()); for (int i = n - 1; i >= 0; i--) { if (G[topol[i]].size() == 0) M[topol[i]] = 0; else { vector<int> cur; for (auto v : G[topol[i]]) cur.push_back(M[v]); sort(cur.begin(), cur.end()); int ptr = 0; for (auto v : cur) if (v == ptr) ptr++; M[topol[i]] = ptr; } } for (int i = 1; i <= n; i++) t[M[i]] ^= h[i]; int mika = -1; for (int i = 0; i < n; i++) if (t[i] > 0) mika = i; if (mika == -1) return cout << "LOSE\n", 0; cout << "WIN\n"; int id = -1; for (int i = 1; i <= n; i++) if (M[i] == mika && (h[i] ^ t[mika]) < h[i]) id = i; h[id] ^= t[mika]; memset(visited, 0, sizeof visited); for (auto v : G[id]) if (!visited[M[v]]) h[v] = h[v] ^ t[M[v]], visited[M[v]] = 1; for (int i = 1; i <= n; i++) cout << h[i] << " "; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using LL = long long; const int MAXN = 200010; vector<int> x[MAXN], y[MAXN]; int v[MAXN], out[MAXN], sg[MAXN]; int xr[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> v[i]; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; x[a].push_back(b); y[b].push_back(a); ++out[a]; } queue<int> Q; for (int i = 0; i < n; ++i) { if (out[i] == 0) Q.push(i); } int maxsg = 0; while (!Q.empty()) { int a = Q.front(); Q.pop(); for (int b : y[a]) { --out[b]; if (out[b] == 0) { Q.push(b); } } set<int> s; for (int b : x[a]) s.insert(sg[b]); for (int i = 0; i < n; ++i) { if (s.find(i) == s.end()) { sg[a] = i; break; } } maxsg = max(maxsg, sg[a]); } for (int i = 0; i < n; ++i) { xr[sg[i]] ^= v[i]; } bool win = false; for (int i = maxsg; i >= 0; --i) { if (xr[i] != 0) { win = true; int p = -1; for (int j = 0; j < n; ++j) { if (sg[j] != i) continue; if (p == -1) { p = j; continue; } if ((v[j] ^ xr[i]) <= v[j]) p = j; } xr[i] ^= v[p]; v[p] = xr[i]; xr[i] = 0; for (int b : x[p]) { if (xr[sg[b]] != 0) { v[b] ^= xr[sg[b]]; xr[sg[b]] = 0; } } break; } } if (win) { cout << "WIN\n"; for (int i = 0; i < n; ++i) cout << v[i] << " "; cout << '\n'; } else { cout << "LOSE\n"; } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename tp> inline void read(tp &x) { x = 0; char c = getchar(); int f = 0; for (; c < '0' || c > '9'; f |= c == '-', c = getchar()) ; for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar()) ; if (f) x = -x; } const int N = 3e5 + 233; int n, m, h[N], vis[N], sg[N], sum[N]; vector<int> G[N]; inline void dfs(int u) { if (vis[u]) return; vis[u] = true; vector<int> mark(G[u].size() + 1, 0); for (int v : G[u]) { dfs(v); if (sg[v] < mark.size()) mark[sg[v]] = true; } while (mark[sg[u]]) sg[u]++; sum[sg[u]] ^= h[u]; } int main(void) { read(n); read(m); for (register int i = 1; i <= (n); i++) read(h[i]); for (register int i = 1; i <= (m); i++) { int x, y; read(x); read(y); G[x].push_back(y); } for (register int i = 1; i <= (n); i++) if (!vis[i]) dfs(i); for (int i = n; i >= 0; i--) if (sum[i]) { cout << "WIN\n"; for (register int k = 1; k <= (n); k++) if (sg[k] == i && h[k] > (h[k] ^ sum[i])) { auto flip = [](int x) { h[x] ^= sum[sg[x]]; sum[sg[x]] = 0; }; flip(k); for (int v : G[k]) flip(v); break; } for (register int k = 1; k <= (n); k++) cout << h[k] << " \n"[k == n]; return 0; } cout << "LOSE\n"; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 200010; int head[MAXN], nxt[MAXN], to[MAXN], ind[MAXN], tot; void adde(int b, int e) { nxt[++tot] = head[b]; to[head[b] = tot] = e; ++ind[e]; } int A[MAXN], mx[MAXN]; std::set<int> S; int n, m; std::queue<int> q; int bfn[MAXN], idx; int B[MAXN]; int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> m; for (int i = 1; i <= n; ++i) std::cin >> A[i]; for (int i = 1, b, e; i <= m; ++i) std::cin >> b >> e, adde(b, e); for (int i = 1; i <= n; ++i) if (!ind[i]) q.push(i); while (!q.empty()) { int t = q.front(); q.pop(); bfn[++idx] = t; for (int i = head[t]; i; i = nxt[i]) if (!--ind[to[i]]) q.push(to[i]); } for (int T = idx; T; --T) { const int u = bfn[T]; for (int i = head[u]; i; i = nxt[i]) S.insert(mx[to[i]]); while (S.count(mx[u])) ++mx[u]; S.clear(); B[mx[u]] ^= A[u]; } int cur = n; while (cur >= 0 && !B[cur]) --cur; if (cur < 0) return std::cout << "LOSE" << std::endl, 0; for (int u = 1; u <= n; ++u) if (mx[u] == cur) { if ((A[u] ^ B[cur]) > A[u]) continue; A[u] ^= B[cur], B[cur] = 0; for (int i = head[u]; i; i = nxt[i]) A[to[i]] ^= B[mx[to[i]]], B[mx[to[i]]] = 0; break; } std::cout << "WIN" << std::endl; for (int i = 1; i <= n; ++i) std::cout << A[i] << ' '; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<vector<int>> E; vector<int> G; vector<int> h; void dfs(int cur) { if (G[cur] != -1) return; set<int> S; for (int i = 0; i < (E[cur].size()); ++i) { int to = E[cur][i]; dfs(to); S.insert(G[to]); } for (int i = 0; i < (1000000001); ++i) { if (!(S.count(i))) { G[cur] = i; break; } } } int main() { int n, m; scanf("%d %d", &n, &m); h.resize(n); for (int i = 0; i < (n); ++i) scanf("%d", &h[i]); E.resize(n); G.resize(n, -1); for (int i = 0; i < (m); ++i) { int u, v; scanf("%d %d", &u, &v); u--; v--; E[u].push_back(v); } for (int i = 0; i < (n); ++i) { dfs(i); } vector<int> Gs(1000, 0); for (int i = 0; i < (n); ++i) { Gs[G[i]] ^= h[i]; } for (int i = 999; i >= 0; i--) { if (Gs[i] == 0) continue; int maxi = -1, ind = -1; for (int j = 0; j < (n); ++j) { if (G[j] == i) { if ((Gs[i] ^ h[j]) < h[j]) { h[j] = Gs[i] ^ h[j]; Gs[i] = 0; ind = j; break; } } } h[ind] = Gs[i] ^ h[ind]; Gs[i] = 0; for (int j = 0; j < (E[ind].size()); ++j) { int to = E[ind][j]; h[to] = Gs[G[to]] ^ h[to]; Gs[G[to]] = 0; } cout << "WIN" << endl; for (int j = 0; j < (n); ++j) { if (j != 0) printf(" "); printf("%d", h[j]); } cout << endl; return 0; } cout << "LOSE" << endl; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class n, class second> ostream &operator<<(ostream &p, pair<n, second> x) { return p << "<" << x.first << ", " << x.second << ">"; } template <class n> auto operator<<(ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type { int o = 0; p << "{"; for (auto c : y) { if (o++) p << ", "; p << c; } return p << "}"; } void dor() { cerr << endl; } template <class n, class... second> void dor(n p, second... y) { cerr << p << " "; dor(y...); } template <class n, class second> void mini(n &p, second y) { if (p > y) p = y; } template <class n, class second> void maxi(n &p, second y) { if (p < y) p = y; } const int M = 1024 * 256; int H[M], mex[M]; int n, m, out[M], xo[M], xo2[M]; vector<int> V[M], T[M]; int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (int)(n); ++i) { scanf("%d", H + i); } for (int i = (1); i <= (int)(m); ++i) { int a, b; scanf("%d%d", &a, &b); V[a].push_back(b); T[b].push_back(a); out[a]++; } queue<int> Q; for (int i = (1); i <= (int)(n); ++i) { if (out[i] == 0) { Q.push(i); } } while (!Q.empty()) { int v = Q.front(); Q.pop(); vector<int> X; for (auto x : V[v]) { X.push_back(mex[x]); } sort((X).begin(), (X).end()); X.resize(unique((X).begin(), (X).end()) - X.begin()); X.push_back(1e9); for (int i = (0); i <= (int)((int)(X).size() - 1); ++i) { if (X[i] != i) { mex[v] = i; break; } } for (auto x : T[v]) { out[x]--; if (out[x] == 0) { Q.push(x); } } xo[mex[v]] ^= H[v]; } int t = -1; for (int i = (0); i <= (int)(n); ++i) { if (xo[i] != 0) { t = i; } } if (t == -1) { printf("LOSE\n"); return 0; } printf("WIN\n"); bool boo = false; for (int i = (1); i <= (int)(n); ++i) { if (mex[i] != t || (xo[t] ^ H[i]) >= H[i]) { continue; } boo = true; H[i] = xo[t] ^ H[i]; for (auto x : V[i]) { if (xo[mex[x]] != 0) { H[x] = xo[mex[x]] ^ H[x]; xo[mex[x]] = 0; } } break; } assert(boo); for (int i = (1); i <= (int)(n); ++i) { printf("%d ", H[i]); xo2[mex[i]] ^= H[i]; } for (int i = (0); i <= (int)(n); ++i) { assert(xo2[i] == 0); } printf("\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> a[200005]; struct edge { int to, nxt; } e[200005]; int hed[200005], cnt; inline void add(int u, int v) { e[++cnt] = (edge){v, hed[u]}; hed[u] = cnt; } int num[200005], ru[200005]; int sg[200005], tag[200005], dep[200005]; inline void topo() { queue<int> q; for (int i = 1; i <= n; ++i) if (!ru[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < a[u].size(); ++i) tag[dep[a[u][i]]] = u; while (tag[dep[u]] == u) dep[u]++; sg[dep[u]] ^= num[u]; for (int i = hed[u]; i; i = e[i].nxt) { int v = e[i].to; if (--ru[v] == 0) q.push(v); } } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", num + i); int u, v; for (int i = 1; i <= m; ++i) { scanf("%d%d", &u, &v); a[u].push_back(v), add(v, u), ru[u]++; } topo(); for (int i = n; i >= 0; --i) { if (sg[i] == 0) continue; for (int j = 1; j <= n; ++j) { if (dep[j] == i && num[j] > (num[j] ^ sg[i])) { u = j; break; } } num[u] ^= sg[i]; for (int j = 0; j < a[u].size(); ++j) { int v = a[u][j]; num[v] ^= sg[dep[v]], sg[dep[v]] = 0; } puts("WIN"); for (int j = 1; j <= n; ++j) printf("%d ", num[j]); return 0; } return puts("LOSE"), 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct node { int t, next; } a[200010], a1[200010]; vector<int> v[200010]; queue<int> q; int head[200010], head1[200010], num[200010], out[200010], f[200010], sum[200010], n, m, tot, tot1; bool vis[200010]; inline int rd() { int x = 0; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return x; } inline void print(int x) { static char s[233]; if (!x) { putchar('0'); putchar(' '); return; } int tot = 0; for (; x; x /= 10) s[++tot] = x % 10 + '0'; for (; tot; tot--) putchar(s[tot]); putchar(' '); } inline void add(int x, int y) { a[++tot].t = y; a[tot].next = head[x]; head[x] = tot; } inline void add1(int x, int y) { a1[++tot1].t = y; a1[tot1].next = head1[x]; head1[x] = tot1; } int main() { n = rd(); m = rd(); tot = tot1 = 0; for (int i = 1; i <= n; i++) num[i] = rd(); for (int i = 1; i <= m; i++) { int x = rd(), y = rd(); add(x, y); add1(y, x); out[x]++; } for (int i = 1; i <= n; i++) if (!out[i]) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = a[i].next) vis[f[a[i].t]] = true; for (int i = 0; i <= n; i++) if (!vis[i]) { f[x] = i; break; } for (int i = head[x]; i; i = a[i].next) vis[f[a[i].t]] = false; v[f[x]].push_back(x); sum[f[x]] ^= num[x]; for (int i = head1[x]; i; i = a1[i].next) { int t = a1[i].t; if (!(--out[t])) q.push(t); } } for (int i = n; ~i; i--) if (sum[i]) { puts("WIN"); for (int x : v[i]) if (num[x] >= (sum[i] ^ num[x])) { num[x] = sum[i] ^ num[x]; for (int j = head[x]; j; j = a[j].next) { int t = a[j].t; if (vis[f[t]]) continue; vis[f[t]] = true; num[t] = sum[f[t]] ^ num[t]; } break; } for (int j = 1; j <= n; j++) print(num[j]); putchar('\n'); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.stream.IntStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.lang.reflect.Field; import java.util.OptionalInt; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); EElectionPromises solver = new EElectionPromises(); solver.solve(1, in, out); out.close(); } } static class EElectionPromises { IntegerVersionArray iva = new IntegerVersionArray((int) 1e6); public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); int m = in.ri(); Node[] nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(); nodes[i].h = in.ri(); } for (int i = 0; i < m; i++) { Node a = nodes[in.ri() - 1]; Node b = nodes[in.ri() - 1]; a.adj.add(b); } int[] sg = new int[n]; for (int i = 0; i < n; i++) { sg[sg(nodes[i])] ^= nodes[i].h; } int max = Arrays.stream(sg).max().orElse(-1); if (max == 0) { out.println("LOSE"); return; } out.println("WIN"); int maxIndex = 0; for (int i = 0; i < n; i++) { if (sg[i] > 0) { maxIndex = i; } } Node end = null; for (Node node : nodes) { if (sg(node) == maxIndex && (node.h ^ sg[maxIndex]) < node.h) { end = node; break; } } end.h ^= sg[maxIndex]; sg[maxIndex] = 0; for (Node node : end.adj) { node.h ^= sg[sg(node)]; sg[sg(node)] = 0; } for (Node node : nodes) { out.append(node.h).append(' '); } } public int sg(Node root) { if (root.sg == -1) { root.sg = 0; for (Node node : root.adj) { sg(node); } iva.clear(); for (Node node : root.adj) { iva.set(sg(node), 1); } while (iva.get(root.sg) == 1) { root.sg++; } } return root.sg; } } static class IntegerVersionArray { int[] data; int[] version; int now; int[] def; public IntegerVersionArray(int cap) { this(cap, null); } public IntegerVersionArray(int cap, int[] def) { data = new int[cap]; version = new int[cap]; now = 0; this.def = def; } public void clear() { now++; } public void visit(int i) { if (version[i] < now) { version[i] = now; data[i] = def == null ? 0 : def[i]; } } public void set(int i, int v) { version[i] = now; data[i] = v; } public int get(int i) { visit(i); return data[i]; } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < data.length; i++) { if (version[i] < now) { continue; } builder.append(i).append(':').append(data[i]).append(','); } if (builder.length() > 0) { builder.setLength(builder.length() - 1); } return builder.toString(); } } static class Node { List<Node> adj = new ArrayList<>(); int h; int sg = -1; } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int ri() { return readInt(); } public int readInt() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private OutputStream writer; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); private static Field stringBuilderValueField; private char[] charBuf = new char[THRESHOLD * 2]; private byte[] byteBuf = new byte[THRESHOLD * 2]; static { try { stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value"); stringBuilderValueField.setAccessible(true); } catch (Exception e) { stringBuilderValueField = null; } stringBuilderValueField = null; } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(OutputStream writer) { this.writer = writer; } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(int c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println(String c) { return append(c).println(); } public FastOutput println() { return append('\n'); } public FastOutput flush() { try { if (stringBuilderValueField != null) { try { byte[] value = (byte[]) stringBuilderValueField.get(cache); writer.write(value, 0, cache.length()); } catch (Exception e) { stringBuilderValueField = null; } } if (stringBuilderValueField == null) { int n = cache.length(); if (n > byteBuf.length) { //slow writer.write(cache.toString().getBytes(StandardCharsets.UTF_8)); // writer.append(cache); } else { cache.getChars(0, n, charBuf, 0); for (int i = 0; i < n; i++) { byteBuf[i] = (byte) charBuf[i]; } writer.write(byteBuf, 0, n); } } writer.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { writer.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read() { char c = getchar(); int x = 0; while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return x; } int n, m; const int _ = 2e5 + 7; int val[_], v[_]; vector<int> E[_]; int deg[_]; int st[_], top; int q[_], h[_]; bool used[_]; int main() { n = read(), m = read(); for (int i = 1; i <= n; ++i) v[i] = read(); for (int i = 1; i <= m; ++i) { int u = read(), v = read(); E[u].push_back(v); deg[v]++; } int l = 1, r = 0; for (int i = 1; i <= n; ++i) if (!deg[i]) q[++r] = i; while (l <= r) { int x = q[l++]; st[++top] = x; for (int i = 0; i < (int)E[x].size(); ++i) { int y = E[x][i]; deg[y]--; if (!deg[y]) q[++r] = y; } } while (top) { int x = st[top]; top--; for (int i = 0; i < (int)E[x].size(); ++i) { int y = E[x][i]; used[val[y]] = 1; } for (int i = 0; i <= m; ++i) { if (used[i]) continue; val[x] = i; break; } for (int i = 0; i < (int)E[x].size(); ++i) { int y = E[x][i]; used[val[y]] = 0; } } int tag = 0; for (int i = 1; i <= n; ++i) h[val[i]] ^= v[i]; for (int i = 0; i <= m; ++i) tag |= h[i]; if (!tag) return puts("LOSE"), 0; puts("WIN"); int mx = 0; for (int i = 0; i <= m; ++i) { if (h[i]) mx = i; } for (int u = 1; u <= n; ++u) { if (val[u] == mx && (h[mx] ^ v[u]) < v[u]) { v[u] = h[mx] ^ v[u]; h[mx] = 0; for (int i = 0; i < (int)E[u].size(); ++i) { int y = E[u][i]; if (h[val[y]]) { v[y] = h[val[y]] ^ v[y]; h[val[y]] = 0; } } break; } } for (int i = 1; i <= n; ++i) printf("%d%c", v[i], " \n"[i == n]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int gi() { int x = 0, w = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') w = 0, ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return w ? x : -x; } const int N = 2e5 + 5; int n, m, h[N], in[N], q[N], hd, tl, mrk[N], sg[N], sum[N], vis[N]; vector<int> E[N]; int main() { n = gi(); m = gi(); for (int i = 1; i <= n; ++i) h[i] = gi(); for (int i = 1, x, y; i <= m; ++i) x = gi(), ++in[y = gi()], E[x].push_back(y); for (int i = 1; i <= n; ++i) if (!in[i]) q[++tl] = i; while (hd < tl) { int u = q[++hd]; for (int v : E[u]) if (!--in[v]) q[++tl] = v; } for (int i = n; i; --i) { int u = q[i]; for (int v : E[u]) mrk[sg[v]] = u; while (mrk[sg[u]] == u) ++sg[u]; sum[sg[u]] ^= h[u]; } for (int i = n - 1; ~i; --i) if (sum[i]) { puts("WIN"); for (int j = 1; j <= n; ++j) if (sg[j] == i && h[j] > (h[j] ^ sum[i])) { h[j] ^= sum[i]; for (int v : E[j]) h[v] ^= sum[sg[v]], sum[sg[v]] = 0; break; } for (int j = 1; j <= n; ++j) printf("%d ", h[j]); return puts(""), 0; } return puts("LOSE"), 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long h[200005], s[200005]; int n, m, a, b, ru[200006], mex[200005], fu[200005], ma; vector<int> x[200005]; int dfs(int now) { if (mex[now]) return mex[now]; for (int i = 0; i < x[now].size(); i++) dfs(x[now][i]); for (int i = 0; i < x[now].size(); i++) fu[mex[x[now][i]]] = now; for (int i = 0; i <= n; i++) if (fu[i] != now) return mex[now] = i; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", h + i); for (int j = 1; j <= m; j++) { scanf("%d%d", &a, &b); x[a].push_back(b); ru[b]++; } for (int i = 1; i <= n; i++) if (ru[i] == 0) dfs(i); for (int i = 1; i <= n; i++) s[mex[i]] ^= h[i]; int ans = 0; ma = -1; memset(fu, 0, sizeof(fu)); for (int i = 1; i <= n; i++) if (s[mex[i]] != 0) fu[mex[i]] = 1, ma = max(ma, mex[i]); if (~ma) { puts("WIN"); for (int i = 1; i <= n; i++) { if (mex[i] == ma && (h[i] ^ s[mex[i]]) < h[i]) { h[i] ^= s[mex[i]]; for (int j = 0; j < x[i].size(); j++) if (fu[mex[x[i][j]]]) h[x[i][j]] ^= s[mex[x[i][j]]], fu[mex[x[i][j]]] = 0; for (int i = 1; i <= n; i++) printf("%lld ", h[i]); return 0; } } } else puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200005, maxm = 200005; int n, m, e, stp; int h[maxn], SG[maxn], start[maxn], to[maxm], then[maxm], xorsum[maxn], deg[maxn], vis[maxn]; vector<int> v[maxn], revg[maxn], g[maxn]; queue<int> q; inline void add(int x, int y) { g[x].push_back(y), deg[x]++; revg[y].push_back(x); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); } for (int i = 1; i <= n; i++) if (deg[i] == 0) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(), stp++; for (int i = 0; i < revg[x].size(); i++) { int y = revg[x][i]; deg[y]--; if (deg[y] == 0) q.push(y); } for (int i = 0; i < g[x].size(); i++) vis[SG[g[x][i]]] = stp; while (vis[SG[x]] == stp) SG[x]++; xorsum[SG[x]] ^= h[x], v[SG[x]].push_back(x); } for (int i = n; i >= 0; i--) { if (xorsum[i] == 0) continue; puts("WIN"); int x; for (int j = 0; j < v[i].size(); j++) if ((h[v[i][j]] ^ xorsum[i]) < h[v[i][j]]) { x = v[i][j]; break; } h[x] ^= xorsum[SG[x]], xorsum[SG[x]] = 0; for (int j = 0; j < g[x].size(); j++) { int y = g[x][j]; h[y] ^= xorsum[SG[y]], xorsum[SG[y]] = 0; } for (int j = 1; j <= n; j++) printf("%d%c", h[j], i == n ? '\n' : ' '); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vector<long long> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp1(vector<int> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp2(vector<vector<int>> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cerr << ou[i][j] << ' '; } cerr << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 301; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } int inf = 1e9 + 110; void reverse(vector<vector<int>> &v, vector<int> &sign, long long &sumx, long long &sumy, int pos, vector<int> &init) { if (sumx <= 1e6 + 1 && sumx >= -1e6 - 2) if (sumy <= 1e6 + 1 && sumy >= -1e6 - 2) { vector<int> reals(v.size()); for (int i = 0; i < v.size(); i++) { reals[v[i][2]] = sign[i]; } for (int i = 0; i < v.size(); i++) cout << reals[i] * init[i] << ' '; cout << '\n'; exit(0); } if (sign[pos] == 1) { sign[pos] = -1; sumx -= 2 * v[pos][0]; sumy -= 2 * v[pos][1]; } else { sign[pos] = 1; sumx += 2 * v[pos][0]; sumy += 2 * v[pos][1]; } if (sumx <= 1e6 + 2 && sumx >= -1e6 - 2) if (sumy <= 1e6 + 2 && sumy >= -1e6 - 2) { vector<int> reals(v.size()); for (int i = 0; i < v.size(); i++) { reals[v[i][2]] = sign[i]; } for (int i = 0; i < v.size(); i++) cout << reals[i] * init[i] << ' '; cout << '\n'; exit(0); } } int mexx(vector<int> &v) { vector<int> u(v.size() + 2); for (auto it : v) { if (it < u.size()) u[it] = 1; } for (int i = 0; i < u.size(); i++) if (u[i] == 0) return i; } void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n, m; cin >> n >> m; vector<vector<int>> gr(n), revgr(n); vector<int> grundy(n, -1), degs(n); vector<long long> vals(n); for (int i = 0; i < n; i++) cin >> vals[i]; set<int> zerodeg; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; degs[x]++; gr[x].push_back(y); revgr[y].push_back(x); } for (int i = 0; i < n; i++) if (!degs[i]) zerodeg.insert(i); while (!zerodeg.empty()) { int a = *zerodeg.begin(); vector<int> v; for (auto g : gr[a]) v.push_back(grundy[g]); grundy[a] = mexx(v); for (auto g : revgr[a]) { degs[g]--; if (degs[g] == 0) zerodeg.insert(g); } zerodeg.erase(a); } vector<int> xors(n + 1); vector<vector<int>> verts(n + 1); for (int i = 0; i < n; i++) { xors[grundy[i]] ^= vals[i]; verts[grundy[i]].push_back(i); } int pos = -1; for (int i = n; i >= 0; i--) if (xors[i] != 0) { pos = i; break; } if (pos == -1) cout << "LOSE" << '\n'; else { int p1 = -1; for (auto ver : verts[pos]) { long long need = vals[ver] ^ xors[pos]; if (need < vals[ver]) { p1 = ver; break; } } assert(p1 != -1); cout << "WIN" << '\n'; vals[p1] = vals[p1] ^ xors[pos]; for (auto ver : gr[p1]) { int cal = grundy[ver]; vals[ver] = xors[cal] ^ vals[ver]; xors[cal] = 0; } for (auto it : vals) cout << it << ' '; cout << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int tests = 1; for (int i = 0; i < tests; i++) solve(); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200005; int n, m, sg[N]; bool r[N]; long long a[N], sum[N]; std::vector<int> g[N]; bool vis[N]; int dfs(int x) { if (~sg[x]) return sg[x]; for (int p : g[x]) dfs(p); for (int p : g[x]) vis[dfs(p)] = 1; for (int i = 0;; ++i) if (!vis[i]) { sg[x] = i; break; } for (int p : g[x]) vis[dfs(p)] = 0; return sg[x]; } void print(int col) { std::cout << "WIN\n"; int pos = 0; for (int i = 1; i <= n; ++i) if (sg[i] == col && (sum[col] ^ a[i]) < a[i]) pos = i; a[pos] ^= sum[col], sum[col] = 0; for (int p : g[pos]) a[p] ^= sum[sg[p]], sum[sg[p]] = 0; for (int i = 1; i <= n; ++i) std::cout << a[i] << ' '; std::cout << '\n', std::exit(0); } int main() { std::ios::sync_with_stdio(0), std::cin.tie(0); std::cin >> n >> m; for (int i = 1; i <= n; ++i) std::cin >> a[i], sg[i] = -1; for (int i = 0, x, y; i < m; ++i) std::cin >> x >> y, g[x].emplace_back(y); for (int i = 1; i <= n; ++i) sum[dfs(i)] ^= a[i]; for (int i = n - 1; ~i; --i) if (sum[i]) print(i); std::cout << "LOSE\n"; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 200005; long long ver[N << 1], nxt[N << 1], head[N], deg[N], tot; long long h[N], st[N], vis[N << 1], id[N], t[N << 1], Max, top, n, m; queue<long long> q; inline long long read() { long long x = 0, ff = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') ff = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * ff; } void add(long long x, long long y) { ver[++tot] = y; nxt[tot] = head[x]; head[x] = tot; deg[y]++; } signed main() { n = read(); m = read(); for (long long i = 1; i <= n; i++) h[i] = read(); for (long long i = 1; i <= m; i++) { long long u, v; u = read(); v = read(); add(u, v); } for (long long i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (q.size()) { long long x = q.front(); q.pop(); st[++top] = x; for (long long i = head[x]; i; i = nxt[i]) { long long y = ver[i]; deg[y]--; if (!deg[y]) q.push(y); } } for (long long i = top; i >= 1; i--) { long long now = st[i]; for (long long j = head[now]; j; j = nxt[j]) vis[id[ver[j]]] = 1; for (long long nw = 0;; nw++) { if (!vis[nw]) { id[now] = nw; break; } } t[id[now]] ^= h[now]; Max = max(Max, id[now]); for (long long j = head[now]; j; j = nxt[j]) vis[id[ver[j]]] = 0; } long long maxx = -1; for (long long i = 0; i <= Max; i++) { if (t[i]) maxx = max(maxx, i); } if (maxx == -1) { puts("LOSE"); return 0; } for (long long i = 1; i <= n; i++) { if (id[i] == maxx && (t[id[i]] ^ h[i]) < h[i]) { h[i] ^= t[id[i]]; for (long long j = head[i]; j; j = nxt[j]) { long long y = ver[j]; if (t[id[y]]) { h[y] ^= t[id[y]]; t[id[y]] = 0; } } break; } } puts("WIN"); for (long long i = 1; i <= n; i++) printf("%lld ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vector<long long> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp1(vector<int> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp2(vector<vector<int>> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cerr << ou[i][j] << ' '; } cerr << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 301; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } int inf = 1e9 + 110; void reverse(vector<vector<int>> &v, vector<int> &sign, long long &sumx, long long &sumy, int pos, vector<int> &init) { if (sumx <= 1e6 + 1 && sumx >= -1e6 - 2) if (sumy <= 1e6 + 1 && sumy >= -1e6 - 2) { vector<int> reals(v.size()); for (int i = 0; i < v.size(); i++) { reals[v[i][2]] = sign[i]; } for (int i = 0; i < v.size(); i++) cout << reals[i] * init[i] << ' '; cout << '\n'; exit(0); } if (sign[pos] == 1) { sign[pos] = -1; sumx -= 2 * v[pos][0]; sumy -= 2 * v[pos][1]; } else { sign[pos] = 1; sumx += 2 * v[pos][0]; sumy += 2 * v[pos][1]; } if (sumx <= 1e6 + 2 && sumx >= -1e6 - 2) if (sumy <= 1e6 + 2 && sumy >= -1e6 - 2) { vector<int> reals(v.size()); for (int i = 0; i < v.size(); i++) { reals[v[i][2]] = sign[i]; } for (int i = 0; i < v.size(); i++) cout << reals[i] * init[i] << ' '; cout << '\n'; exit(0); } } int mexx(vector<int> &v) { vector<int> u(v.size() + 2); for (auto it : v) { if (it < u.size()) u[it] = 1; } for (int i = 0; i < u.size(); i++) if (u[i] == 0) return i; } void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n, m; cin >> n >> m; vector<vector<int>> gr(n), revgr(n); vector<int> grundy(n, -1), degs(n); vector<long long> vals(n); for (int i = 0; i < n; i++) cin >> vals[i]; set<int> zerodeg; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; degs[x]++; gr[x].push_back(y); revgr[y].push_back(x); } for (int i = 0; i < n; i++) if (!degs[i]) zerodeg.insert(i); while (!zerodeg.empty()) { int a = *zerodeg.begin(); vector<int> v; for (auto g : gr[a]) v.push_back(grundy[g]); grundy[a] = mexx(v); for (auto g : revgr[a]) { degs[g]--; if (degs[g] == 0) zerodeg.insert(g); } zerodeg.erase(a); } vector<long long> xors(n + 1); vector<vector<int>> verts(n + 1); for (int i = 0; i < n; i++) { xors[grundy[i]] ^= vals[i]; verts[grundy[i]].push_back(i); } int pos = -1; for (int i = n; i >= 0; i--) if (xors[i] != 0) { pos = i; break; } if (pos == -1) cout << "LOSE" << '\n'; else { int p1 = -1; for (auto ver : verts[pos]) { long long need = vals[ver] ^ xors[pos]; if (need < vals[ver]) { p1 = ver; break; } } assert(p1 != -1); cout << "WIN" << '\n'; vals[p1] = vals[p1] ^ xors[pos]; for (auto ver : gr[p1]) { int cal = grundy[ver]; vals[ver] = xors[cal] ^ vals[ver]; xors[cal] = 0; } for (auto it : vals) cout << it << ' '; cout << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int tests = 1; for (int i = 0; i < tests; i++) solve(); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct edge { int to, nxt; } e[N]; vector<int> G[N]; int deg[N], n, head[N], cnt, m, h[N], mex[N], X[N]; bool vis[N]; queue<int> q; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", h + i); while (m--) { int u, v; scanf("%d%d", &u, &v); e[++cnt] = (edge){v, head[u]}, head[u] = cnt, ++deg[u]; G[v].push_back(u); } for (int i = 1; i <= n; ++i) if (deg[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = e[i].nxt) vis[mex[e[i].to]] = 1; while (vis[mex[u]]) ++mex[u]; for (int i = head[u]; i; i = e[i].nxt) vis[mex[e[i].to]] = 0; for (int to : G[u]) if (!--deg[to]) q.push(to); } for (int i = 1; i <= n; ++i) X[mex[i]] ^= h[i]; for (int i = n; ~i; --i) if (X[i]) { puts("WIN"); for (int j = 1; j <= n; ++j) if (mex[j] == i && (h[j] ^ X[i]) < h[j]) { h[j] ^= X[i], X[i] = 0; for (int k = head[j]; k; k = e[k].nxt) { int to = e[k].to; h[to] ^= X[mex[to]], X[mex[to]] = 0; } } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') f = -1; } for (; isdigit(ch); ch = getchar()) { x = x * 10 + ch - 48; } return x * f; } const int mxN = 2e5; struct Edge { int v, nxt; } e[(mxN << 1) + 3]; int fe[mxN + 3]; int a[mxN + 3]; int f[mxN + 3], sg[mxN + 3]; int ind[mxN + 3]; bool vis[mxN + 3]; int n, m, en; void addedge(int u, int v) { en++; e[en].v = v; e[en].nxt = fe[u]; fe[u] = en; } void dfs(int u) { if (vis[u]) return; vis[u] = true; vector<int> apr; for (int i = fe[u]; i; i = e[i].nxt) { int v = e[i].v; dfs(v); apr.push_back(f[v]); } sort(apr.begin(), apr.end()); f[u] = 0; for (int i = 0; i < apr.size(); i++) { if (apr[i] == f[u]) { f[u]++; } else if (apr[i] > f[u]) { break; } } } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= m; i++) { int u = read(), v = read(); ind[v]++; addedge(u, v); } for (int i = 1; i <= n; i++) { if (ind[i] == 0) { dfs(i); } } for (int i = 1; i <= n; i++) { sg[f[i]] ^= a[i]; } for (int i = n; i >= 0; i--) if (sg[i]) { puts("WIN"); for (int u = 1; u <= n; u++) if (f[u] == i && (a[u] ^ sg[i]) < a[u]) { a[u] ^= sg[i]; for (int o = fe[u]; o; o = e[o].nxt) { int v = e[o].v; a[v] ^= sg[f[v]]; sg[f[v]] = 0; } break; } for (int u = 1; u <= n; u++) printf("%d ", a[u]); puts(""); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200005; int n, m, t = -1, he, ta, sg[N], d[N], s[N], q[N]; bool vis[N]; long long h[N]; struct ed { ed* nxt; int to; } pool[N], *p = pool, *lnk[N]; void ae(int u, int v) { *++p = (ed){lnk[u], v}, ++d[v], lnk[u] = p; } void dfs(int u) {} int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (n); ++i) scanf("%lld", h + i); for (int i = (1); i <= (m); ++i) { int u, v; scanf("%d%d", &u, &v); ae(u, v); } for (int i = (1); i <= (n); ++i) if (!d[i]) q[++ta] = i; while (he != ta) { int u = q[++he]; for (ed* i = lnk[u]; i; i = i->nxt) if (!--d[i->to]) q[++ta] = i->to; } for (int i = (n); i >= (1); --i) { int u = q[i]; for (ed* i = lnk[u]; i; i = i->nxt) vis[sg[i->to]] = 1; while (vis[sg[u]]) ++sg[u]; for (ed* i = lnk[u]; i; i = i->nxt) vis[sg[i->to]] = 0; s[sg[u]] ^= h[u]; } for (int i = (0); i <= (n); ++i) if (s[i]) t = i; if (t == -1) return 0 & puts("LOSE"); for (int i = (1); i <= (n); ++i) { if (sg[i] == t && ((h[i] ^ s[t]) < h[i])) { h[i] ^= s[t]; for (ed* j = lnk[i]; j; j = j->nxt) { h[j->to] ^= s[sg[j->to]]; s[sg[j->to]] = 0; } break; } } puts("WIN"); for (int i = (1); i <= (n); ++i) printf("%lld ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 400005; int n, m, i, j, k, h[N], head[N], adj[N], nxt[N], sg[N], q[N], bg, ed, d[N], s[N]; bool v[N]; int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", h + i); for (i = 1; i <= m; ++i) { scanf("%d%d", &j, &k); adj[i] = k; nxt[i] = head[j]; head[j] = i; ++d[k]; } for (i = 1; i <= n; ++i) if (!d[i]) q[++ed] = i; bg = 1; while (bg <= ed) { for (i = head[q[bg]]; i; i = nxt[i]) { --d[adj[i]]; if (d[adj[i]] == 0) q[++ed] = adj[i]; } ++bg; } for (i = n; i >= 1; --i) { for (j = head[q[i]]; j; j = nxt[j]) v[sg[adj[j]]] = true; for (j = 0; v[j]; ++j) ; sg[q[i]] = j; for (j = head[q[i]]; j; j = nxt[j]) v[sg[adj[j]]] = false; } for (i = 1; i <= n; ++i) s[sg[i]] ^= h[i]; for (i = n; i >= 0; --i) if (s[i]) { puts("WIN"); for (j = 1; j <= n; ++j) if (sg[j] == i && (s[i] ^ h[j]) <= h[j]) { h[j] = s[i] ^ h[j]; for (k = 0; k <= n; ++k) v[k] = false; for (k = head[j]; k; k = nxt[k]) if (!v[sg[adj[k]]]) { v[sg[adj[k]]] = true; h[adj[k]] = s[sg[adj[k]]] ^ h[adj[k]]; } for (k = 1; k <= n; ++k) printf("%d ", h[k]); return 0; } } printf("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } int n, m, val[maxn], Id[maxn], sum[maxn], vis[maxn]; vector<int> to[maxn]; int dfs(int u) { if (vis[u]) return Id[u]; vector<int> t; vis[u] = 1; for (int v : to[u]) t.push_back(dfs(v)); sort(t.begin(), t.end()); int lst = -1; Id[u] = -1; for (int i = 0; i < t.size(); ++i) if (t[i] > lst + 1) { Id[u] = lst + 1; break; } else lst = t[i]; if (Id[u] == -1) Id[u] = lst + 1; return Id[u]; } int main() { n = gi(); m = gi(); for (int i = 1; i <= n; ++i) val[i] = gi(); for (int u, v, i = 1; i <= m; ++i) u = gi(), v = gi(), to[u].push_back(v); for (int i = 1; i <= n; ++i) if (!vis[i]) dfs(i); for (int i = 1; i <= n; ++i) sum[Id[i]] ^= val[i]; int pos = -1; for (int i = 0; i <= n; ++i) if (sum[i]) pos = i; if (~pos) { puts("WIN"); for (int i = 1; i <= n; ++i) if (Id[i] == pos && (val[i] ^ sum[pos]) < val[i]) { val[i] = val[i] ^ sum[pos]; sum[pos] = 0; for (int v : to[i]) val[v] = sum[Id[v]] ^ val[v], sum[Id[v]] = 0; } for (int i = 1; i <= n; ++i) printf("%d ", val[i]); } else puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int head[200005], ver[200005], nxt[200005], cnt; void add(int a, int b) { ver[++cnt] = b, nxt[cnt] = head[a], head[a] = cnt; } vector<int> e[200005], has[200005]; int n, m, cd[200005]; long long a[200005], sg[200005]; int pos[200005], mex[200005], mx; bool vis[200005]; queue<int> q; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1, a, b; i <= m; i++) { scanf("%d%d", &a, &b); add(a, b), e[b].push_back(a), cd[a]++; } for (int i = 1; i <= n; i++) if (!cd[i]) q.push(i); while (!q.empty()) { int now = q.front(); q.pop(), cnt = 0; for (int i = head[now]; i; i = nxt[i]) mex[++cnt] = pos[ver[i]]; pos[now] = 1; sort(mex + 1, mex + 1 + cnt); for (int i = 1; i <= cnt; i++) if (mex[i] == pos[now]) pos[now]++; sg[pos[now]] ^= a[now]; has[pos[now]].push_back(now); mx = max(mx, pos[now]); for (int i = 0; i < e[now].size(); i++) { cd[e[now][i]]--; if (!cd[e[now][i]]) q.push(e[now][i]); } } for (int o = mx; o >= 1; o--) { if (sg[o] != 0) { int now; for (int i = 0; i < has[o].size(); i++) if ((a[has[o][i]] ^ sg[o]) <= a[has[o][i]]) { now = has[o][i]; break; } a[now] ^= sg[o]; for (int i = head[now]; i; i = nxt[i]) { int to = ver[i]; if (vis[pos[to]]) continue; vis[pos[to]] = 1; a[to] = sg[pos[to]] ^ a[to]; } printf("WIN\n"); for (int i = 1; i <= n; i++) printf("%lld ", a[i]); return 0; } } printf("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> E[N]; int n, m, h[N], deg[N], vis[N], sg[N], sum[N], sq[N], tt = 0; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 1, u, v; i <= m; i++) cin >> u >> v, ++deg[v], E[u].push_back(v); queue<int> q; for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); sq[++tt] = u; for (auto v : E[u]) if (!--deg[v]) q.push(v); } for (int i = tt, u; i; i--) { u = sq[i]; for (auto v : E[u]) vis[sg[v]] = u; for (int j = 0;; j++) if (vis[j] ^ u) { sum[sg[u] = j] ^= h[u]; break; } } for (int i = n - 1; ~i; i--) if (sum[i]) { puts("WIN"); for (int u = 1; u <= n; u++) if (sg[u] == i && (h[u] ^ sum[i]) < h[u]) { h[u] ^= sum[i]; for (auto v : E[u]) h[v] ^= sum[sg[v]], sum[sg[v]] = 0; for (int v = 1; v <= n; v++) cout << h[v] << ' '; return 0; } } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
java
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CF1149E { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io, new Debug(local)); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; final Debug debug; int inf = (int) 1e8; int mod = (int) 1e9 + 7; public int mod(int val) { val %= mod; if (val < 0) { val += mod; } return val; } public int mod(long val) { val %= mod; if (val < 0) { val += mod; } return (int) val; } public Task(FastIO io, Debug debug) { this.io = io; this.debug = debug; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); int m = io.readInt(); Node[] nodes = new Node[n + 1]; for (int i = 1; i <= n; i++) { nodes[i] = new Node(); } for (int i = 1; i <= n; i++) { nodes[i].tax = io.readInt(); } for (int i = 0; i < m; i++) { nodes[io.readInt()].next.add(nodes[io.readInt()]); } for (int i = 1; i <= n; i++) { dfs(nodes[i]); } int[] xor = new int[n]; for (int i = 1; i <= n; i++) { xor[nodes[i].mex] ^= nodes[i].tax; } boolean allZero = true; int mex = n - 1; for (int i = 0; i < n; i++) { allZero = allZero && xor[i] == 0; if (xor[i] != 0) { mex = i; } } if (allZero) { io.cache.append("LOSE"); return; } Node heldOn = null; for (int i = 1; i <= n; i++) { if (nodes[i].mex == mex && (nodes[i].tax ^ xor[mex]) < nodes[i].tax) { heldOn = nodes[i]; break; } } heldOn.tax = heldOn.tax ^ xor[mex]; xor[mex] = 0; for (Node node : heldOn.next) { if (xor[node.mex] == 0) { continue; } node.tax ^= xor[node.mex]; xor[node.mex] = 0; } io.cache.append("WIN\n"); for (int i = 1; i <= n; i++) { io.cache.append(nodes[i].tax).append(' '); } } public void dfs(Node root) { if (root.visited) { return; } root.visited = true; for (Node node : root.next) { dfs(node); } root.next.sort((a, b) -> a.mex - b.mex); root.mex = 0; for (Node node : root.next) { if (node.mex > root.mex) { break; } if (node.mex == root.mex) { root.mex++; } } } } public static class FastIO { public final StringBuilder cache = new StringBuilder(); private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } public FastIO(InputStream is, OutputStream os) { this(is, os, Charset.forName("ascii")); } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { boolean sign = true; skipBlank(); if (next == '+' || next == '-') { sign = next == '+'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } if (next != '.') { return sign ? val : -val; } next = read(); long radix = 1; long point = 0; while (next >= '0' && next <= '9') { point = point * 10 + next - '0'; radix = radix * 10; next = read(); } double result = val + (double) point / radix; return sign ? result : -result; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readLine(char[] data, int offset) { int originalOffset = offset; while (next != -1 && next != '\n') { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Debug { private boolean allowDebug; public Debug(boolean allowDebug) { this.allowDebug = allowDebug; } public void assertTrue(boolean flag) { if (!allowDebug) { return; } if (!flag) { fail(); } } public void fail() { throw new RuntimeException(); } public void assertFalse(boolean flag) { if (!allowDebug) { return; } if (flag) { fail(); } } private void outputName(String name) { System.out.print(name + " = "); } public void debug(String name, int x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, long x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, double x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, int[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, long[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, double[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, Object x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, Object... x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.deepToString(x)); } } public static class Node { boolean visited; int mex; int tax; List<Node> next = new ArrayList<>(); } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, m, h[N], q[N], d[N], s[N], vis[N], sg[N]; vector<int> G[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1, x, y; i <= m; i++) scanf("%d%d", &x, &y), G[x].push_back(y), d[y]++; int qs = 1, qe = 1; for (int i = 1; i <= n; i++) if (!d[i]) q[qe++] = i; while (qs < qe) { int u = q[qs++]; for (int i = 0; i < G[u].size(); i++) if (!--d[G[u][i]]) q[qe++] = G[u][i]; } for (int i = n; i; i--) { int u = q[i]; for (int j = 0; j < G[u].size(); j++) vis[sg[G[u][j]]] = i; while (vis[sg[u]] == i) sg[u]++; s[sg[u]] ^= h[u]; } for (int i = n, u; ~i; i--) if (s[i]) { for (int j = 1; j <= n; j++) if (sg[j] == i && h[j] > (s[i] ^ h[j])) u = j; h[u] ^= s[i]; for (int j = 0; j < G[u].size(); j++) h[G[u][j]] ^= s[sg[G[u][j]]], s[sg[G[u][j]]] = 0; puts("WIN"); for (int j = 1; j <= n; j++) printf("%d ", h[j]); puts(""); return 0; } puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline int fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline long long ty() { long long a = 0; int b = 1, c = fetch(); while (!isdigit(c)) b ^= c == '-', c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::ty; const int _ = 200007; vector<int> e[_]; int n, m, sg[_], mxsg = 0; long long val[_] = {0}, sval[_] = {0}; void dfs(int x) { if (sg[x] >= 0) return; unordered_set<int> got; for (auto b : e[x]) dfs(b), got.emplace(sg[b]); sg[x] = 0; while (got.count(sg[x])) sg[x]++; mxsg = max(mxsg, sg[x]), sval[sg[x]] ^= val[x]; } int main() { ios::sync_with_stdio(0), cout.tie(nullptr); n = ty(), m = ty(); for (int i = 1; i <= n; i++) val[i] = ty(); for (int i = 1, a, b; i <= m; i++) a = ty(), b = ty(), e[a].emplace_back(b); memset(sg, -1, sizeof(sg)); for (int i = 1; i <= n; i++) dfs(i); for (int i = mxsg; i >= 0; i--) { if (!sval[i]) continue; cout << "WIN" << lf; for (int j = 1; j <= n; j++) { if (sg[j] == i && (val[j] ^ sval[i]) < val[j]) { val[j] ^= sval[i], sval[i] = 0; for (auto b : e[j]) if (sval[sg[b]]) val[b] ^= sval[sg[b]], sval[sg[b]] = 0; break; } } for (int j = 1; j <= n; j++) cout << val[j] << " \n"[j == n]; return 0; } cout << "LOSE" << lf; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 50; int n, m, h[N], f[N]; vector<int> G[N]; int d[N], S[N]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), ++d[v], G[u].push_back(v); static int q[N]; int hd = 1, tl = 0; for (int i = 1; i <= n; i++) if (d[i] == 0) q[++tl] = i; while (hd <= tl) { int x = q[hd++]; for (int v : G[x]) if (--d[v] == 0) q[++tl] = v; } for (int i = tl, x; i; i--) { x = q[i]; static int ex[N], tim; ++tim; for (int v : G[x]) ex[f[v]] = tim; for (int j = 0;; j++) if (ex[j] != tim) { f[x] = j; break; } S[f[x]] ^= h[x]; } bool ok = true; for (int i = 0; i <= n; i++) if (S[i]) ok = false; if (ok) return puts("LOSE"), 0; for (int i = n; ~i; i--) if (S[i]) { int x = 0; for (int j = 1; j <= n; j++) if (f[j] == i && (h[j] ^ S[i]) < h[j]) { x = j; break; } h[x] = h[x] ^ S[i], S[i] = 0; for (int v : G[x]) if (S[f[v]]) h[v] ^= S[f[v]], S[f[v]] = 0; } puts("WIN"); for (int i = 1; i <= n; i++) cout << h[i] << " "; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; vector<int> dp; vector<vector<int> > adj; int f(int u) { if (dp[u] != -1) return dp[u]; vector<int> ch; for (int j = 0; j < (int)adj[u].size(); j++) { int v = adj[u][j]; ch.push_back(f(v)); } sort(ch.begin(), ch.end()); int re = 0; for (int i = 0; i < (int)ch.size(); i++) { if (ch[i] == re) re++; else if (ch[i] > re) break; } dp[u] = re; return dp[u]; } int main() { int n, m; cin >> n >> m; vector<int> h(n, 0); for (int i = 0; i < n; i++) scanf("%d", &h[i]); adj.assign(n, vector<int>()); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; adj[x].push_back(y); } dp.assign(n, -1); int ans = 0; int sz = 0; for (int i = 0; i < n; i++) sz = max(sz, f(i) + 1); vector<int> tp(sz, 0); for (int i = 0; i < n; i++) tp[f(i)] ^= h[i]; for (int i = 0; i < sz; i++) ans |= tp[i]; if (ans == 0) { cout << "LOSE" << endl; return 0; } int p = -1; for (int i = 0; i < sz; i++) { if (tp[i]) p = i; } for (int i = 0; i < n; i++) if (f(i) == p && (h[i] ^ tp[p]) < h[i]) { h[i] ^= tp[p]; tp[p] = 0; for (int j = 0; j < (int)adj[i].size(); j++) { int v = adj[i][j]; h[v] ^= tp[f(v)]; tp[f(v)] = 0; } break; } cout << "WIN" << endl; for (int i = 0; i < n; i++) cout << h[i] << " "; cout << endl; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> G[maxn]; int sg[maxn], deg[maxn], Xor[maxn], topo[maxn], mem[maxn], cnt; long long h[maxn], XOR[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%lld", &h[i]); for (int i = 0; i < m; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); ++deg[v]; } queue<int> q; for (int i = 1; i <= n; ++i) if (deg[i] == 0) q.push(i); while (!q.empty()) { int t = q.front(); q.pop(); topo[++cnt] = t; for (int i = 0; i < G[t].size(); ++i) { int c = G[t][i]; --deg[c]; if (!deg[c]) q.push(c); } } for (int i = n; i >= 1; --i) { int u = topo[i]; for (int j = 0; j < G[u].size(); ++j) { int c = G[u][j]; mem[sg[c]] = i; } while (mem[sg[u]] == i) ++sg[u]; XOR[sg[u]] ^= h[u]; } long long sghigh = -1; for (int i = 0; i < maxn; ++i) if (XOR[i] > 0) sghigh = i; if (sghigh == -1) return 0 * puts("LOSE"); for (int i = 1; i <= n; ++i) if (sg[i] == sghigh && (XOR[sghigh] ^ h[i]) < h[i]) { h[i] ^= XOR[sghigh]; for (int j = 0; j < G[i].size(); ++j) { int c = G[i][j]; h[c] ^= XOR[sg[c]]; XOR[sg[c]] = 0; } break; } printf("WIN\n"); for (int i = 1; i <= n; ++i) printf("%lld ", h[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(200001); int c[200001], mex[200001], h[200001], b[200001]; void DFS(int i) { if (mex[i] >= 0) { return; } for (int j : adj[i]) { DFS(j); } for (int j : adj[i]) { c[mex[j]] = i; } int &k = mex[i]; for (k = 0; c[k] == i; ++k) ; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); } while (m--) { int i, j; scanf("%d%d", &i, &j); adj[i].push_back(j); } for (int i = 1; i <= n; ++i) { mex[i] = -1; } for (int i = 1; i <= n; ++i) { DFS(i); } for (int i = 1; i <= n; ++i) { b[mex[i]] ^= h[i]; } int root = 0; mex[root] = -1; for (int i = 1; i <= n; ++i) { if (mex[i] > mex[root] && ((b[mex[i]] ^ h[i]) < h[i])) { root = i; } } bool win = false; for (int i = 0; i <= mex[root] && !win; ++i) { win = (b[i] != 0); } if (!win) { printf("LOSE\n"); return 0; } c[mex[root]] = root; for (int j : adj[root]) { c[mex[j]] = j; } for (int i = 0; i <= mex[root]; ++i) { int j = c[i]; h[j] ^= b[i]; } printf("WIN\n"); for (int i = 1; i <= n; ++i) { printf("%d ", h[i]); } printf("\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, num[200100], sy[200100], ds[200100], bj[200100], sum[200100]; vector<long long> to[200100], pre[200100]; queue<long long> que; int main() { long long i, j, t, p, q; cin >> n >> m; for (i = 1; i <= n; i++) scanf("%lld", &num[i]); for (i = 1; i <= m; i++) { scanf("%lld%lld", &p, &q); pre[q].push_back(p); to[p].push_back(q); ds[p]++; } for (i = 1; i <= n; i++) if (!ds[i]) que.push(i); for (; !que.empty();) { q = que.front(); que.pop(); for (i = 0; i < pre[q].size(); i++) { t = pre[q][i]; ds[t]--; if (!ds[t]) que.push(t); } for (i = 0; i < to[q].size(); i++) { t = to[q][i]; bj[sy[t]] = q; } for (i = 0; bj[i] == q; i++) ; sy[q] = i; sum[sy[q]] ^= num[q]; } for (i = n; i >= 0; i--) { if (!sum[i]) continue; puts("WIN"); for (j = 1; j <= n; j++) if (sy[j] == i && (num[j] ^ sum[i]) < num[j]) break; q = j, num[q] ^= sum[i]; for (j = 0; j < to[q].size(); j++) { t = to[q][j]; if (sum[sy[t]] && bj[sy[t]] != -1) { bj[sy[t]] = -1; num[t] ^= sum[sy[t]]; } } for (i = 1; i <= n; i++) printf("%lld ", num[i]); return 0; } puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> V[maxn], H[maxn]; int n, m; long long a[maxn]; int SG[maxn]; int h[maxn]; int maxx = 0; int dfs(int u) { if (SG[u] != -1) return SG[u]; vector<int> tmp; for (auto v : V[u]) { tmp.push_back(dfs(v)); } sort(tmp.begin(), tmp.end()); int j = 0; for (int i = 0;; i++) { if (tmp.size() == 0 || tmp[j] != i || j >= tmp.size()) { SG[u] = i; break; } while (j < tmp.size() && tmp[j] == i) j++; } maxx = max(maxx, SG[u]); return SG[u]; } int c[maxn]; void solve(int x) { for (auto i : H[x]) { if ((a[i] ^ h[x]) < a[i]) { a[i] = a[i] ^ h[x]; for (auto j : V[i]) { if (c[SG[j]]) continue; c[SG[j]] = 1; a[j] ^= h[SG[j]]; } break; } } } int main() { memset(SG, -1, sizeof(SG)); cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); V[u].push_back(v); } for (int i = 1; i <= n; i++) { if (SG[i] == -1) dfs(i); } for (int i = 1; i <= n; i++) { h[SG[i]] ^= a[i]; H[SG[i]].push_back(i); } int p; int flag = 0; for (int i = maxx; i >= 0; i--) { if (h[i]) { flag = 1; p = i; solve(i); break; } } if (flag) { puts("WIN"); for (int i = 1; i <= n; i++) cout << a[i] << " "; } else puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, h[200205]; int info[200205], Prev[200205], to[200205], cnt_e; void Node(int u, int v) { Prev[++cnt_e] = info[u], info[u] = cnt_e, to[cnt_e] = v; } int in[200205], Q[200205], L, R, SG[200205], S[200205]; int vis[200205], tim, Mx; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), Node(u, v), in[v]++; for (int i = 1; i <= n; i++) if (!in[i]) Q[R++] = i; for (int now; L < R;) { now = Q[L++]; for (int i = info[now]; i; i = Prev[i]) { in[to[i]]--; if (!in[to[i]]) Q[R++] = to[i]; } } for (int i = R - 1; i >= 0; i--) { tim++; int u = Q[i]; for (int j = info[u]; j; j = Prev[j]) vis[SG[to[j]]] = tim; for (SG[u] = 0; vis[SG[u]] == tim; SG[u]++) ; S[SG[u]] ^= h[u]; } bool flg = 0; for (int i = 0; i <= n; i++) if (S[i] != 0) flg = 1; if (!flg) puts("LOSE"); else { puts("WIN"); int loc = 0, Maxloc = 0; for (int i = n; i >= 0; i--) if (S[i] != 0) { loc = i; break; } for (int i = 1; i <= n; i++) if (SG[i] == loc && (S[loc] ^ h[i]) < h[i]) Maxloc = i; h[Maxloc] ^= S[loc]; for (int i = info[Maxloc]; i; i = Prev[i]) h[to[i]] ^= S[SG[to[i]]], S[SG[to[i]]] = 0; for (int i = 1; i <= n; i++) printf("%d%c", h[i], i == n ? '\n' : ' '); } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int h[200000]; vector<int> adjList[200000]; int nim[200000]; int doDFS(int u) { if (nim[u] != -1) return 0; int i, j = 0; vector<int> s; for (i = 0; i < adjList[u].size(); i++) { int v = adjList[u][i]; doDFS(v), s.push_back(nim[v]); } sort(s.begin(), s.end()); for (i = 0; i <= adjList[u].size(); i++) { while ((j < s.size()) && (s[j] < i)) j++; if ((j == s.size()) || (s[j] > i)) break; } nim[u] = i; return 0; } int x[200000], y[200000]; int main() { int i; int n, m, u, v; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &h[i]); for (i = 0; i < m; i++) { scanf("%d %d", &u, &v); adjList[u - 1].push_back(v - 1); } int c = 0; fill(nim, nim + n, -1); for (i = 0; i < n; i++) doDFS(i), x[nim[i]] ^= h[i]; for (i = 0; i < n; i++) { if (x[i] > 0) c++; } if (c == 0) { printf("LOSE\n"); return 0; } int j; for (i = 0; i < n; i++) { if ((x[nim[i]] ^ h[i]) < h[i]) { int c2 = 1; for (j = 0; j < adjList[i].size(); j++) { int v = adjList[i][j]; if ((x[nim[v]] > 0) && !y[nim[v]]) c2++; y[nim[v]] = 1; } if (c == c2) { h[i] ^= x[nim[i]]; for (j = 0; j < adjList[i].size(); j++) { int v = adjList[i][j]; h[v] ^= x[nim[v]], x[nim[v]] = 0; } printf("WIN\n"); for (j = 0; j < n; j++) printf("%d%c", h[j], (j == (n - 1)) ? '\n' : ' '); break; } else { for (j = 0; j < adjList[i].size(); j++) y[nim[adjList[i][j]]] = 0; } } } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2.1e5; int N, M; uint64_t H[MAXN]; int outDeg[MAXN]; vector<int> outEdges[MAXN]; vector<int> inEdges[MAXN]; int omegaPow[MAXN]; uint64_t totalNimber[MAXN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N >> M; for (int i = 0; i < N; i++) cin >> H[i]; for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--, v--; inEdges[v].push_back(u); outEdges[u].push_back(v); outDeg[u]++; } stack<int> q; for (int i = 0; i < N; i++) { if (outDeg[i] == 0) q.push(i); } while (!q.empty()) { int cur = q.top(); q.pop(); assert(outDeg[cur] == 0); set<int> nxtOmegas; for (int nxt : outEdges[cur]) { nxtOmegas.insert(omegaPow[nxt]); } omegaPow[cur] = 0; while (nxtOmegas.count(omegaPow[cur])) omegaPow[cur]++; totalNimber[omegaPow[cur]] ^= H[cur]; for (int prv : inEdges[cur]) { if (--outDeg[prv] == 0) { q.push(prv); } } } bool isZero = true; for (int i = 0; i <= N; i++) { if (totalNimber[i]) { isZero = false; } } if (isZero) { cout << "LOSE" << '\n'; exit(0); } else { cout << "WIN" << '\n'; int biggestPow = N + 1; while (biggestPow >= 0 && totalNimber[biggestPow] == 0) biggestPow--; assert(biggestPow != -1); int cur; for (cur = 0; cur < N; cur++) { if (omegaPow[cur] == biggestPow && (totalNimber[omegaPow[cur]] ^ H[cur]) < H[cur]) { break; } } assert(cur != N); vector<int> nxtOmegas(omegaPow[cur] + 1, -1); nxtOmegas[omegaPow[cur]] = cur; for (int nxt : outEdges[cur]) { assert(omegaPow[nxt] != omegaPow[cur]); if (omegaPow[nxt] < omegaPow[cur]) { nxtOmegas[omegaPow[nxt]] = nxt; } } for (int p = 0; p <= omegaPow[cur]; p++) { H[nxtOmegas[p]] ^= totalNimber[p]; totalNimber[p] = 0; } for (int i = 0; i < N; i++) { cout << H[i] << " \n"[i + 1 == N]; } } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int M = 2e5 + 5; int n, m; long long h[N]; long long ans[N]; struct Edge { int num; int next; } edge[M]; int tot, last[N]; int du[N]; int sg[N]; queue<int> L; int node[N], cnt; int mem[N]; void Add(int i, int j) { tot++; edge[tot].num = j; edge[tot].next = last[i]; last[i] = tot; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lld", &h[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); Add(u, v); du[v]++; } for (int i = 1; i <= n; i++) if (du[i] == 0) { L.push(i); node[++cnt] = i; } while (!L.empty()) { int u = L.front(); L.pop(); for (int k = last[u]; k != 0; k = edge[k].next) { int v = edge[k].num; du[v]--; if (du[v] == 0) { L.push(v); node[++cnt] = v; } } } for (int i = n; i >= 1; i--) { int u = node[i]; for (int k = last[u]; k != 0; k = edge[k].next) { int v = edge[k].num; mem[sg[v]] = i; } while (mem[sg[u]] == i) ++sg[u]; } for (int i = 1; i <= n; i++) ans[sg[i]] ^= h[i]; for (int i = n; i >= 0; i--) if (ans[i] != 0) { printf("WIN\n"); for (int j = 1; j <= n; j++) { if (sg[j] != i) continue; if ((h[j] ^ ans[i]) >= h[j]) continue; h[j] = h[j] ^ ans[i]; for (int k = last[j]; k != 0; k = edge[k].next) { int v = edge[k].num; h[v] ^= ans[sg[v]]; ans[sg[v]] = 0; } break; } for (int j = 1; j <= n; j++) printf("%lld ", h[j]); return 0; } printf("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; using ll = long long; int n, m, top; vector<int> v[N]; ll a[N], val[N]; int deg[N], p[N], vis[N], id[N], mx; queue<int> q; void toposort() { for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (!q.empty()) { int nd = q.front(); q.pop(); p[++top] = nd; for (auto &i : v[nd]) if (!--deg[i]) q.push(i); } for (int i = n; i > 0; --i) { int x = p[i]; for (auto &j : v[x]) vis[id[j]] = 1; while (vis[id[x]]) ++id[x]; val[id[x]] ^= a[x]; mx = max(mx, id[x]); for (auto &j : v[x]) vis[id[j]] = 0; } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) { int q, w; cin >> q >> w; v[q].push_back(w); ++deg[w]; } toposort(); int pos = -1; for (int i = 0; i <= mx; i++) if (val[i]) pos = i; if (pos == -1) { puts("LOSE"); return 0; } cout << "WIN" << endl; for (int i = 1; i <= n; i++) if (id[i] == pos) { if ((val[id[i]] ^ a[i]) > a[i]) continue; a[i] ^= val[id[i]]; val[id[i]] = 0; for (auto &j : v[i]) a[j] ^= val[id[j]], val[id[j]] = 0; } for (int i = 1; i <= n; i++) printf("%lld ", a[i]); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, m, h[maxn], u, v, d[maxn], sg[maxn], vis[maxn], chc[maxn], hed[maxn], ed, s[maxn], cnt; struct edge { int to, nex; edge(int _ = 0, int __ = 0) : to(_), nex(__) {} } e[maxn]; inline void add(int u, int v) { e[++ed] = edge(v, hed[u]); hed[u] = ed; } void topsort() { queue<int> Q; for (int i = (1); i <= (n); ++i) if (!d[i]) Q.push(i); while (!Q.empty()) { int u = Q.front(); Q.pop(); s[++cnt] = u; for (int i = hed[u], v = e[i].to; i; i = e[i].nex, v = e[i].to) if (!(--d[v])) Q.push(v); } } int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (n); ++i) scanf("%d", &h[i]); for (int i = (1); i <= (m); ++i) scanf("%d%d", &u, &v), add(u, v), ++d[v]; topsort(); for (int w = (n); w >= (1); --w) { int u = s[w]; for (int i = hed[u], v = e[i].to; i; i = e[i].nex, v = e[i].to) vis[sg[v]] = u; sg[u] = 0; while (vis[sg[u]] == u) ++sg[u]; chc[sg[u]] ^= h[u]; } for (int i = (n); i >= (0); --i) if (chc[i]) { puts("WIN"); int u = 0; for (int j = (1); j <= (n); ++j) if (sg[j] == i && (h[j] > (h[j] ^ chc[i]))) { u = j, h[j] ^= chc[i], chc[i] = 0; break; } for (int j = hed[u], v = e[j].to; j; j = e[j].nex, v = e[j].to) h[v] ^= chc[sg[v]], chc[sg[v]] = 0; for (int j = (1); j <= (n); ++j) printf("%d ", h[j]); puts(""); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxN = 200002; int mex(vector<int>& cands) { static bool flags[maxN]; for (int i = 0; i <= cands.size(); ++i) { flags[i] = false; } for (int x : cands) { flags[x] = true; } int res = 0; while (flags[res]) { ++res; } return res; } static bool used[maxN]; static int level[maxN]; static int xorSum[maxN]; static vector<int> edge[maxN]; static int h[maxN]; static int maxLevel = 0; void dfs(int u) { if (used[u]) { return; } used[u] = true; vector<int> cands; for (int v : edge[u]) { dfs(v); cands.push_back(level[v]); } level[u] = mex(cands); } int main(int argc, char** argv) { std::ios::sync_with_stdio(false); for (int i = 0; i < maxN; ++i) { used[i] = false; level[i] = 0; xorSum[i] = 0; } int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> h[i]; } for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; edge[--x].push_back(--y); } for (int i = 0; i < n; ++i) { dfs(i); } for (int i = 0; i < n; ++i) { xorSum[level[i]] ^= h[i]; } int maxLevel = n - 1; while (maxLevel >= 0 && xorSum[maxLevel] == 0) { --maxLevel; } if (maxLevel < 0) { cout << "LOSE" << endl; return 0; } int u = 0; while (level[u] != maxLevel || h[u] < (xorSum[maxLevel] ^ h[u])) { ++u; } h[u] = xorSum[maxLevel] ^ h[u]; xorSum[maxLevel] = 0; for (int v : edge[u]) { if (xorSum[level[v]] != 0) { h[v] ^= xorSum[level[v]]; xorSum[level[v]] = 0; } } cout << "WIN" << endl; for (int i = 0; i < n; ++i) { cout << h[i] << ' '; } cout << endl; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m; int h[maxn], in[maxn], q[maxn], sg[maxn], sum[maxn], vis[maxn]; vector<int> G[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &h[i]); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); ++in[v]; } int l = 1, r = 0; for (int i = 1; i <= n; ++i) if (!in[i]) q[++r] = i; while (l <= r) { int u = q[l++]; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (!--in[v]) q[++r] = v; } } for (int i = n; i; --i) { int u = q[i]; for (int j = 0; j < G[u].size(); ++j) { int v = G[u][j]; vis[sg[v]] = 1; } for (; vis[sg[u]]; ++sg[u]) ; for (int j = 0; j < G[u].size(); ++j) { int v = G[u][j]; vis[sg[v]] = 0; } } for (int i = 1; i <= n; ++i) sum[sg[i]] ^= h[i]; int p = -1; for (int i = n; ~i; --i) if (sum[i]) { p = i; break; } if (p == -1) { puts("LOSE"); return 0; } puts("WIN"); for (int i = 1; i <= n; ++i) if (sg[i] == p) { if ((sum[p] ^ h[i]) > h[i]) continue; h[i] ^= sum[p]; sum[p] = 0; for (int j = 0; j < G[i].size(); ++j) { int v = G[i][j]; h[v] ^= sum[sg[v]]; sum[sg[v]] = 0; } } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); return 0; }