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 misunderstandings\dots 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_ixi​and 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_i\ge y_ixi​≥yi​).

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(1\le n,k\le 500\ 0001≤n,k≤500 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,k\le 2\ 000n,k≤2 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][],root,q,circle[maxn],dep[maxn];
int num[maxn],sum[maxn],tot,pos[maxn],vis[maxn];
void findcircle(int x){
int now=x;
while(){
if(vis[x]==now)break;
if(vis[x])return;
vis[x]=now;
x=fa[x][];
}
tot++;
while(!circle[x]){
circle[x]=x;
dep[x]=;
num[x]=++sum[tot];
pos[x]=tot;
x=fa[x][];
}
}
void dfs(int x){
if(dep[x])return;
dfs(fa[x][]);
circle[x]=circle[fa[x][]];
dep[x]=dep[fa[x][]]+;
for(int i=;(<<i)<dep[x];i++)
fa[x][i]=fa[fa[x][i-]][i-];
}
int lca(int a,int b){
if(dep[a]!=dep[b]){
if(dep[a]<dep[b])swap(a,b);
for(int i=;i>=;i--)
if(dep[fa[a][i]]>=dep[b])a=fa[a][i];
}
if(a==b)return a;
for(int i=;i>=;i--)
if(fa[a][i]!=fa[b][i])
a=fa[a][i],b=fa[b][i];
if(a==b)return a;
return fa[a][];
}
bool judge(int a,int b,int c,int d){
if(max(a,b)<max(c,d))return ;
if(max(a,b)>max(c,d))return ;
if(min(a,b)<min(c,d))return ;
if(min(a,b)>min(c,d))return ;
if(a>=b)return ;
return ;
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)scanf("%d",&fa[i][]);
for(int i=;i<=n;i++)findcircle(i);
for(int i=;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\n",dep[x]-dep[t],dep[y]-dep[t]);
continue;
}
int ans1=dep[x]-,ans2=dep[y]-,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\n",ans1+z1,ans2);
else printf("%d %d\n",ans1,ans2+z2);
}
}

洛谷P3533 [POI2012]RAN-Rendezvous的更多相关文章

  1. 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告

    P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...

  2. BZOJ2801/洛谷P3544 [POI2012]BEZ-Minimalist Security(题目性质发掘+图的遍历+解不等式组)

    题面戳这 化下题面给的式子: \(z_u+z_v=p_u+p_v-b_{u,v}\) 发现\(p_u+p_v-b_{u,v}\)是确定的,所以只要确定了一个点\(i\)的权值\(x_i\),和它在同一 ...

  3. 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]

    题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...

  4. 洛谷P3539 [POI2012] ROZ-Fibonacci Representation

    题目传送门 转载自:five20,转载请注明出处 本来看到这题,蒟蒻是真心没有把握的,还是five20大佬巨orz 首先由于斐波拉契数的前两项是1,1 ,所以易得对于任何整数必能写成多个斐波拉契数加减 ...

  5. 洛谷P3537 [POI2012]SZA-Cloakroom(背包)

    传送门 蠢了……还以为背包只能用来维护方案数呢……没想到背包这么神奇…… 我们用$dp[i]$表示当$c$的和为$i$时,所有的方案中使得最小的$b$最大时最小的$b$是多少 然后把所有的点按照$a$ ...

  6. 洛谷P3531 [POI2012]LIT-Letters

    题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...

  7. 洛谷P3534 [POI2012] STU

    题目 二分好题 首先用二分找最小的绝对值差,对于每个a[i]都两个方向扫一遍,先都改成差满足的形式,然后再找a[k]等于0的情况,发现如果a[k]要变成0,则从他到左右两个方向上必会有两个连续的区间也 ...

  8. 【洛谷3546_BZOJ2803】[POI2012]PRE-Prefixuffix(String Hash)

    Problem: 洛谷3546 Analysis: I gave up and saw other's solution when I had nearly thought of the method ...

  9. bzoj 1014: 洛谷 P4036: [JSOI2008]火星人

    题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...

随机推荐

  1. 基于Protobuf的分布式高性能RPC框架——Navi-Pbrpc

    基于Protobuf的分布式高性能RPC框架——Navi-Pbrpc 二月 8, 2016 1 简介 Navi-pbrpc框架是一个高性能的远程调用RPC框架,使用netty4技术提供非阻塞.异步.全 ...

  2. JavaUtil_09_email_使用 commons-email 发送邮件

    二.参考资料 1.[commons]邮件发送工具——commons-email

  3. 使用myeclipes制造属于自己的jar

    选定你需要在jar中包含的package或者class 步骤1:右键export导出, 步骤2:导出类型为java --JRE  file. 步骤3:直接finish即可完成 如果希望你的jar带源码 ...

  4. codeforces 633A A. Ebony and Ivory(暴力)

    A. Ebony and Ivory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Linux-安装ssh服务

    问题描述: 有些版本的linux系统,如Ubuntn 16 ,安装完成后缺少ssh服务, 所以putty链接会出现访问失败的情况. 解决办法: 在linux中安装ssh服务,并启动 1.安装 sudo ...

  6. 1045 Favorite Color Stripe (30)(30 分)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

  7. uoj problem 12 猜数

    题目大意 每次询问给出g,l,有\(a*b = g*l = n\),且\(a,b\)均为\(g\)的倍数.求\(a+b\)的最小值和\(a-b\)的最大值. 题解 因为\(a,b\)均为\(g\)的倍 ...

  8. bzoj 2178 圆的面积并 —— 辛普森积分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2178 先看到这篇博客:https://www.cnblogs.com/heisenberg- ...

  9. mac下安装node

    学着使用homebrew进行安装,发现很是方便. homebrew是mac下的一款管理安装的工具. 1. 安装homebrew 使用mac自带的ruby下载安装: ruby -e "$(cu ...

  10. Date---String is 合法的date 方法---

    package com.etc.jichu; import java.text.SimpleDateFormat; public class IsDate { public static boolea ...