当前位置:网站首页>[牛客挑战赛47]C.条件 (bitset加速floyd)
[牛客挑战赛47]C.条件 (bitset加速floyd)
2022-04-23 06:21:00 【Zimba_】
题意:
给定 n n n个点的图,有 m 1 m_{1} m1条边一定存在,有 m 2 m_{2} m2条边一定不存在。
给出 Q Q Q次询问,每次询问两个点 x x x和 y y y,问:
- x x x是否一定可达 y y y
- x x x是否可能可达 y y y
( n ≤ 1000 , Q ≤ 200000 ) (n\leq1000,Q\leq 200000) (n≤1000,Q≤200000)
做法:
首先要明确的思路是建两张图,一张是只有一定存在的边的,一张是包括一定存在的边和可能存在的边的。
然后对于两张图都预处理出点间的可达性,最后询问直接查询。
一种想法是有向图的可达性,我们用 S C C SCC SCC求出强连通分量,然后缩点变成 D A G DAG DAG,然后就可以搞了。
不过题解给的思路好像更妙一些。
有向图我们可以用 f l o y d floyd floyd在 o ( n 3 ) o(n^{3}) o(n3)下处理出两两可达性,然后用 b i t s e t bitset bitset加速一下,最终复杂度为 o ( n 3 / w ) o(n^{3}/w) o(n3/w), w w w为64。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bitset<1001>f[1001],f1[1001];
int mpp[1005][1005];
int main()
{
int n,m1,m2,q;
scanf("%d%d%d%d",&n,&m1,&m2,&q);
for(int i=1;i<=m1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mpp[a][b]=1;
}
for(int i=1;i<=m2;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mpp[a][b]=2;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)f[i][j]=f1[i][j]=1;
if(mpp[i][j]==1)
{
f[i][j]=1;
f1[i][j]=1;
}
else if(mpp[i][j]==0)
{
f1[i][j]=1;
}
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)if(f[i][k])
{
f[i]=f[i]|f[k];
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)if(f1[i][k])
{
f1[i]=f1[i]|f1[k];
}
}
while(q--)
{
int a,b;
scanf("%d%d",&a,&b);
if(f[a][b])printf("Yes ");
else printf("No ");
if(f1[a][b])printf("Yes\n");
else printf("No\n");
}
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43785386/article/details/112757309
边栏推荐
猜你喜欢

通过sparksql读取presto中的数据存到clickhouse

# 可视化常见绘图(二)折线图

使用compressorjs压缩图片,优化功能,压缩所有格式的图片

manjaro安装与配置(vscode,微信,美化,输入法)

免费开源充电桩物联网云平台

记录阿里云服务器挖矿程序处理

菜菜的刷题日记 | 238.除自身以外数组的乘积

Beifeng communication helps Zhanjiang fire brigade build PDT wireless communication system

Background management system framework, there is always what you want

利用mysql-binlog恢复数据
随机推荐
Urban emergency management - urban emergency communication command and dispatching system
How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?
go语言:在函数间传递切片
el-select 中v-model绑定值,数据回显只显示value,不显示label
自定义classloader并实现热部署-使用loadClass
anaconda3安装
字节跳动2020秋招编程题:根据工号快速找到自己的排名
remote: Support for password authentication was removed on August 13, 2021.
HuggingFace
简单易懂的子集dp
Solution of emergency communication system for major security incidents
PyTorch 11. Regularization
SDC intelligent communication patrol management system of Nanfang investment building
可视化常见问题解决方案(七)画图刻度设置解决方案
Typora语法详解(一)
Transformer的pytorch实现
可视化常见绘图(一)堆叠图
presto日期函数的使用
Pycharm
华为云MVP邮件