Ultraviolet 11927-games are important (combined games + Memory)

Source: Internet
Author: User

Link to the question: Ultraviolet A 11927-games are important

A directed graph without loops is given, and the number of stones on each node is given. You can select a stone for each operation and move it to the next node. The two work in turn until they fail.

Solution: with the help of graphs, the SG value of each node is processed in a memory mode, and the NIM sum of all nodes with odd stones is obtained.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1005;int N, M, g[maxn][maxn], c[maxn], s[maxn];inline int SG(int u) {    if (s[u] != -1)        return s[u];    int vis[maxn];    memset(vis, 0, sizeof(vis));    for (int i = 0; i < c[u]; i++) {        int tmp = SG(g[u][i]);        vis[tmp] = 1;    }    int ret = -1;    while (vis[++ret]);    return s[u] = ret;}void init () {    int u, v;    memset(s, -1, sizeof(s));    memset(c, 0, sizeof(c));    for (int i = 0; i < M; i++) {        scanf("%d%d", &u, &v);        g[u][c[u]++] = v;    }    for (int i = 0; i < N; i++)        s[i] = SG(i);}int main () {    while (scanf("%d%d", &N, &M) == 2 && N + M) {        init();        int ans = 0, u;        for (int i = 0; i < N; i++) {            scanf("%d", &u);            if (u&1)                ans ^= s[i];        }        printf("%s\n", ans ? "First" : "Second");    }    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.