POJ 2599 A funny game#树形SG(DFS实现)
http://poj.org/problem?id=2599
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; int n,k,pos;
vector<int> g[1005];
bool flag[1005]; int dfs(int k)
{
for(int i=0;i<g[k].size();i++)
{
int t=g[k][i];
if(!flag[t])
{
flag[t]=1;
if(!dfs(t))
{
pos=t;
return 1;//k状态为败,则i状态为胜
}
flag[t]=0;//初始化,进行下一次遍历
}
}
return 0;
} int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i=1;i<=n;i++)
g[i].clear();
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(flag,0,sizeof(flag));
flag[k]=1;
if(dfs(k)) printf("First player wins flying to airport %d\n",pos);
else printf("First player loses\n");
}
return 0;
}
POJ 2599 A funny game#树形SG(DFS实现)的更多相关文章
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 2425 A Chess Game#树形SG
http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...
- poj 3710 Christmas Game【博弈论+SG】
也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- POJ 2023 Choose Your Own Adventure(树形,dfs,简单题)
题意: 输入一个整数n,表示有n组测试数据, 每组第一行输入一个整数x表示该组测试一共有x页,接下来输入x行,每行表示一页, 每页或者以C开头(第一页都是以C开头),或者以E开头,中间用引号括起一段文 ...
- 【POJ 2486】 Apple Tree (树形DP)
Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- POJ 2425 A Chess Game 博弈论 sg函数
http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...
- 树的直径的求法即相关证明【树形DP || DFS】
学习大佬:树的直径求法及证明 树的直径 定义: 一棵树的直径就是这棵树上存在的最长路径. 给定一棵树,树中每条边都有一个权值,树中两点之间的距离定义为连接两点的路径边权之和.树中最远的两个节点之间的距 ...
随机推荐
- 【Python】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- find指令参数
1.name ~ 根目录 . 当前和子目录 name之后跟的是文件名 find . -name "[a-z]*[4-9].log" -print 2.perm perm后面跟的是权 ...
- canvas实现画板功能(渐变、动画、阴影...)
刚刚在博客园落户了,希望能在这认识更多大神,希望能和大家交好朋友. 闲来无事,把以前用canvas写的画板代码改进了一番,用Html5提供的表单标签给其 加了一个选择颜色的功能,因此发现了该标签的一个 ...
- sql 查询表共多少列
1.oracle: select count(*) from user_tab_cols where table_name='表名';--表名含英文的话应为英文大写字母 2.mysql: select ...
- laravel 安装 Laravel 扩展包
问题说明 我们经常要往现有的项目中添加扩展包,有时候因为文档的错误引导,如下图来自这个文档 的: composer update 这个命令在我们现在的逻辑中,可能会对项目造成巨大伤害. 因为 comp ...
- 【转】如何将ACCESS数据库后缀名accdb改为mdb
office 2007中的ACCESS数据库保存时,默认的后缀名是*.accdb, 但是在数据库编程中,用到的后缀名却是*.mdb, 不能直接将后缀名由 accdb 改为 mdb,虽然没有出错,但是编 ...
- UNIX基础--Shells
Shells Shell提供了一个和操作系统交互的命令行接口.shell的主要功能就是从输入取得命令然后去执行.FreeBSD内含了一些shell,包括:Bourne shell(sh). exten ...
- Jsp中out.println()与System.out.println()的区别
第一次上Web实验课时咱写了一个jsp程序: <% System.out.println("Hello The World"); %> 然后放在浏览器下运行,结果是这样 ...
- OOP in JS - Inheritance
Summary You cause a class to inherit using ChildClassName.prototype = new ParentClass();. You need t ...
- 得分(Score, ACM/ICPC Seoul 2005,UVa 1585)
#include<cstdio>#include<cstdlib>#include<cstring>int main(){ char s[80];//输入OOXXO ...