1. 程式人生 > >移動遊戲By HYJ

移動遊戲By HYJ

down dfs code i++ -m image void can putchar

技術分享圖片

暴力求SG函數即可,記憶化賊方便

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<‘0‘||ch>‘9‘;ch=getchar())  if (ch==‘-‘)    f=-1;
    for (;ch>=‘0‘&&ch<=‘9‘;ch=getchar())    x=(x<<1)+(x<<3)+ch-‘0‘;
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+‘0‘);
}
const int N=1e4,M=1e5;
int pre[M+10],now[N+10],child[M+10],tot;
int can[N+10];
int n,m;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void dfs(int x){
    if (can[x]!=-1) return;
    bool flag=0;
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
        dfs(son);
        if (!can[son])  flag=1;
    }
    can[x]=flag;
}
void init(){
    tot=0;
    memset(now,0,sizeof(now));
    memset(can,-1,sizeof(can));
}
int main(){
    for (int Data=read();Data;Data--){
        init();
        n=read(),m=read(),can[n]=0;
        for (int i=1;i<=m;i++){
            int x=read(),y=read();
            join(x,y);
        }
        dfs(1);
        printf(can[1]?"yes":"no");
        putchar(‘\n‘);
    }
    return 0;
}

移動遊戲By HYJ