CodeforcesD. Aztec Catacombs
$n \leq 300000$的完全无向图,每条边有可行和不可行的状态,一开始只有$m \leq 300000$条边是可行的,给出。每次从$x$走到$y$时,所有与$x$相连的边的可行/不可行状态会改变。问从1最少走几步到$n$。
先考虑只走原来有的路,如果走原来有的路能到$n$,那么这可能是一种可行方案。如果要利用边状态的改变来到达$n$,最优地可以找一个距离为2的点,走到他,再回到1,然后一步到$n$,读者自证不难(考虑每一步如果会出现更优的利用原图没有的边来走会发生什么)。也就是说,直接走路程$\leq4$时直接走,否则找个距离2的绕一圈。
如果没有距离2的点,又不能到$t$,那么1所在的连通分量大概是个菊花,但菊花的叶子是有连边的。这时从1走出去再也回不到1,可删之,与1相连的点变成若干连通块。新到那个点叫$x$,如果$x$的连通块里有个距离$x$为2的点,那可以用上面的方法得到5的答案;如果没有,说明这个点与分量里的所有点有连边,那还要递归下去找吗?不必了,如果枚举所有与1相连的点做如此判断,得不到5的答案就无解了。证明可以联系大前提:没有与1距离大于1的点。
//#include<iostream>
#include<cstring>
#include<cstdio>
//#include<math.h>
//#include<set>
//#include<queue>
//#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define LL long long
int qread()
{
char c; int s=,f=; while ((c=getchar())<'' || c>'') (c=='-') && (f=-);
do s=s*+c-''; while ((c=getchar())>='' && c<=''); return s*f;
} //Pay attention to '-' , LL and double of qread!!!! int n,m;
#define maxn 300011
struct Edge{int to,next;}edge[maxn<<]; int first[maxn],le=;
void in(int x,int y) {Edge &e=edge[le]; e.to=y; e.next=first[x]; first[x]=le++;}
void insert(int x,int y) {in(x,y); in(y,x);} int dis[maxn],pre[maxn],ufs[maxn],que[maxn],head,tail,du[maxn],size[maxn];
int find(int x) {return x==ufs[x]?x:(ufs[x]=find(ufs[x]));}
void Union(int x,int y) {if ((x=find(x))!=(y=find(y))) ufs[x]=y,size[y]+=size[x];}
void bfs(int s)
{
for (int i=;i<=n;i++) dis[i]=0x3f3f3f3f,pre[i]=;
head=tail=; que[tail++]=s; dis[s]=;
while (head!=tail)
{
int x=que[head++];
for (int i=first[x];i;i=edge[i].next)
{
Edge &e=edge[i]; if (e.to==) continue;
if (dis[e.to]==0x3f3f3f3f)
{
dis[e.to]=dis[x]+;
pre[e.to]=x;
que[tail++]=e.to;
}
}
}
} int main()
{
n=qread(); m=qread();
for (int i=,x,y;i<=m;i++) {x=qread(); y=qread(); insert(x,y);}
bfs();
if (dis[n]<=)
{
printf("%d\n1 ",dis[n]);
int ans[],lans=;
for (int i=n;i!=;i=pre[i]) ans[++lans]=i;
for (int i=lans;i;i--) printf("%d ",ans[i]);
return ;
}
for (int i=;i<=n;i++) if (dis[i]==)
{
printf("4\n1 %d %d 1 %d",pre[i],i,n);
return ;
}
for (int i=;i<n;i++) du[i]=-,ufs[i]=i,size[i]=;
for (int i=;i<n;i++) if (dis[i]==)
for (int j=first[i];j;j=edge[j].next)
{
if (edge[j].to!=) Union(i,edge[j].to);
du[i]++;
}
for (int i=;i<n;i++) if (dis[i]==)
{
int u=size[find(i)];
if (u-==du[i]) continue;
bfs(i);
for (int j=;j<n;j++) if (dis[j]==)
{
printf("5\n1 %d %d %d %d %d",i,pre[j],j,i,n);
return ;
}
}
puts("-1");
return ;
}
CodeforcesD. Aztec Catacombs的更多相关文章
- Atitit 常用二维码对比(QR、PDF417、DM、汉信码 Aztec code maxicode
Atitit 常用二维码对比(QR.PDF417.DM.汉信码 Aztec code maxicode DM码则更"小",可在仅仅25mm²的面积上编码30个数字.但也就是因为太小 ...
- Codeforces 890C - Petya and Catacombs 模拟
C. Petya and Catacombstime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...
- Codeforce 886 Технокубок 2018 - Отборочный Раунд 3 C. Petya and Catacombs(结论题)
A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really exper ...
- CF886C Petya and Catacombs
题目描述 A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really ...
- codeforce 886C Petya and Catacombs (map,思路)
突然发现百度不到这题的单独题解(果然是因为这是水题么),那我就来写一个了~ 先把题给贴了. C. Petya and Catacombs time limit per test 1 second me ...
- (zxing.net)二维码Aztec的简介、实现与解码
一.简介 Aztec Code是1995年,由Hand HeldProducts公司的Dr. Andrew Longacre设计.它是一种高容量的二维条形码格式.它可以对ASCII和扩展ASCII码进 ...
- 二维码Aztec简介及其解码实现(zxing-cpp)
Aztec Code是1995年,由Hand HeldProducts公司的Dr. Andrew Longacre设计.它是一种高容量的二维条形码格式.它可以对ASCII和扩展ASCII码进行编码.当 ...
- Codeforces Round #445 C. Petya and Catacombs【思维/题意】
C. Petya and Catacombs time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 【Codeforces Round #445 (Div. 2) C】 Petya and Catacombs
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看时间戳为i的点有哪些. 每次优先用已经访问过的点. 如果不行就新创一个点. 注意新创点的时间戳也是i. [代码] #includ ...
随机推荐
- CF-629 D - Babaei and Birthday Cake (离散化 + 线段树|树状数组)
求上升子序列的最大和.O(n^2)会暴力,在查询的时候要用线段树维护 因为权值是浮点数,故先离散化一下,设第 i 个位置的权值,从小到大排名为 id.那么dp转移中 \[d[i] = max(d[i] ...
- 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭
正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...
- GoogleTest 之路1-Generic Build Instructions编译指导总方案
准备工作 为了在你的测试中使用GoogleTest, 你必须让你的编译系统 知道到哪里去寻找GoogleTest 的头文件和源文件. 具体的方法只能依赖于你具体使用的哪种编译系统了,一般来讲这个非常容 ...
- 2-python基础
1.第一个程序 新建一个python文件,然后写上代码,运行即可 print("hello world") 2.变量 变量就是存东西,供后面来用的 变量名只能是 字母.数字或下划线 ...
- Yii2 HelloWord
一.安装Yii2 可以直接官网或github下载: https://github.com/yiisoft/yii2 下载完后进入项目的web/目录,入口文件为该目录下的index.php,这里直接启用 ...
- C语言结构体初始化的四种方法(转载)
原文:https://blog.csdn.net/ericbar/article/details/79567108 定义 struct InitMember { int first: double s ...
- POJ2239二分匹配
开始以为是最长路,想着把每一门课程的每一节课时作为一个点去建有向图...然后写的时候发现点太多了(300*7*12)建图特麻烦,就果断放弃了这个思路. 然后开始用排除法来想用什么算法合适,没环不可能缩 ...
- HDU 5396 区间DP 数学 Expression
题意:有n个数字,n-1个运算符,每个运算符的顺序可以任意,因此一共有 (n - 1)! 种运算顺序,得到 (n - 1)! 个运算结果,然后求这些运算结果之和 MOD 1e9+7. 分析: 类比最优 ...
- Nginx与Lua的开发
1. Lua基础语法 安装lua hello world 也可以编写lua脚本 运行脚本 lua注释 变量 局部变量的话前面加个local 循环 if语句 2. Nginx与Lua开发环境 https ...
- org.hibernate.AnnotationException: No identifier specified for entity: com.example1.demo1.Entity.User错误
最近在公司带人,他们问我的问题在这里也顺便总结下. 此项目为SpringDataJpa项目. 出现的错误如下: Caused by: org.hibernate.AnnotationException ...