zoukankan      html  css  js  c++  java
  • 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


    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.
    1T2001N1041a,bNab|C|=N1|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=10005;
    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=0;
        for(int i=0;i<arc[u].size();i++)
        {
            int to=arc[u][i];
            if(to!=fa)
            {
                dfs1(to,u);
                mx=max(dist[to],mx);
            }
        }
        dist[u]=mx+1;
    }
    bool dfs2(int u,int net)
    {
        if(net==len)    return true;
        vis[u]=1;
        for(int i=0;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+1))
                {
                    return true;
                }
            }
        }
        int fa=par[u];
        if(!vis[fa]&&value[fa]==s[net])
        {
            if(dfs2(fa,net+1))
            {
                return true;
            }
        }
        return false;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)    arc[i].clear();
            for(int i=0;i<n-1;i++)    
            {
                int u,v;
                scanf("%d%d",&u,&v);
                arc[u].push_back(v);
                arc[v].push_back(u);
            }
            scanf("%s",value+1);
            scanf("%s",s);
            dfs1(1,0);
            len=strlen(s);
            bool tag=false;
            for(int i=1;i<=n;i++)
            {
                if(value[i]==s[0])
                {
                    memset(vis,0,sizeof(vis));
                    if(dfs2(i,1))
                    {
                        tag=true;
                        break;    
                    }
                }
            }
            printf("Case #%d: ",cas);
            if(tag)    printf("Find
    ");
            else    printf("Impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    C语言中scanf()的用法
    Android学习笔记——Day3
    Android学习笔记——Day6
    Android学习笔记——Day5
    Android学习笔记——Day4
    Android学习笔记——Day2
    一个计时器按钮
    直方图均衡
    拉普拉斯算子进行图像边缘提取
    在jframe上显示超大号的文字
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5814444.html
Copyright © 2011-2022 走看看