CF232C Doe Graphs
Solution: (不理解时对着图研究一下就清楚啦!!!)
sm[i]为|D(i)| (x,y,n)为x,y在D(n)中的最短路
已知sm[i-1]+1为D(i)的割点
于是x-y的最短路就可以分为三种情况:
- x<sm[n-1]+1&&y>=sm[n-1]+1
- x<sm[n-1]+1&&y<sm[n-1]+1
- x>=sm[n-1]+1&&y>=sm[n-1]+1
下面我们就来讨论这三种情况
- x在图D(n-1)上,y在图D(n-2)上,它们的最短路必过割点sm[n-1]+1
我们只要分别求解x,y到割点的最短路即可
y到割点的最短路即为(1,y-sm[n-1],n-2)
x到割点的最短路却有两种可能 (1,x,n-1)+1或(x,sm[n-1],n-1)+1 这两种情况取小即可
- x,y都在图D(n-1)上
一定要注意这里 x-y的最短路并不一定局限于D(n-1) 还有可能经过割点
所以这里有两种情况:(x,y,n-1)
又有两种经过割点的方式: (1,x,n-1)+(y,sm[n-1],n-1)+2 和 (1,y,n-1)+(x,sm[n-1],n-1)+2
同样取小即可
- x,y都在图D(n-2)上
是最简单的一种情况啊,为(x,y,n-2)
但是如果这样子递归下去是会TLE的,所以我们要优化一下
发现只要求出图D(i)中x,y点到1和sm[i]的最短路就可以了
于是预处理出就可以了
d1[i]为(1,x,i) d2[i]为(x,sm[i],i) d3[i]为(1,y,i) d4[i]为(y,sm[i],i)
pre函数看图研究一下就可以理解啦
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#define R register
#define go(i,a,b) for(R int i=a;i<=b;i++)
#define ll long long
#define M 105
using namespace std;
ll rd()
{
ll x=,y=;char c=getchar();
while(c<''||c>''){if(c=='-')y=-;c=getchar();}
while(c>=''&&c<=''){x=(x<<)+(x<<)+c-'';c=getchar();}
return x*y;
}
ll T,n,sm[M],d[M],d1[M],d2[M],d3[M],d4[M];
void pre(ll x,ll nw,ll t1[],ll t2[])
{
if(nw==) return ;
if(nw==) {t1[]=(x==);t2[]=(x==);return ;}
if(x<=sm[nw-])
{
pre(x,nw-,t1,t2);
t1[nw]=min(t1[nw-],t2[nw-]+);
t2[nw]=min(t1[nw-],t2[nw-])+d[nw-]+;
}
else
{
pre(x-sm[nw-],nw-,t1,t2);
t1[nw]=t1[nw-]+;
t2[nw]=t2[nw-];
}
}
ll qy(ll x,ll y,ll nw)
{
if(nw<=) return x!=y;
if(x<sm[nw-]+&&y>=sm[nw-]+) return min(d1[nw-],d2[nw-])+d3[nw-]+;
if(x<sm[nw-]+&&y<sm[nw-]+) return min(qy(x,y,nw-),min(d1[nw-]+d4[nw-],d2[nw-]+d3[nw-])+);
return qy(x-sm[nw-],y-sm[nw-],nw-);
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
T=rd();n=rd();n=min(n,(ll));
sm[]=;sm[]=;d[]=;d[]=;//d[i]表示D(i)的1到sm[i]结点的最短距离
go(i,,n) sm[i]=sm[i-]+sm[i-],d[i]=d[i-]+;n=min(n,(ll));
while(T--)
{
ll x=rd(),y=rd();if(x>y)swap(x,y);
pre(x,n,d1,d2);pre(y,n,d3,d4);
printf("%lld\n",qy(x,y,n));
}
return ;
}
后:
真的没那么难啊 仔细分析细心一点就没有问题啦
然而 我还是调了一晚上qwq 因为longlong 要哭了...
如果哪里不懂一定要问我 因为可能我也不懂那我就要感谢你发现我没懂的地方啦
然后我们可以一起研究啦啦啦
CF232C Doe Graphs的更多相关文章
- CodeForces 232C Doe Graphs(分治+搜索)
CF232C Doe Graphs 题意 题意翻译 \(Doe\)以她自己的名字来命名下面的无向图 \(D(0)\)是只有一个编号为\(1\)的结点的图. \(D(1)\)是只有两个编号分别为\(1\ ...
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- Codeforces Round #144 (Div. 2)
A. Perfect Permutation 奇偶对调. B. Non-square Equation \(s(x)\)不超过200,根据求根公式计算\(x\). C. Cycles 每次新增点时都和 ...
- tunning-Instruments and Flame Graphs
On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...
- Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图
https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...
- 特征向量-Eigenvalues_and_eigenvectors#Graphs
https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A {\displaystyle A} ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- Graphs and Minimum Cuts(Karger's Min-Cut Algorithm)
Graphs Two ingredients 1. vertices (nodes) v 2. edges(undirected or directed) Examples: road networ ...
- Safari HTML5 Canvas Guide: Creating Charts and Graphs
Safari HTML5 Canvas Guide: Creating Charts and Graphs Bar graphs are similar to data plots, but each ...
随机推荐
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
- 20135202闫佳歆--week2 操作系统是如何工作的--学习笔记
此为个人学习笔记存档 week 2 操作系统是怎么工作的 一.计算机是如何工作的?--三个法宝 (一)三个法宝 1.存储程序计算机 所有计算机的基础性的逻辑框架. 2.函数调用堆栈 在低级语言中并不很 ...
- [转帖]ESXi、Linux、Windows获取机器序列号的方法
http://blog.51cto.com/liubin0505star/1717473 windows: wmic bios get serialnumber linux: dmidecode准确一 ...
- [转帖] 外部访问k8s 里面pod的方式方法
https://jimmysong.io/posts/accessing-kubernetes-pods-from-outside-of-the-cluster/ 从外部访问Kubernetes中的P ...
- Python day5 --------递归、匿名函数、高阶函数、内置函数
一.递归 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 递归要求: 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减 ...
- Python【知识点】面试小点列表生成式小坑
1.问题 有这么一个小面试题: 看下面代码请回答输出的结果是什么?为什么? result = [lambda x: x + i for i in range(10)] print(result[0]( ...
- BZOJ2743 HEOI2012采花(离线+树状数组)
如果能够把所有区间内第二次出现某颜色的位置标记出来,树状数组查询一下就可以了. 考虑离线.按左端点从小到大排序,不断移动左端点并更新第二次出现的位置. #include<iostream> ...
- linq partition by
static void Main(string[] args) { var beatles = (new[] { new { id=1 , inst = "guitar" , na ...
- GCD - Extreme (II) UVA - 11426(欧拉函数!!)
G(i) = (gcd(1, i) + gcd(2, i) + gcd(3, i) + .....+ gcd(i-1, i)) ret = G(1) + G(2) + G(3) +.....+ G(n ...
- 【BZOJ2067】SZN(二分,动态规划,贪心)
[BZOJ2067]SZN(二分,动态规划,贪心) 题面 权限题额 Description String-Toys joint-stock 公司需要你帮他们解决一个问题. 他们想制造一个没有环的连通图 ...