传送门

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. Linux内核分析——第二章 从内核出发

    第二章 从内核出发 一.获取内核源码 1.Git是分布式的:下载和管理Linux内核源代码: 2.获取最新提交到版本树的一个副本 $ git clone git://git.kernel.org/pu ...

  2. Linux第三周学习总结——构造一个简单的Linux系统MenuOS

    第三周学习总结--构造一个简单的Linux系统MenuOS 作者:刘浩晨 [原创作品转载请注明出处] <Linux内核分析>MOOC课程http://mooc.study.163.com/ ...

  3. @PathVariable获取带点参数,获取不全

    {account:.+}在{account}后加上:.+ 可参考原博:http://blog.csdn.net/jrainbow/article/details/46126179

  4. 设置matplotlib画图支持中文显示

    1.安装中文字体 git clone https://github.com/tracyone/program_font && cd program_font && ./ ...

  5. 2013 C#单元测试

    首先安装Unit Test Generator. 点击拓展和更新——>联机搜索Unit Test Generator 新建项目 新建一个测试类  add函数 选定test 类名 ——>右键 ...

  6. Alpha 冲刺一

    团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 界面(简陋) 登录界 ...

  7. ejabberd与XMPP

    ejabberd支持XMPP协议. worktile用ejabberd来做了实时消息推送: https://worktile.com/tech/basic/worktile-real-time-not ...

  8. Majority Element问题---Moore's voting算法

    Leetcode上面有这么一道难度为easy的算法题:找出一个长度为n的数组中,重复次数超过一半的数,假设这样的数一定存在.O(n2)和O(nlog(n))(二叉树插入)的算法比较直观.Boyer–M ...

  9. php环境搭建及入门

    在php文件里,写入header('content-type:text/html;charset = uft-8'); <?php header('content-type:text/html; ...

  10. Win2008r2 由ESXi 转换到 HyperV的处理过程

    1. 大部分2008r2 采取了 windows loader的方式激活 这种方式 会导致hyperV 启动失败 因为他家在了错误的bios类型 所以第一步建议 使用windows loader 卸载 ...