ZOJ Problem Set - 3666 Alice and Bob(SG)

来源:互联网 发布:查看图片分辨率软件 编辑:程序博客网 时间:2024/05/21 07:54

Alice and Bob

Time Limit: 2 Seconds      Memory Limit: 65536 KB

"Alice and Bob take turns choosing one of the numbers, and replace it with one of its positive factor but not itself. The one who makes the product of all numbers become 1 wins."

Alice and Bob meet again this week, they played the game as description above last week. But Bob is so stupid that he had to spend lots of time on picking up "one of the number's factor but not itself". So Alice puts up a new game which needn't deal with the number. Here comes the problem.

Alice and Bob take turns choosing one of the toys to move on the map, at the beginning of the game, they have M toys to choose. At each turn, they must choose one toy on the map, and make it move, the toy which the player chooses to move would not stay in the same place as last turn. There are N places on the map, naming 1, 2, 3, ..., N-1, N, the initial places of M toys are among these 1, 2, 3, ..., N-1 places, except the place N. Each of the M toys has a distinct initial place. If two different toys arrive at the same place, the operation is also valid. Finally, the place N is the terminal place. After the toy is moved to the place N, it will be eliminated from the map. Of course the eliminated toys cannot be moved again. And notice that we assume Alice takes the first turn. The one who has no toys to move is the loser. Notice that even there are toys remaining on the map, it has the possibility that the player cannot move any toys forward. The player would lose in this situation. See the Case 2 in the Sample Input section.

The map is assured to be a directed acyclic graph and the map would not change in the game. As the one of the cleverest programmers in the world, you are asked to place the toys on the map (That means that you decide the value of M and the initial places of these M toys). So, you want to know that who will win in your arrangement, assuming both of them are clever enough to play this game. The number of the edges between places is no more than 100000.

Input

There are multiple test cases. Each case begins with an integer N (1 ≤ N ≤ 10000) in one line. In the following N-1 lines, ith(1≤ i ≤N-1) line describe the places where place naming ican arrive. "Ci p1 p2 ... pCi" implies that the number of places i can arrive totally Ci places, p1p2, ..., pCi respectively.

Then input comes with an integer Q in one line. In the fowllowing Q(1≤ Q ≤ 100) lines, it containes an integer M(1≤ M ≤ N-1) and the name of M distinct places, which are the initial places of M toys at the beginning of the game.

There would not be any empty lines between cases.

Output

For each case, the output begins with "Case c:" in one line, where c indicates the case number. Then print exactly one line for each query in each test case. If the winner would be Alice, print "Alice", otherwise print "Bob".

Sample Input

42 2 31 41 431 11 22 1 242 2 31 4021 32 3 1101 21 31 41 51 61 71 81 91 1031 15 1 2 3 4 59 1 2 3 4 5 6 7 8 9

Sample Output

Case 1:BobAliceAliceCase 2:BobAliceCase 3:AliceAliceAlice

//zoj3666

//SG函数:
//对于任意状态,定义SG(x)=mex(S),其中S是x的后继状态的SG函数值集合,mex(S)表示不再S内的最小非负整数
//SG(X)=0当且仅当x为必败态。
//SG(n)由于初度为0,则sg(n)=0;然后深搜
#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>


using namespace std;


const int N = 1e4 + 5;


vector<int> g[N];


bool vis[N];


int sg[N];


void dfs(int u)
{
    vis[u]=1;
    vector<int>num;
    for(int i=0;i<(int)g[u].size();i++)
    {
        int v=g[u][i];
        if(!vis[v])
            dfs(v);
        num.push_back(sg[v]);
    }
    sort(num.begin(),num.end());
    sg[u]=0;
    for(int i=0;i<(int)num.size();i++)
    {
        if(sg[u]==num[i]) sg[u]++;
        else if(sg[u]<num[i]) break;
    }
}


int main( )
{
    freopen("1.txt","r",stdin);
    int n,kase=0;
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<n;i++)
        {
            g[i].clear();
            int num;
            scanf("%d",&num);
            while(num--)
            {
                int v;
                scanf("%d",&v);
                g[i].push_back(v);
            }
        }
        g[n].clear();
        for(int i=1;i<=n;i++)  //由于图不一定是连通图,所以要遍历一遍节点
            if(!vis[i])
                dfs(i);
        int q;
        scanf("%d",&q);
        printf("Case %d:\n",++kase);
        while(q--)
        {
            int num;
            scanf("%d",&num);
            int ans=0;
            while(num--)
            {
                int i;
                scanf("%d",&i);
                ans^=sg[i];
            }
            if(!ans)  printf("Bob\n");
            else  printf("Alice\n");
        }
    }
    return 0;
}
0 0