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. 20145230《JAVA程序设计》第1周学习总结

    20145230<JAVA程序设计>第一周学习总结 教材学习内容总结 在第一周的学习中,我初次认识了JAVA程序的一些基础知识.首先,我们需要在网上先下载JDK或者JDE,通过视频的学习, ...

  2. nodejs实现静态托管

    const express = require("express"); const app = express(); /* 语法1: app.use(express.static( ...

  3. INSPIRED启示录 读书笔记 - 第2章 产品管理与产品营销

    两者不是一回事 1.产品经理的工作是从细节上定义开发团队开发什么产品 2.市场营销的职责是对外宣传产品 产品公司常常会陷入的三种误区 1.由市场营销人员定义产品:由产品营销经理或所谓的产品经理负责收集 ...

  4. Ubuntu 12.04下安装OpenCV 2.4.2

    http://sourceforge.net/projects/opencvlibrary/files/ Ubuntu 12.04下安装OpenCV 2.4.2 http://blog.csdn.ne ...

  5. vlan 配置

    在RedHat上配置vlan: ~]$ modinfo 8021q在ethX接口上配置vlan逻辑子接口: DEVICE=ethX. BOOTPROTO=none ONBOOT=yes IPADDR= ...

  6. php如何查看扩展是否开启

    php如何查看扩展是否开启 一.总结 一句话总结:php -m 1.查看php已安装扩展命令 ? php -m 2.phpinfo();这是最常用的方法,但那么多扩展一时还真不太好找.? 3.exte ...

  7. 安装及使用webpack

    Webpack 什么是webpack:现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包.为了简化开发的复杂度,前端社区涌现出了很多好的实践方法:1.模 ...

  8. 论文笔记 — MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching

    论文:https://github.com/ei1994/my_reference_library/tree/master/papers 本文的贡献点如下: 1. 提出了一个新的利用深度网络架构基于p ...

  9. Spring Boot配置文件详解:自定义属性、随机数、多环境配置

    自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: ...

  10. keras中无法下载 https://s3.amazonaws.com/img-datasets/mnist.npz 解决方法

    网址:https://s3.amazonaws.com/img-datasets/mnist.npz,由于显而易见的原因,无法访问. npz实际上是numpy提供的数组存储方式,简单的可看做是一系列n ...