• 洛谷P3533 [POI2012]RAN-Rendezvous


    P3533 [POI2012]RAN-Rendezvous

    题目描述

    Byteasar is a ranger who works in the Arrow Cave - a famous rendezvous destination among lovers.

    The cave consists of nn chambers connected with one-way corridors.

    In each chamber exactly one outgoing corridor is marked with an arrow.

    Every corridor leads directly to some (not necessarily different) chamber.

    The enamoured couples that agree to meet in the Arrow Cave are notorious for forgetting to agree upon specific chamber, and consequently often cannot find their dates.

    In the past this led to many mix-ups and misunderstandingsdots But ever since each chamber is equipped with an emergency telephone line to the ranger on duty, helping the enamoured find their dates has become the rangers' main occupation.

    The rangers came up with the following method.

    Knowing where the enamoured are, they tell each of them how many times they should follow the corridor marked with an arrow in order to meet their date.

    The lovers obviously want to meet as soon as possible - after all, they came to the cave to spend time together, not to wander around alone!

    Most rangers are happy to oblige: they do their best to give each couple a valid pair of numbers such that their maximum is minimal.

    But some rangers, among their numbers Byteasar, grew tired of this extracurricular activity and ensuing puzzles. Byteasar has asked you to write a program that will ease the process. The program, given a description of the cave and the current location of kk couples, should determine kk pairs of numbers x_ixi and y_iyi such that if the ii-th couple follows respectively: he x_ixiand she y_iyi corridors marked with arrows,then they will meet in a single chamber of the cave max(x_i,y_i)max(xi,yi) is minimal,subject to above min(x_i,y_i)min(xi,yi) is minimal,if above conditions do not determine a unique solution, then the woman should cover smaller distance (x_ige y_ixiyi).

    It may happen that such numbers x_ixi and y_iyi do not exist - then let x_i=y_i=-1xi=yi=1. Note that it is fine for several couples to meet in a single chamber. Once the lovers have found their dates, they will be happy to lose themselves in the cave again...

    给定一棵内向森林,多次给定两个点a和b,求点对(x,y)满足:

    1.从a出发走x步和从b出发走y…

    输入输出格式

    输入格式:

    In the first line of the standard input there are two positive integers nn and kk(1le n,kle 500 0001n,k500 000), separated by a single space, that denote the number of chambers in the Arrow Cave and the number of couples who want to find their dates, respectively.

    The chambers are numbered from 1 to nn, while the enamoured couples are numbered from 1 to kk.

    The second line of input contains nn positive integers separated by single spaces:

    the ii-th such integer determines the number of chamber to which the corridor marked with an arrow going out of chamber iileads.

    The following kk lines specify the queries by the separated couples. Each such query consists of two positive integers separated by a single space - these denote the numbers of chambers where the lovers are - first him, then her.

    In the tests worth 40% of the total points it additionally holds that n,kle 2 000n,k2 000.

    输出格式:

    Your program should print exactly kk lines to the standard output, one line per each couple specified in the input:

    the ii-th line of the output should give the instructions for the ii-th couple on the input.

    I.e., the ii-th line of output should contain the integers x_i,y_ixi,yi, separated by a single space.

    输入输出样例

    输入样例#1: 复制
    12 5
    4 3 5 5 1 1 12 12 9 9 7 1
    7 2
    8 11
    1 2
    9 10
    10 5
    输出样例#1: 复制
    2 3
    1 2
    2 2
    0 1
    -1 -1

    说明

    给定一棵内向森林,多次给定两个点a和b,求点对(x,y)满足:

    1.从a出发走x步和从b出发走y步会到达同一个点

    2.在1的基础上如果有多解,那么要求max(x,y)最小

    3.在1和2的基础上如果有多解,那么要求min(x,y)最小

    4.如果在1、2、3的基础上仍有多解,那么要求x>=y

    /*
        n个点,n条边且每个点都有出边,显然是环套树森林。
        先dfs把环套树拆成一堆树,倍增LCA。
        先将x,y两个点倍增到环上,然后判断即可。
    */
    #include<cstdio>
    #include<algorithm>
    #define maxn 500050
    using namespace std;
    int n,fa[maxn][20],root,q,circle[maxn],dep[maxn];
    int num[maxn],sum[maxn],tot,pos[maxn],vis[maxn];
    void findcircle(int x){
        int now=x;
        while(1){
            if(vis[x]==now)break;
            if(vis[x])return;
            vis[x]=now;
            x=fa[x][0];
        }
        tot++;
        while(!circle[x]){
            circle[x]=x;
            dep[x]=1;
            num[x]=++sum[tot];
            pos[x]=tot;
            x=fa[x][0];
        }
    }
    void dfs(int x){
        if(dep[x])return;
        dfs(fa[x][0]);
        circle[x]=circle[fa[x][0]];
        dep[x]=dep[fa[x][0]]+1;
        for(int i=1;(1<<i)<dep[x];i++)
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    int lca(int a,int b){
        if(dep[a]!=dep[b]){
            if(dep[a]<dep[b])swap(a,b);
            for(int i=18;i>=0;i--)
                if(dep[fa[a][i]]>=dep[b])a=fa[a][i];
        }
        if(a==b)return a;
        for(int i=18;i>=0;i--)
            if(fa[a][i]!=fa[b][i])
                a=fa[a][i],b=fa[b][i];
        if(a==b)return a;
        return fa[a][0];
    }
    bool judge(int a,int b,int c,int d){
        if(max(a,b)<max(c,d))return 1;
        if(max(a,b)>max(c,d))return 0;
        if(min(a,b)<min(c,d))return 1;
        if(min(a,b)>min(c,d))return 0;
        if(a>=b)return 1;
        return 0;
    }
    int main(){
        freopen("Cola.txt","r",stdin);
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)scanf("%d",&fa[i][0]);
        for(int i=1;i<=n;i++)findcircle(i);
        for(int i=1;i<=n;i++)if(!circle[i])dfs(i);
        while(q--){
            int x,y;
            scanf("%d%d",&x,&y);
            if(pos[circle[x]]!=pos[circle[y]]){
                puts("-1 -1");continue;
            }
            if(circle[x]==circle[y]){
                int t=lca(x,y);
                printf("%d %d
    ",dep[x]-dep[t],dep[y]-dep[t]);
                continue;
            }
            int ans1=dep[x]-1,ans2=dep[y]-1,t=pos[circle[x]];
            x=num[circle[x]];y=num[circle[y]];
            int z1=(sum[t]+y-x)%sum[t];
            int z2=sum[t]-z1;
            if(judge(ans1+z1,ans2,ans1,ans2+z2))
            printf("%d %d
    ",ans1+z1,ans2);
            else printf("%d %d
    ",ans1,ans2+z2);
        }
    }
  • 相关阅读:
    ES6学习之数组扩展
    js三元表达式
    ES6学习之函数扩展
    TS学习之for..of
    TS学习之Symbol
    Visual Studio编译时报错“函数名:重定义;不同的基类型”
    Windows Visual Studio中修改PostgreSQL代码后调试报错
    Windows安装MySQL5.7.17
    289. Game of Life -- In-place计算游戏的下一个状态
    Swift的 convenience && designated init
  • 原文地址:https://www.cnblogs.com/thmyl/p/7794382.html
Copyright © 2020-2023  润新知