hdu3197 Game

67 篇文章 1 订阅

Game

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


Problem Description
Flynn and oooccc1 are playing game some day. The game is played on a rooted graph which is an undirected graph with every edge attached by some path to a special vertex called the root or the ground. The ground is denoted in the figure that follows by a dotted line. You could see it from the figure.



On each step, one player could chop out one edge, and removing that edge and anything not connected to the ground. The one who have nothing to chop with fails.
Flynn is the one who is going to move. Help him by telling him if it is possible to win by carefully choose the edge to chop with.

Input
There are a few cases. Process to the end of file
Each case starts with N (1 <= N <= 1000) stand for the number of points.
The next line give N numbers, the i-th (i = 0 … N-1) of which represents its father nodes. The root nodes was the one with value -1.

Output
Print “YES” if could win if play optimally, and “NO” otherwise.

Sample Input
  
  
8 -1 1 2 2 -1 5 6 6

Sample Output
  
  
NO
单棵树在上一篇已说,这里,只要把所有的树异开或和就可以了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define M 100500
vector<int > vec[M];
int vis[M],dp[M],pri[M];
int dfs(int x){
    if(dp[x]!=-1)return dp[x];
    vis[x]=1;
    dp[x]=0;
    for(int i=0;i<vec[x].size();i++){
        if(!vis[vec[x][i]])
        dp[x]^=dfs(vec[x][i])+1;
    }
    return dp[x];
}
int main()
{
    int tcase,n,i,s,e;
    int cnt=0,ans=0,k;
    while(scanf("%d",&n)!=EOF){
        ans=0;
        for(i=0;i<=n;i++)vec[i].clear();
        memset(vis,0,sizeof(vis));
        memset(dp,-1,sizeof(dp));
       for(i=0;i<n;i++){
            scanf("%d",&pri[i]);
            if(pri[i]!=-1){
                vec[pri[i]].push_back(i);
                vec[i].push_back(pri[i]);
            }
        }
        for(i=0;i<n;i++)
        if(pri[i]==-1){
            k=dfs(i);
            ans^=k;
        }
        if(ans)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值