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. 老司机也该掌握的MySQL优化指南

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,所以我们本文会提供一些优化参考,大家可以参考以下步骤来优化: 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运 ...

  2. C/C++ 字符串操作函数 思维导图梳理

    这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰

  3. MySQL数据库基本操作(一)

    进入mysql 本地连接: mysql -u用户名 -p 输入密码 qwe123 mysql -uroot -pqwe123 sudo apt-get install mysql-server # p ...

  4. 通过公钥解密密文思路(256bits RSA)

    256bit RSA公钥安全系数极低,只需要几分钟即可破解密文,本文综合其他文章记录了一次解密256bits RSA加密的密文的过程,仅作为备忘. 1.分解公钥,分解出n与e: 1.1使用openss ...

  5. 利用CXF框架开发webservice

    开发服务端代码 1. web.xml文件中添加cxf的servlet 2. 定义接口 @WebService(targetNamespace="http://UserInfo.ws.com& ...

  6. linux基础(7)-IO重定向

    符合含义 > (重新生成或清空后添加) $ ls -l >test22.log >>(追加) $ ls -l >>test22.log   实例1 $ find . ...

  7. 3Sum,4Sum问题

    //三数和为0的问题.要求去重,并且输出数字有序.public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(n ...

  8. Python之单例模式总结

    一.单例模式 a.单例模式分为四种:文件,类,基于__new__方法实现单例模式,基于metaclass方式实现 b.类实现如下: class Sigletion(objects): import t ...

  9. Ubuntu16.04 安装wine下的QQ

    下载连接 wine-qqintl http://www.ubuntukylin.com/application/show.php?lang=cn&id=279 安装步骤 安装依赖库 sudo ...

  10. 《Advanced Bash-scripting Guide》学习(十五):测试坏的链接文件(broken link)

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 #/bin/bash #用一个纯粹的shell脚本来找出坏链接文件 #什么是br ...