hdu5469 Antonidas(DFS)

思路:直接找到开头的那个结点,然后往子树搜或者父亲结点搜...一个可行的剪枝是如果子树剩下的最大高度小于需要匹配的字符数目的话就肯定没有解了...然后就水过去了...


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+7;
vector<int>e[maxn];
int n,tarlen;
int dep[maxn],vis[maxn],f[maxn];
char s[maxn];
char tar[maxn];
void dfs(int u,int fa)
{
    int d = 0;
//    dep[u]=d;
    f[u]=fa;
    for(int i = 0;i<e[u].size();i++)
    {
        int v = e[u][i];
        if(v!=fa)
        {    
            dfs(v,u);
            d = max(d,dep[v]);
        }
    }
    dep[u]=d+1;
}
bool dfs1(int now,int len)
{
    vis[now]=1;
    if(len==tarlen)
        return true;
    for(int i = 0;i<e[now].size();i++)
    {
        int v = e[now][i];
        if(!vis[v] && f[now]!=v && s[v]==tar[len] && (tarlen-len<=dep[v]))
            if(dfs1(v,len+1))return true;
    }
    int fa = f[now];
    if(!vis[fa] && s[fa]==tar[len])
        if (dfs1(fa,len+1))return true;
    return false;
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i= 0;i<=n;i++)
            e[i].clear();
        memset(dep,0,sizeof(dep));
        for(int i = 1;i<=n-1;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            e[u].push_back(v);
            e[v].push_back(u);
        }
        dfs(1,0);
        scanf("%s",s+1);
        scanf("%s",tar);
        tarlen = strlen(tar);
        bool flag = false;
        for(int i = 1;i<=n;i++)
        {
            if(s[i]==tar[0])
            {
                memset(vis,0,sizeof(vis));
                flag = dfs1(i,1);
                if(flag)
                    break;
            }
        }
        printf("Case #%d: ",cas++);
        if(flag)
            printf("Find\n");
        else
            printf("Impossible\n");
    }
}


Problem Description
Given a tree with  N  vertices and  N1  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  N1  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.
1T200 1N104 1a,bN ab |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
 

Source
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值