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;
    }
  • 相关阅读:
    QTableView表格控件区域选择-自绘选择区域
    Qt高仿Excel表格组件-支持冻结列、冻结行、内容自适应和合并单元格
    QRowTable表格控件(三)-效率优化之-合理使用QStandardItem
    QRowTable表格控件(二)-红涨绿跌
    QRowTable表格控件-支持hover整行、checked整行、指定列排序等
    Qt实现表格控件-支持多级列表头、多级行表头、单元格合并、字体设置等
    Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证
    Bootstrap:弹出框和提示框效果以及代码展示
    Bootstrap treeview增加或者删除节点
    bootstrap-treeview 如何实现全选父节点下所有子节点及反选
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5814444.html
Copyright © 2011-2022 走看看