Ultraviolet A 12163-addition-subtraction game (game)

Source: Internet
Author: User
Ultraviolet A 12163-addition-subtraction game

Question Link

Given a directed graph, each node has a ki, and each time the game gives each node A starting value, a position is selected in turn each time, so that it can reach the next node and the value is positive, set the value to-1, select K [I] + 1 at the surrounding node, and then ask who cannot perform the operation or lose. Then, ask the game to win or lose each time.

Idea: first construct the SG function on the graph. Since each node connects up to 15 nodes, You can enumerate which nodes are added with an odd number of times 1, because the even number is equal to no effect, (The first step is to take the second step without any impact), use a State compression, then you can find the SG function based on the state transition, and then each round of the game only needs to consider the NIM and

Code:

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <map>using namespace std;const int MAXS = (1<<15);const int N = 105;int t, n, m, k[N], sg[N], cnt[MAXS];vector<int> g[N];int bitcount(int x) {    int ans = 0;    while (x) {ans += (x&1);x >>= 1;    }    return ans;}int dfs(int u) {    if (sg[u] != -1) return sg[u];    if (g[u].size() == 0) return sg[u] = 0;    for (int i = 0; i < g[u].size(); i++)dfs(g[u][i]);    int maxs = (1<<g[u].size());    map<int, int> vis;    for (int i = 0; i < maxs; i++) {if (cnt[i] > k[u]) continue;if ((cnt[i]^k[u])&1) continue;int x = 0;for (int j = 0; j < g[u].size(); j++) {    if (i&(1<<j))x ^= sg[g[u][j]];}vis[x] = 1;    }    for (int i = 0; ; i++)if (!vis.count(i))    return sg[u] = i;}void getsg() {    memset(sg, -1, sizeof(sg));    for (int i = 0; i < n; i++)dfs(i);}int main() {    for (int i = 0; i < MAXS; i++)cnt[i] = bitcount(i);    int cas = 0;    scanf("%d", &t);    while (t--) {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++)    g[i].clear();int u, v;while (m--) {    scanf("%d%d", &u, &v);    g[u].push_back(v);}for (int i = 0; i < n; i++)    scanf("%d", &k[i]);getsg();scanf("%d", &m);printf("Game#%d:\n", ++cas);int rou = 0;while (m--) {    int sum = 0, x;    for (int i = 0; i < n; i++) {scanf("%d", &x);if (x&1) sum ^= sg[i];    }    printf("Round#%d: %s\n", ++rou, sum == 0 ? "LOSING" : "WINNING");}printf("\n");    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.