Pictionary 方法记录
[COCI2017-2018#5] Pictionary
题面翻译
题目描述
在宇宙一个不为人知的地方,有一个星球,上面有一个国家,只有数学家居住。
在这个国家有\(n\)个数学家,有趣的是,每个数学家都住在自己的城市,且城市间无道路相连,因为他们可以在线交流。当然,城市有从\(1\)到\(n\)的编号。
一位数学家决定用手机发论文,而手机将“不言而喻”自动更正成了“猜谜游戏”。
不久之后,这个国家就发现了猜谜游戏。他们想要见面一起玩,于是这个国家就开始了修路工程。
道路修建会持续\(m\)天。对于第\(i\)天,若\(\gcd(a,b)=m-i+1\),则\(a\)和\(b\)城市间会修一条路。
由于数学家们忙于建筑工作,请你来确定一对数学家最早什么时候能凑到一起玩。
输入输出格式
输入格式
第一行有三个正整数\(n,m,q\),表示城市数量、修路持续天数、询问数量。
接下来\(q\)行,每行有两个正整数\(a,b\),表示询问\(a\)和\(b\)两个城市的数学家最早什么时候能在一起玩。
输出格式
输出\(q\)行,第\(i\)行有一个正整数,表示第\(i\)次询问的结果
说明
数据范围:
对于\(40\%\)的数据:
\(n≤4000,q≤10^5\)
对于全部数据:
\(1≤n,q≤10^5\)
\(1≤m≤n\)
样例1解释:
在第一天,\((3,6)\)之间修了一条路,因此第二次询问输出1
在第二天,\((2,4),(2,6),(2,8),(4,6),(6,8)\)之间都修了一条路,此时\(4\)和\(8\)号城市连通,第三次询问输出2
在第三天,所有编号互质的城市之间都修了路,\(2\)和\(5\)号城市在此时连通,第一次询问输出1
样例2解释:
在第二天,\((20,15)\)之间修了一条路
第四天,\((15,9)\)之间修了一条路
所以\(20\)和\(9\)号城市在第四天连通,输出4
题目描述
There is a planet, in a yet undiscovered part of the universe, with a country inhabited solely
by mathematicians. In this country, there are a total of N mathematicians, and the interesting
fact is that each mathematician lives in their own city. Is it also interesting that no two cities
are connected with a road, because mathematicians can communicate online or by
reviewing academic papers. Naturally, the cities are labeled with numbers from 1 to N.
Life was perfect until one mathematician decided to write an academic paper on their
smartphone. The smartphone auto-corrected the word “self-evident” to “Pictionary” and the
paper was published as such. Soon after, the entire country discovered pictionary and
wanted to meet up and play, so construction work on roads between cities began shortly.
.
The road construction will last a total of M days, according to the following schedule: on the
first day, construction is done on roads between all pairs of cities that have M as their
greatest common divisor. On the second day, construction is done on roads between all
pairs of cities that have M-1 as their greatest common divisor, and so on until the \(M^{th}\) day
when construction is done on roads between all pairs of cities that are co-prime. More
formally, on the \(i^{th}\) day, construction is done on roads between cities a and b if gcd(a, b) = \(M-i+1\).
Since the mathematicians are busy with construction work, they’ve asked you to help them
determine the minimal number of days before a given pair of mathematicians can play
pictionary together.
输入格式
The first line of input contains three positive integers N, M
and Q
(1 ≤ N
, Q ≤ 100 000, 1 ≤ M
≤ N
), the number of cities, the number of days it takes to build the roads, and the number of
queries.
Each of the following Q lines contains two distinct positive integers A and B
(1 ≤ A
, B ≤ N
)
that denote the cities of the mathematicians who want to find out the minimal number of days
before they can play pictionary together.
输出格式
The \(i^{th}\) line must contain the minimal number of days before the mathematicians from the \(i^{th}\) query can play pictionary together.
样例 #1
样例输入 #1
8 3 3
2 5
3 6
4 8
样例输出 #1
3
1
2
样例 #2
样例输入 #2
25 6 1
20 9
样例输出 #2
4
样例 #3
样例输入 #3
9999 2222 2
1025 2405
3154 8949
样例输出 #3
1980
2160
提示
In test cases worth 40% of total points, it will hold N
≤ 1000, Q
≤ 100 000.
Clarification of the first test case:
On the first day, road (3, 6) is built. Therefore the answer to the second query is 1.
On the second day, roads (2, 4), (2, 6), (2, 8), (4, 6) and (6, 8) are built. Cities 4 and 8 are now
connected (it is possible to get from the first to the second using city 6).
On the third day, roads between relatively prime cities are built, so cities 2 and 5 are connected.
Clarification of the second test case:
On the second day, road (20, 15) is built, whereas on the fourth day, road (15, 9) is built. After the
fourth day, cities 20 and 9 are connected via city 15.
审题
题意转化:
有\(n\)座城市;
有\(m\)个时刻,在第\(i\)个时刻,所有\(m-i+1\)的倍数之间连边;
有\(q\)个询问,询问第\(x_i\)座城市和第\(y_i\)座城市什么时候连通。
连边(合并)
连通城市的过程,其实就是对多个点建立集合,并进行合并的过程,所以考虑使用并查集。
在对两个城市进行连边(即合并两个点)时,钦定第一个城市是第二个城市的父亲节点,并记录这两个点合并时的时间戳。
查询
现询问两个点\((x,y)\)的最早连通时间。\((x,y)\)的最早连通时间就是\(x\)到\(lca(x,y)\)中的最大时间戳加上\(lca(x,y)\)到\(y\)中的最大时间戳。
为什么是最大值?
两个城市要想连通,就必须要等到路径上最后一条边建好才行。
为什么是\(lca\)?
题目中城市\(x\)和城市\(y\)的连通抽象到图上就是\(x\)到\(y\)的一条树上路径(同一条边不能重复走),也就是\(x\)->\(lca(x,y)\)->\(y\).这里不能包含\(lca(x,y)\),因为这个点储存的时间戳是和另一个点击合并的时间。
考虑\(lca\)的求法。在处理并查集的时候,其实就处理出的图上的每一个父子关系,我们可以将其利用起来:根据这些父子关系,\(x\)和\(y\)往上跳,从而找到\(lca(x,y)\).
但要想保存父子关系就不能使用路径压缩,所以采用启发式合并:将小集合合并到大集合,以起到优化的作用。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m,q;
int f[N],siz[N],dfn[N],dep[N];
struct Edge//链式前向星存图
{
int u,v,nex;
}e[N<<1];
int head[N],tot;
void add(int u,int v)
{
tot++;
e[tot].u=u;
e[tot].v=v;
e[tot].nex=head[u];
head[u]=tot;
}
int maxx(int a,int b)
{
return a>b?a:b;
}
int find(int x)//并查集查找(不能用路径压缩)
{
if(x==f[x]) return x;
return find(f[x]);
}
void dfs(int u)//dfs求每个点相对于超级祖先的深度
{
for(int i=head[u];i;i=e[i].nex)
{
int v=e[i].v;
dep[v]=dep[u]+1;
dfs(v);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
int rt;
for(int i=1;i<=n;i++)
{
f[i]=i;
siz[i]=0;
}
for(int i=m;i>=1;i--)
{
for(int j=(i<<1);j<=n;j+=i)//对所有m-i+1的倍数之间连边
{
int dx=find(i),dy=find(j);
if(dx==dy)
{
continue;
}
if(siz[dx]<siz[dy])//启发式合并,将小集合合并到大集合
{
swap(dx,dy);
}
f[dy]=dx;
add(dx,dy);
rt=dx;//rt最终会记录所有点的公共祖先,钦定这个“超级祖先”为根节点
if(siz[dx]==siz[dy])
{
siz[dx]++;
}
dfn[dy]=m-i+1;//dfn记录合并时的时间戳
}
}
dfs(rt);
for(int i=1;i<=q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int ans=0;
if(dep[x]<dep[y])
{
swap(x,y);
}
while(dep[x]>dep[y])
{
ans=maxx(ans,dfn[x]);
x=f[x];
}
while(x!=y)
{
ans=maxx(maxx(dfn[x],dfn[y]),ans);
x=f[x],y=f[y];
}
printf("%d\n",ans);
}
}
Pictionary 方法记录的更多相关文章
- EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态
本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...
- 64位 SQL Server2008链接访问Oracle 过程汇总解决方法记录
64位 SQL Server2008链接访问Oracle 过程汇总解决方法记录 经过几天不停的网上找资料,实验,终于联通了. 环境:系统:win 2008 ,SqlServer2008 R2, 连接O ...
- js实用方法记录-js动态加载css、js脚本文件
js实用方法记录-动态加载css/js 附送一个加载iframe,h5打开app代码 1. 动态加载js文件到head标签并执行回调 方法调用:dynamicLoadJs('http://www.yi ...
- js实用方法记录-简单cookie操作
js实用方法记录-简单cookie操作 设置cookie:setCookie(名称,值,保存时间,保存域); 获取cookie:setCookie(名称); 移除cookie:setCookie(名称 ...
- js实用方法记录-指不定哪天就会用到的js方法
js实用方法记录-指不定哪天就会用到的js方法 常用或者不常用都有 判断是否在微信浏览器中 测试代码:isWeiXin()==false /** * 是否在微信中 */ function isWeix ...
- Java给各个方法记录执行时间
Java给各个方法记录执行时间 long startTime = System.currentTimeMillis();...//要测试时间的方法LoggerFactory.getLogger(Bas ...
- make menuconfig error 解决方法记录
新建的一个虚拟机,发现make menuconfig 后会出错,查了一下是缺少一些库. 这个错误已经错了两次了,我不希望第三次出现了还想不起来,所以特此记录. # 错误信息: make[2]: *** ...
- 简单一键CENTOS6 安装PPTP VPN方法记录
申明:我们使用PPTP VPN仅仅只能用在查阅资料等正规渠道,不要用在不良用途上.方法收集于网上,这里我用在搬瓦工VPS(VPS方案直达),采用的是CENTOS6 64位系统.我们需要预先将VPS服务 ...
- ASP.NET页面优化性能提升方法记录
今天与大家分享:一种优化页面执行速度的方法.采用这个方法,可以使用页面的执行速度获得[8倍]的提升效果. 为了让您对优化的效果有个直观的了解,我准备了下面的测试结果截图: 测试环境:1. Window ...
随机推荐
- java学习第二天小细节.day10
栈内存溢出表示可以使用递归 This的使用 普通方法,字段,其他方法与构造器三种访问方法 Super的使用 在子类如果需要使用到父类的字段者使用到super(字段,字段),需要放到第一行,因需要初始化 ...
- 自定义spring boot starter 初尝试
自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...
- Java 在PDF中添加水印
在日常工作和学习中,PDF的普及给人们带来了极大的便利,但同时也出现了很多问题. PDF文件容易复制和传播,版权难以保护. 在海量文件中也很难区分信息,例如,你有没有打开一个文件夹,里面有这么多同名的 ...
- [CF1386C] Joker (IOI 赛制,分治,整体二分+可回退并查集)
题面 给一个 N N N 点 M M M 边的简单无向图,询问 Q Q Q 次,每次问你把编号在 [ l i , r i ] [l_i,r_i] [li,ri] 之间的边删掉后,该图是否存在奇数环 ...
- k8s-Pod调度
Deployment全自动调度 NodeSelector定向调度 NodeAffinity亲和性 PodAffinity-Pod亲和性与互斥性 污点和容忍度 DaemonSet Job CronJob ...
- 新增一个Redis 从节点为什么与主节点的key数量不一样呢?
在日常的 Redis 运维过程中,经常会发生重载 RDB 文件操作,主要情形有: 主从架构如果主库宕机做高可用切换,原从库会挂载新主库重新获取数据 主库 QPS 超过10万,需要做读写分离,重新添加从 ...
- haodoop概念总结
大数据部门组织结构 Hadoop的优势(4高) 高可靠性:Hadoop底层维护多个数据副本 高扩展性:在集群间分配任务数据,可方便的扩展 高效性:在MapReduce的思想下,Hadoop时并行工作的 ...
- 《Java基础——继承》
Java基础--继承 一.类的继承: 规则: 1.一个子类只能有一个父类,但一个父类可以有多个子类,每个子类都可以作为父类使用. 2.若一个类没有声明父类,则默认其父类为Object. 3.父 ...
- Python数据科学手册-机器学习: 决策树与随机森林
无参数 算法 随机森林 随机森林是一种集成方法,集成多个比较简单的评估器形成累计效果. 导入标准程序库 随机森林的诱因: 决策树 随机森林是建立在决策树 基础上 的集成学习器 建一颗决策树 二叉决策树 ...
- 8. Ceph 基础篇 - 运维常用操作
文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485300&idx=1&sn=aacff9f7 ...