传送门

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的更多相关文章

  1. CodeForces 232C Doe Graphs(分治+搜索)

    CF232C Doe Graphs 题意 题意翻译 \(Doe\)以她自己的名字来命名下面的无向图 \(D(0)\)是只有一个编号为\(1\)的结点的图. \(D(1)\)是只有两个编号分别为\(1\ ...

  2. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  3. Codeforces Round #144 (Div. 2)

    A. Perfect Permutation 奇偶对调. B. Non-square Equation \(s(x)\)不超过200,根据求根公式计算\(x\). C. Cycles 每次新增点时都和 ...

  4. tunning-Instruments and Flame Graphs

    On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...

  5. 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 ...

  6. 特征向量-Eigenvalues_and_eigenvectors#Graphs

    https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A               {\displaystyle A} ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. Graphs and Minimum Cuts(Karger's Min-Cut Algorithm)

    Graphs  Two ingredients 1. vertices (nodes) v 2. edges(undirected or directed) Examples: road networ ...

  9. 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 ...

随机推荐

  1. Redis学习笔记之Redis的对象

    类型与编码: typedef struct redisObject {                unsigned type:4://类型               unsigned encod ...

  2. 《Linux内核设计与实现》第四章学习笔记——进程调度

                                                                        <Linux内核设计与实现>第四章学习笔记——进程调 ...

  3. 《Linux内核分析》第四周:扒开系统调用的三层皮

    杨舒雯 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 " 一. 用户 ...

  4. 从零开始学Kotlin-泛型(8)

    从零开始学Kotlin基础篇系列文章 与 Java 一样,Kotlin 也提供泛型,为类型安全提供保证,消除类型强转的烦恼. 泛型类的基本使用 泛型,即 "参数化类型",将类型参数 ...

  5. Alpha冲刺第9天

    Alpha第9天 1.团队成员 郑西坤 031602542 (队长) 陈俊杰 031602504 陈顺兴 031602505 张胜男 031602540 廖钰萍 031602323 雷光游 03160 ...

  6. Activiti随着Spring启动自动部署开关

    Activiti的act_re_deployment表NAME_列:全部显示SpringAutoDeployment. 查阅Activiti,https://github.com/Activiti/A ...

  7. js & parseFloat & toFixed

    js & parseFloat & toFixed https://repl.it/languages/javascript https://repl.it/repls/MintyBa ...

  8. quartz.net 3.x版本如何通过xml文件进行Job配置

    在2.x版本中,我们可以简单的在quartz.config文件中通过以下Xml配置方式来注册相应的Job以及触发器 quartz.plugin.xml.type = Quartz.Plugin.Xml ...

  9. obj.getClass() == Person.class 用于判断类型

    obj.getClass() == Person.class  用于判断类型

  10. 02 使用Mybatis的逆向工程自动生成代码

    1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...