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;
    }
  • 相关阅读:
    受脑认知和神经科学启发的人工智能
    1分钟爱上管理学
    刻意练习
    课题设计相关
    销售必备心灵鸡汤(转)
    从优秀到卓越
    小记
    何谓成熟?
    三体运动的程序模拟
    行星运动轨迹的程序实现
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5814444.html
Copyright © 2011-2022 走看看