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. 20145240 《Java程序设计》第六周学习总结

    20145240 <Java程序设计>第六周学习总结 教材学习内容总结 InputStream与OutputStream 10.1.1串流设计的概念 Java将输入/输出抽象化为串流,数据 ...

  2. iOS应用网络安全之HTTPS

    移动互联网开发中iOS应用的网络安全问题往往被大部分开发者忽略,iOS9和OS X 10.11开始Apple也默认提高了安全配置和要求.本文以iOS平台App开发中对后台数据接口的安全通信进行解析和加 ...

  3. Go 字符串相关-标准库

    标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...

  4. yum 源配置

    在 /etc/yum.repos.d 下建立一个 .repo 文件 vim  yum.repo [cd] name=cd baseurl=file:///run/media/root/RHEL-7.0 ...

  5. 【bzoj1232】[Usaco2008Nov]安慰奶牛cheer(最小生成树)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1232 这道题要保留的道路肯定是原图的一棵生成树,因为要保留n-1条边,且使删边后的图连 ...

  6. ggplot笔记001——ggplot2安装

         R3.2.2版安装ggplot2      今天安装ggplot2,开始用的是R3.2.1版本,但是一直报错.后面换了一个最新的R3.2.2,但安装时还是一样报错,原因是munsell这个包 ...

  7. jQuery计时器插件

    /** * 定义一个jQuery计时插件,实现记录计时开始时间.结束时间,总共耗时的功能 * @param $ * * @author Ivan 2862099249@qq.com * @date 2 ...

  8. Tomcat学习之Wrapper

    Tomcat学习之Wrapper 分类: WEB服务器2012-08-30 22:16 1547人阅读 评论(0) 收藏 举报 tomcatservletwrapperservletslistexce ...

  9. Mirantis OpenStack 7.0: NFVI Deployment Guide — NUMA/CPU pinning

    https://www.mirantis.com/blog/mirantis-openstack-7-0-nfvi-deployment-guide-numacpu-pinning/ Compute ...

  10. 深入剖析Redis主从复制

    [http://sofar.blog.51cto.com/353572/1413024/]   [Redis 主从复制的内部协议和机制]   一.主从概述 Redis 支持 Master-Slave( ...