2015北京网络赛 Couple Trees 倍增算法
题意:两棵树,求不同树上两个节点的最近公共祖先
思路:比赛时看过的队伍不是很多,没有仔细想。今天补题才发现有个 倍增算法,自己竟然不知道。
解法来自 qscqesze ,这个其实之前如果了解过倍增的话还是不是很难,不过这题的数据也不是很给力,极限数据理论上是过不了的。
其他解法有树链剖分?并不是很清楚。就这样水过了吧。。。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 100005
using namespace std; int f1[MAXN][], f2[MAXN][];
int deep1[MAXN], deep2[MAXN];
int step1, step2, ans;
void work(int x, int y){
step1 = step2 = ;
while (x != y){
if (x < y){
//x < y means y is not x's ancestor, so let y up
for (int i = ; i >= ; i--){
if (f2[y][i] > x){
y = f2[y][i];
step2 += << i;
break;
}
}
y = f2[y][];
step2++;
}
else{
for (int i = ; i >= ; i--){
if (f1[x][i] > y){
x = f1[x][i];
step1 += << i;
break;
}
}
x = f1[x][];
step1++;
}
}
ans = x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int n, m;
while (~scanf("%d%d", &n, &m)){
int x, y;
deep1[] = deep2[] = ;
for (int i = ; i <= ; i++){
f1[][i] = f2[][i] = ;
}
for (int i = ; i <= n; i++){
scanf("%d", &x);
f1[i][] = x;
deep1[i] = deep1[x] + ;
for (int j = ; j <= ; j++){
f1[i][j] = f1[f1[i][j - ]][j - ];
}
}
for (int i = ; i <= n; i++){
scanf("%d", &x);
f2[i][] = x;
deep2[i] = deep2[x] + ;
for (int j = ; j <= ; j++){
f2[i][j] = f2[f2[i][j - ]][j - ];
}
}
ans = ;
for (int i = ; i <= m; i++){
scanf("%d%d", &x, &y);
x = (x + ans) % n + ;
y = (y + ans) % n + ;
work(x, y);
printf("%d %d %d\n", ans, step1, step2);
}
}
}
2015北京网络赛 Couple Trees 倍增算法的更多相关文章
- 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT
2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- 2015北京网络赛 F Couple Trees 暴力倍增
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- acm 2015北京网络赛 F Couple Trees 树链剖分+主席树
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- acm 2015北京网络赛 F Couple Trees 主席树+树链剖分
提交 题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点 解: 我们将第一棵树进行书链剖,然后第 ...
- Hiho coder 1236 2015 北京网络赛 Score
五维偏序..一开始被吓到了,后来知道了一种BITSET分块的方法,感觉非常不错. 呆马: #include <iostream> #include <cstdio> #incl ...
- hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题
题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...
- 2015北京网络赛B题 Mission Impossible 6
借用大牛的一张图片:模拟 #include<cstdio> #include<cmath> #include<cstring> #include<algori ...
- 2015北京网络赛A题The Cats' Feeding Spots
题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. #include ...
随机推荐
- FTP 无法获取目录列表的处理方法
FTP 无法获取目录列表的处理方法 1.以阿里云的服务器为例 对于阿里云的服务器是因为阿里云为了进一步保护用户的安全利益使用了安全策略组,我们要设置安全策略组对应的端口开启. 首先要设置端口范围,这个 ...
- vue+element-ui+slot-scope或原生实现可编辑表格(日历)
你们公司的产品是不是还在做一个可编辑表格功能? 1.前言 咱开发拿到需求大多数是去网上找成型的组件,找不到再看原生的方法能否实现,大牛除外哈,大牛一般喜欢封装组件框架. 2.思路 可编辑表格在后台管理 ...
- pandas 2 选择数据
from __future__ import print_function import pandas as pd import numpy as np np.random.seed(1) dates ...
- HTTP——学习笔记(5)
我们通信的过程中会有哪些风险?: 1.HTTP不会对通信方的身份进行确认 因为HTTP协议中的请求和相应不会对通信方进行确认,就是不管发送或接收信息的人是不是之前的人,都不妨碍信息的发送或接收. 缺点 ...
- ASP.NET-跨站伪造请求CSRF
经常看到在项目中ajax post数据到服务器不加防伪标记,造成CSRF攻击,在Asp.net Mvc里加入防伪标记很简单在表单中加入Html.AntiForgeryToken()即可Html.Ant ...
- 你必须了解的RecyclerView的五大开源项目-解决上拉加载、下拉刷新和添加Header、Footer等问题
前段时间做项目由于采用的MD设计,所以必须要使用RecyclerView全面代替ListView.但是开发中遇到了需要实现RecyclerView上拉加载.下拉刷新和添加Header以及Footer等 ...
- [Javascript] Improve Composition with the Compose Combinator
To make our composition more readable and easier to name we are going to ceate a compose function we ...
- UTF8有很明显的特征:如果最高字节为0,则表示一个英文字符(与ASCII完全相同)。如果有2个以上1,表示是首个字节。如果最高位是10,则表示一个中间字节。
摘自<Qt中的C++技术.pdf> page 33
- 9.优先队列,priority_queue
#include <iostream> #include <queue> #include <deque> #include <list> using ...
- Kali linux 2016.2(Rolling)里Metasploit的常用模块
端口扫描 auxiliary/scanner/portscanscanner/portscan/ack ACK防火墙扫描scanner/portscan/ftpbounce FTP跳端口扫描scann ...