加载中...

sg函数

在一张有向无环图中,对于每个点 uu,设其所有能到的点的 SG 函数值集合为集合 A,那么 u 的 SG 函数值为 mex(A),记做 SG(u)=mex(A) 集合当中不存在的最小自然数
只有一个棋子 先手必胜=起手所在位置不等于0
多个棋子 先手必胜=所有起点所在位置异或和不等于0

#include <cstdio>
#include <cstring>
#include <set>

using namespace std;

const int N = 2010, M = 6010;

int n, m, k;
int h[N], e[M], ne[M], idx;
int f[N];

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int sg(int u)
{
    if (f[u] != -1) return f[u];//能返回就返回

    set<int> S;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        S.insert(sg(j));//存所有边
    }

    for (int i = 0; ; i ++ )
        if (S.count(i) == 0)
        {
            f[u] = i;
            break;
        }

    return f[u];
}

int main()
{
    scanf("%d%d%d", &n, &m, &k);
    memset(h, -1, sizeof h);

    for (int i = 0; i < m; i ++ )
    {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
    }

    memset(f, -1, sizeof f);

    int res = 0;
    for (int i = 0; i < k; i ++ )
    {
        int u;
        scanf("%d", &u);
        res ^= sg(u);//有向无环图从sg(起点)
    }

    if (res) puts("win");
    else puts("lose");

    return 0;
}


posted @ 2022-08-21 09:35  liang302  阅读(91)  评论(0编辑  收藏  举报