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 ...
随机推荐
- java基础—哈希编码
- javascript原型继承圣杯模式
javascript纯面向对象开发需要使用到的一个模式,来对对象之间原型继承做中间层代理避免重复继承与代码杂乱 <!DOCTYPE html> <html lang="en ...
- 20180901 JavaScript闭包和匿名函数自动调用
引用: 1. JavaScript闭包_by runoob 2. JS中(function(){xxx})这么写是什么意思? (一)闭包是可以访问上一层函数作用域里变量的函数,即便上一层函数已经关闭. ...
- MySQL数据库主从切换脚本自动化
MySQL数据库主从切换脚本自动化 本文转载自:https://blog.csdn.net/weixin_36135773/article/details/79514507 在一些实际环境中,如何实现 ...
- 二分查找、upper_bound、lower_bound
整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...
- tp5使用外部类的三种方法
在tp5中使用外部类的时候有三种方法 第一种就是通过composer下载,通过这种方式下载的外部类能够支持自动加载,我们只要在使用的时候use一下命名空间就可以使用了 比如:我们的tp5第四季项目要使 ...
- python基本操作(四)
与用户交互 为什么交互? 计算机取代人类,解放劳动力 如何交互 print('-'*100) input('请输入你的姓名:') print(""100) Python2和Pyth ...
- atag信息处理
machine_desc->boot_params参数保存的是u-boot传入的启动参数的地址,如果没有传入启动参数,使用如下的默认参数: /* * This holds our default ...
- LeetCode (160) Intersection of Two Linked Lists
题目 Write a program to find the node at which the intersection of two singly linked lists begins. For ...
- 字符串:HDU3064-最长回文
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descri ...