zoukankan      html  css  js  c++  java
  • CodeForces

    B - Resort

     CodeForces - 350B 

    B. Resort
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Valera's finally decided to go on holiday! He packed up and headed for a ski resort.

    Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has nobjects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.

    Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.

    Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions:

    1. Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel.
    2. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1.
    3. The path contains as many objects as possible (k is maximal).

    Help Valera. Find such path that meets all the criteria of our hero!

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — the number of objects.

    The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.

    The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number aiequals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.

    Output

    In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.

    Examples
    input
    Copy
    5
    0 0 0 0 1
    0 1 2 3 4
    output
    Copy
    5
    1 2 3 4 5
    input
    Copy
    5
    0 0 1 0 1
    0 1 2 2 4
    output
    Copy
    2
    4 5
    input
    Copy
    4
    1 0 0 0
    2 3 4 2
    output
    Copy
    1
    1



          读题就读了好长好长时间.
        
     
    题意呢,就是:有n个地方,给出n大小的序列a,0代表ai是山地,1代表ai是旅馆,再给出个n大小的序列v,第i的序列表示ai可以冲vi到达,问怎么走才能使路途最长(终点要是旅馆,而且路中途没有分叉)
        
          拿样例二来说
          0 1 2 2 4    就是2连1,3连2,4-2,5-4,画图就是这个样子:
          交叉点不能算,所以2忽略,根据0 0 1 0 1,    3,5都是旅馆,均为终点,有3和4,5两条,第二条最长所以选第二个。
          因为题意终点是旅馆,但我们可以反向建图,从旅馆出发,走下去直到不能再走,比如遇到分岔点或者尽头,这样问题就很清晰了。由于涉及到连接,可以用并查集来存和查找,也是个dfs的过程。
        
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<map>
    using namespace std;
    const int maxn=1e5+10;
    int a[maxn],b[maxn],fa[maxn];
    int ac[maxn];
    int dfs(int i)
    {
        int ans=0;      //在以i往下走的过程中,记录步数
        while(fa[i]!=-1)
        {
            i=fa[i];
            ans++;
        }
        return ans;
    }
    int main()
    {
        int n;
        while(cin>>n)
        {
            memset(fa,-1,sizeof(fa));  //边界
            map<int,int>mm;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];    //存入节点类型,旅馆或山头
            }
            for(int i=1;i<=n;i++)
            {
                cin>>b[i];    //存入b【i】,同时计数
                mm[b[i]]++;
            }
            for(int i=1;i<=n;i++)
            {
                if(mm[i]<=1)    //如果不是分岔点,那么可以表明,i的根节点是b[i]
                    fa[i]=b[i];  //如果,mm[i]>1,则此为分岔点,fa=-1,也为边界
            }
            int maxx=-1;    //记录最长路径
            int k;        //记录最长路径的起始点(旅馆)
            for(int i=1;i<=n;i++)
            {
                if(a[i]==1)
                {
                    int mid=dfs(i);  //如果是旅馆,以此i跑dfs
                    if(mid>maxx)
                    {
                        maxx=mid;  //记录最大步数,并记录i值
                        k=i;
                    }
                }
            }
            cout<<maxx<<endl;
            int tot=1;
            while(1)
            {          //开新数组记录过程,以k为起点,不断跑下去,记录。
                ac[tot++]=k;
                k=fa[k];  
                if(fa[k]==-1)  //跑到边界就break;
                    break;
            }
            for(int i=tot-1;i>=1;i--)
                cout<<ac[i]<<" "; 
                cout<<endl;
        }
    }
    
    

    好题!妥妥的好题!!!


          
  • 相关阅读:
    WebSocket理解与使用
    JS面向对象与prototype,__proto__,constructor
    JS值传递与引用传递
    vue报错:./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!
    对于图片视频编辑类产品的思考
    时尚衣裙外贸独立站搭建(一)
    2021年Wordpress手把手教你做个独立站——部署篇
    一次备案失败所引发的思考
    2021年Wordpress博客装修美化(二)
    做什么,怎么做?(20210615)
  • 原文地址:https://www.cnblogs.com/liyexin/p/11663923.html
Copyright © 2011-2022 走看看