hdu3197 Game-------树的删边

这篇博客主要探讨了HDU3197游戏问题,涉及到树的结构以及如何进行边的删除操作。通过深入分析每个节点和边的关系,理解游戏过程中的路径变化,揭示了解题的关键思路。
摘要由CSDN通过智能技术生成

Game

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


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

Author
ZSTU

Source

Recommend
lcy
 
#include<iostream>
#include<vector>
#include<stack>
#include<stdio.h>
using namespace std;
vector<int> V[1010];
stack<int> S;
int N;
int dfs(int v,int pre)   //树上博弈
{
    int i,t;
    int sum=0;
    for(i=0;i<V[v].size();i++)
    {
        t=V[v][i];
        if(t!=pre)
        sum=sum^(dfs(t,v)+1);

    }
    return sum;
}

int main()
{
    int i,flag,t;
    while(scanf("%d",&N)!=EOF)
    {
        for(i=0;i<=N;i++)
            V[i].clear();
        while(!S.empty())
            S.pop();
        for(i=0;i<N;i++)
        {
            scanf("%d",&t);
            if(t!=-1)
            {
                V[t].push_back(i);
                V[i].push_back(t);
            }
            else
            {
                S.push(i);
            }
        }
        int t,sum=0;
        while(!S.empty())   //k棵树
        {
            t=S.top();
            S.pop();
            sum=sum^dfs(t,-1);
        }
        if(sum)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值