HDU5469(树的dfs)
Antonidas
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1066 Accepted Submission(s): 303
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.
If the path exists, please output “Find”. Otherwise, please output “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)的更多相关文章
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- [2]树的DFS序
定义: 树的DFS序就是在对树进行DFS的时候,对树的节点进行重新编号:DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题. 代码: void ...
- 树的dfs序 && 系统栈 && c++ rope
利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...
- CF877E Danil and a Part-time Job 线段树维护dfs序
\(\color{#0066ff}{题目描述}\) 有一棵 n 个点的树,根结点为 1 号点,每个点的权值都是 1 或 0 共有 m 次操作,操作分为两种 get 询问一个点 x 的子树里有多少个 1 ...
- HDU4117 GRE WORDS(AC自动机+线段树维护fail树的dfs序)
Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...
- 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 ...
- Weak Pair---hud5877大连网选(线段树优化+dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...
- bzoj 3551 [ONTAK2010]Peaks加强版(kruskal,主席树,dfs序)
Description [题目描述]同3545 Input 第一行三个数N,M,Q. 第二行N个数,第i个数为h_i 接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径. 接下来 ...
- 镜像树(dfs)
1214: J.镜像树 时间限制: 1 Sec 内存限制: 64 MB提交: 18 解决: 7 标签提交统计讨论版 题目描述 一棵二叉树,若其与自己的镜像完全相同,就称其为镜像树(即这棵二叉树关于 ...
随机推荐
- iOS_数据存取(二)
本节内容目录: 一.SQLite3 二.Core Data 一.SQlite3 SQLite3是⼀款开源的嵌入式关系型数据库,可移植性好.易使用.内存开销小SQLite3是⽆类型的,意味着你可以保存任 ...
- Python的文件读写与存储
文件读写与存储 7.2. 读写文件 open()返回一个文件对象,最常见的用法带有两个参数:open(filename, mode). >>> f = open('workfile' ...
- 数据结构与算法之美 06 | 链表(上)-如何实现LRU缓存淘汰算法
常见的缓存淘汰策略: 先进先出 FIFO 最少使用LFU(Least Frequently Used) 最近最少使用 LRU(Least Recently Used) 链表定义: 链表也是线性表的一种 ...
- windows10添加电源计划修改的快捷方案
转自:http://news.mydrivers.com/1/431/431346.htm 由于目前的Windows 10预览版在UI方面还未优化到位,所以某些设置选项要想找出来是很难的,这时候如果能 ...
- 用javascript实现的验证码
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- 修改jpivot源码实现分页
使用jpivot过程中,如果查询到的结果行数超过一个阈值,后面的显示就会丢失,这时需要分页显示. 假设应用中组装的MDX语句已经含有NON EMPTY,把空行直接过滤掉了. 这时需要修改的jpivot ...
- ssh学习(1)
雇员薪资管理系统(crud) ①先搞定spring ②引入spring包 ③编写applicationContext.xml文件(或者beans.xml),我们把该文件放在src目录下 ④测试一下sp ...
- ajax_基础
ajax 请求过程 1.准备发送请求 2.填写请求地址和数据 3.请请求到服务器 4.等待服务器处理数据. 5.接受服务器返回信息 --------------------------------- ...
- hzau 1199 Little Red Riding Hood
1199: Little Red Riding Hood Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 918 Solved: 158[Submit ...
- windows7安装Scrapy
在Linux下安装Scrapy很容易,基本不会出现问题 pip install Scrapy 但是在Windows下经常会出现问题,首先还是尝试一下使用pip命令: pip install scrap ...