题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011

题目大意:在一棵树上,给你起始状态,问你能否到达终止状态。

给了树的前序遍历序。

直接dfs搜索。

 #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
#include <ctime>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int n,m,k;
char in[];
vector<PII> v[][];
int tree[];
char ch[]; bool isAccepted(int sig){
return sig>=n-m;
} bool dfs(int rt,int sig){
// printf("rt = %d sig = %d\n",rt,sig); int rtVal = tree[rt]; if( rtVal==- ){
return isAccepted(sig);
}
// printf("rtVal = %d\n",rtVal);
bool res = false;
for(int i=;i<v[sig][rtVal].SZ;i++){
res = dfs(rt<<,v[sig][rtVal][i].AA);
if(!res) continue;
res &= dfs(rt<<|,v[sig][rtVal][i].BB);
if( res ) break;
} return res;
} int main(){
int kase = ;
while( scanf("%d%d%d",&n,&m,&k)!=EOF ){
getchar();
for(int i=;i<;i++) for(int j=;j<;j++) v[i][j].CL;
if( n==&&m==&&k== ) break;
for(int i=;i<n;i++){
for(int j=;j<k;j++){
gets(in);
istringstream sss(in);
int a,b;
while( sss>> a >> b ){
v[i][j].PB(MP(a,b));
// printf("v[%d][%d].PB(%d,%d)\n",i,j,a,b);
}
}
}
// puts("*****");
if( kase!= ) puts("");
printf("NTA%d:\n",kase++);
int q;
while( true ){
memset(tree,-,sizeof(tree));
scanf("%d",&q);
if( q==- ) break;
for(int i=;i<=(<<(q+))-;i++){
scanf("%s",&ch);
if( strcmp(ch,"*")== ){
tree[i] = -;
} else {
tree[i] = ch[]-'a';
}
// printf("%d ",tree[i]);
}//puts(""); bool ok = dfs(,);
if(ok) puts("Valid");
else puts("Invalid");
} }
return ;
}

[ZOJ 1011] NTA (dfs搜索)的更多相关文章

  1. ZOJ 1011 - NTA

    题目大意:有一颗完全二叉树,给节点一个信号会从一个表中选择一对信号分别传递给两个子节点.最后判断所有叶子节点是否满足给定的规则.题目有点长,具体可参见原题. 首先是表格中数据的存储,由于会有多个元素, ...

  2. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  4. hihocoder 1050 树中的最长路(动态规划,dfs搜索)

    hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...

  5. sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  6. 蓝桥杯 历届试题 剪格子(dfs搜索)

    历届试题 剪格子 时间限制:1.0s   内存限制:256.0MB 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |* || +--****--+ ||* | ** ...

  7. DFS搜索题素数环

    素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...

  8. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  9. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

随机推荐

  1. windows中用eclipse开发Android环境搭建SDK安装异常

    SDK更新时的“https://dl-ssl.google.com refused”错误 Download interrupted: hostname in certificate didn't ma ...

  2. web.xml文件中配置<mime-mapping>下载文件类型

    TOMCAT在默认情况下下载.rar的文件是把文件当作text打开,以至于IE打开RAR文件为乱码,如果遇到这种情况时不必认为是浏览器的问题,大多数浏览器应该不会死皮赖脸地把二进制文件当作文本打开,一 ...

  3. 获取在线人数 CNZZ 和 51.la

    string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...

  4. sqlite 下载的 ZIP 包的区别

    https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx20-binary-bundle- ...

  5. SPOJ #2 Prime Generator

    My first idea was Sieve of Eratosthenes, too. But obviously my coding was not optimal and it exceede ...

  6. 使用laravel的Eloquent模型获取数据库的指定列

    使用laravel的Eloquent模型获取数据库的指定列   使用Laravel的ORM——Eloquent时,时常遇到的一个操作是取模型中的其中一些属性,对应的就是在数据库中取表的特定列. 如果使 ...

  7. pgsql 9.4修改数据库只读

    先进入psql 切换到目标数据库 \c mydb 对于老表 grant usage on schema public to $read_only_user; grant select on all t ...

  8. Makefile选项CFLAGS,LDFLAGS,LIBS

    CFLAGS 表示用于 C 编译器的选项, CXXFLAGS 表示用于 C++ 编译器的选项.这两个变量实际上涵盖了编译和汇编两个步骤. CFLAGS: 指定头文件(.h文件)的路径,如:CFLAGS ...

  9. HttpUrlConnection java.net.SocketException: Software caused connection abort: recv failed

    最近做java swing程序在模拟httprequest请求的时候出现了这个错误 java.net.SocketException: Software caused connection abort ...

  10. ios之"performSelector may cause a leak because its selector is unknown"警告原因及其解决办法

    问题描述 项目中使用到了从字符串创建选择器,编译时发现警告:"performSelector may cause a leak because its selector is unknown ...