hdu3094—A tree game-(树形博弈)

A tree game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 708    Accepted Submission(s): 385

Problem Description
Alice and Bob want to play an interesting game on a tree.
Given is a tree on N vertices, The vertices are numbered from 1 to N. vertex 1 represents the root. There are N-1 edges. Players alternate in making moves, Alice moves first. A move consists of two steps. In the first step the player selects an edge and removes it from the tree. In the second step he/she removes all the edges that are no longer connected to the root. The player who has no edge to remove loses.
You may assume that both Alice and Bob play optimally.
 

Input
The first line of the input file contains an integer T (T<=100) specifying the number of test cases. 
Each test case begins with a line containing an integer N (1<=N<=10^5), the number of vertices,The following N-1 lines each contain two integers I , J, which means I is connected with J. You can assume that there are no loops in the tree.
 

Output
For each case, output a single line containing the name of the player who will win the game.
 

Sample Input
 
    
3
3
1 2
2 3
1 3
3
1 2
4 3
10
6 2
8 6
8 4
9 5
1 9
2 7
5 8
6 10
 

Sample Output
 
    
Alice
Bob
Alice
思路:num[fa] = num[fa] ^ ( num[son] + 1); num[fa] = 0 (the number of son of the fa is 0)。
 
   
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define REP(i,k,n) for(int i=k;i
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define inf INF*INF
#define mod 1000000007
using namespace std;
#define N 100005
#define M 400005
int num[N];
vectorv[N];
int has[N];
void dfs(int n,int fa) //由于给的边没有确定方向,所以要记录父亲节点
{
   int len = v[n].size();
   for(int i=0;i
      if(v[n][i] == fa) continue;
      dfs(v[n][i],n);
      num[n]^= (num[v[n][i]] + 1);
    //  cout<
   }
}
int main()
{
   int t;
   scan(t);
   while(t--){
      mst(num,0);
      mst(has,0);
      int n;
      scan(n);
      REPP(i,1,n) v[i].clear();
      REP(i,0,n-1){
      int a,b;
      scann(a,b);
      has[b]++;
      has[a]++;
      v[b].push_back(a);
      v[a].push_back(b);
      }
         dfs(1,-1); //1 为 根
         if(num[1]) printf("Alice\n");
         else printf("Bob\n");
      }
}



你可能感兴趣的:(hdu3094—A tree game-(树形博弈))