Antonidas

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1066    Accepted Submission(s): 303

Problem Description
Given a tree with N vertices and N−1 edges. Each vertex has a single letter Ci. Given a string S, you are to choose two vertices A and B, and make sure the letters catenated on the shortest path from A to B is exactly S. Now, would you mind telling me whether the path exists?
 
Input
The first line is an integer T, the number of test cases.
For each case, the first line is an integer N. Following N−1 lines contains two integers a and b, meaning there is an edge connect vertex a and vertex b.
Next line contains a string C, the length of C is exactly N. String C represents the letter on each vertex.
Next line contains a string S.
1≤T≤200, 1≤N≤104, 1≤a,b≤N, a≠b, |C|=N, 1≤|S|≤104. String C and S both only contain lower case letters.
 
Output
First, please output "Case #k: ", k is the number of test case. See sample output for more detail.
If the path exists, please output “Find”. Otherwise, please output “Impossible”.
 
Sample Input
2
7
1 2
2 3
2 4
1 5
5 6
6 7
abcdefg
dbaefg
5
1 2
2 3
2 4
4 5
abcxy
yxbac
 
Sample Output
Case #1: Find
Case #2: Impossible
 
思路:从树上结点的值为字符串开始字母的结点开始遍历。分从儿子结点和父结点两个方向遍历下一个结点。遍历子结点时加一个剪枝,即向下的延伸长度不不小于剩下字母的长度。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=;
vector<int> arc[MAXN];
int n;
int par[MAXN];
int dist[MAXN];
char value[MAXN],s[MAXN];
int len,vis[MAXN];
void dfs1(int u,int fa)
{
par[u]=fa;
int mx=;
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(to!=fa)
{
dfs1(to,u);
mx=max(dist[to],mx);
}
}
dist[u]=mx+;
}
bool dfs2(int u,int net)
{
if(net==len) return true;
vis[u]=;
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(!vis[to]&&par[u]!=to&&value[to]==s[net]&&(len-net)<=dist[to])
{
if(dfs2(to,net+))
{
return true;
}
}
}
int fa=par[u];
if(!vis[fa]&&value[fa]==s[net])
{
if(dfs2(fa,net+))
{
return true;
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) arc[i].clear();
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
arc[u].push_back(v);
arc[v].push_back(u);
}
scanf("%s",value+);
scanf("%s",s);
dfs1(,);
len=strlen(s);
bool tag=false;
for(int i=;i<=n;i++)
{
if(value[i]==s[])
{
memset(vis,,sizeof(vis));
if(dfs2(i,))
{
tag=true;
break;
}
}
}
printf("Case #%d: ",cas);
if(tag) printf("Find\n");
else printf("Impossible\n");
}
return ;
}

HDU5469(树的dfs)的更多相关文章

  1. CodeForces 343D 线段树维护dfs序

    给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...

  2. [2]树的DFS序

    定义: 树的DFS序就是在对树进行DFS的时候,对树的节点进行重新编号:DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题. 代码: void ...

  3. 树的dfs序 && 系统栈 && c++ rope

    利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...

  4. CF877E Danil and a Part-time Job 线段树维护dfs序

    \(\color{#0066ff}{题目描述}\) 有一棵 n 个点的树,根结点为 1 号点,每个点的权值都是 1 或 0 共有 m 次操作,操作分为两种 get 询问一个点 x 的子树里有多少个 1 ...

  5. HDU4117 GRE WORDS(AC自动机+线段树维护fail树的dfs序)

    Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...

  6. Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)

    D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...

  7. Weak Pair---hud5877大连网选(线段树优化+dfs)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877  题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...

  8. bzoj 3551 [ONTAK2010]Peaks加强版(kruskal,主席树,dfs序)

    Description [题目描述]同3545 Input 第一行三个数N,M,Q. 第二行N个数,第i个数为h_i 接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径. 接下来 ...

  9. 镜像树(dfs)

    1214: J.镜像树 时间限制: 1 Sec  内存限制: 64 MB提交: 18  解决: 7 标签提交统计讨论版 题目描述 一棵二叉树,若其与自己的镜像完全相同,就称其为镜像树(即这棵二叉树关于 ...

随机推荐

  1. time_t、pthread_t

    1.time_t实际上就是长整型long int;用来保存从1970年1月1日0时0分0秒到现在时刻的秒数!用time()这个函数获取! #ifndef __TIME_T #define __TIME ...

  2. 通过join方法顺序执行多个线程

    方法一:直接用多线程之间的通讯去解决 package com.toov5.test; import javax.imageio.ImageTypeSpecifier; class Res1{ char ...

  3. Qt QTreeWidget节点的添加+双击响应+删除详解

    转自: http://www.cnblogs.com/Romi/archive/2012/08/08/2628163.html 承接该文http://www.cnblogs.com/Romi/arch ...

  4. Flume-NG启动过程源码分析(二)(原创)

    在上一节中讲解了——Flume-NG启动过程源码分析(一)(原创)  本节分析配置文件的解析,即PollingPropertiesFileConfigurationProvider.FileWatch ...

  5. ANT+JMETER集成2 (发送邮件)

    折腾一天发现各种build源码都不能发送邮件,试了很多次,终于能发送邮件 先看成果 build源码贴出来 <?xml version="1.0" encoding=" ...

  6. phpPgAdmin (win)配置安装及远程访问

    phpPgAdmin (win)配置安装 [1]     通过PostgreSQL的Application Stack Builder配置安装phpPgAdmin 1.确保PostgreSQL安装并正 ...

  7. JDBC方式操作数据库

    1.读取配置文件,我将配置信息写入配置文件(.properties)中,方便后期数据库变更后或者其他项目使用,扩展性好些,尽量避免硬编码. driver=oracle.jdbc.driver.Orac ...

  8. Hbase- Hbase客户端读写数据时的路由流程

    1.客户端先到zookeeper查找hbase:meta所在的RegionServer服务器 2.去hbase:meta表查找自己所要的数据所在的region server 3.去目标region s ...

  9. ubuntu更改启动顺序

    在ubuntu中修改启动配置. 启动相关grub2主要包含下面三个文件:1.   /boot/grub/grub.cfg 文件    2.   /etc/grub.d/ 文件夹   3.   /etc ...

  10. 如何在阿里云上部署war包到tomcat服务器

    一. 准备工作:xshell和xftp 首先我们得确保,xshell能够远程连接阿里云ECS,xftp能够保证windows和linux之间的文件传输(当然也可以选择FileZilla,但xftp感觉 ...