首页 > ACM题库 > HDU-杭电 > HDU 3197-Game-动态规划-[解题报告]HOJ
2014
03-06

HDU 3197-Game-动态规划-[解题报告]HOJ

Game

问题描述 :

  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.

Let’s Ship

  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.

输入:

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.

输出:

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.

样例输入:

8
-1 1 2 2 -1 5 6 6

样例输出:

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;
}

参考:http://blog.csdn.net/mengzhengnan/article/details/12252525


  1. [email protected]

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  3. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。