Cycling City CF521E
Cycling City
毒瘤题
首先建dfs树,由于是个无向图所有返祖边都是连向祖先的。
判是否有解其实很简单,只要图不是一个仙人掌就有解了。
仙人掌有关可以看这个博客
但是这道题由于要输出路径成功变成了一个一个顶俩的题。。。。。
通过判仙人掌我们确认了一个点,它连向自己父亲的路径至少被两个边所覆盖。
然后我们可以从这个点开始深搜,找到这两个返祖,注意是返向这个点祖先的路径。
于是有起点就是这两个路径下端的lca,终点就是这两个路径上端点最深的一个(画图很好看出来的)
然后有哪些路径就显然了。。
输出方案毒瘤!
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
#define MAXN 201116
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
int n , m;
vector<int> G[MAXN];
int dep[MAXN] , par[MAXN] , dir[MAXN];
void dfs( int u , int fa ) {
dep[u] = dep[fa] + 1 , par[u] = fa;
for( int i = 0 ; i < G[u].size( ) ; ++ i ) {
int v = G[u][i];
if( v == fa ) continue;
if( !dep[v] ) dfs( v , u ) , dir[u] += dir[v];
else if( dep[v] < dep[u] )
++ dir[u] , -- dir[ v ];
}
}
int l , r , L , R;
int s;
void dfs2( int u , int fa ) {
for( int i = 0 ; i < G[u].size() ; ++ i ) {
int v = G[u][i];
if( dep[v] == dep[u] + 1 ) dfs2( v , u );
else if( dep[v] < dep[s] && v != fa ) {
if( l ) L = u , R = v;
else l = u , r = v;
}
if( L ) return;
}
}
vector<int> ans , tmp;
void doit( int fr , int to ) {
if( dep[fr] < dep[to] ) {
tmp.clear();
to = par[to];
while( to != fr )
tmp.pb( to ) , to = par[to];
tmp.pb( fr );
for( auto it = tmp.rbegin() ; it != tmp.rend( ) ; ++ it )
ans.pb( *it );
return;
}
while( fr != to )
ans.pb( fr ) , fr = par[fr];
}
int main() {
cin >> n >> m;
for( int i = 1 , u , v ; i <= m ; ++ i ) {
scanf("%d%d",&u,&v);
G[u].pb( v ) , G[v].pb( u );
}
for( int i = 1 ; i <= n ; ++ i ) if( !dep[i] ) dfs( i , 0 );
for( int i = 1 ; i <= n ; ++ i ) if( dir[i] > 1 )
{ s = i; break; }
if( !s ) return puts("NO") , 0;
puts("YES");
dfs2( s , par[s] );
if( dep[R] < dep[r] ) swap( r , R ) , swap( l , L );
int x = l , y = L;
while( x != y ) dep[x] > dep[y] ? x = par[x] : y = par[y];
doit( x , l ) , ans.pb( l ) , doit( r , R ) , ans.pb( R );
printf("%d ",ans.size());
for( auto& x : ans ) printf("%d ",x);
puts("");
ans.clear();
doit( x , L ) , ans.pb( L ) , ans.pb( R );
printf("%d ",ans.size());
for( auto& x : ans ) printf("%d ",x);
puts("");
ans.clear();
doit( x , R ) , ans.pb( R );
printf("%d ",ans.size());
for( auto& x : ans ) printf("%d ",x);
puts("");
}
Cycling City CF521E的更多相关文章
- 「CF521E」 Cycling City
「CF521E」 Cycling City 传送门 首先你能发现这个东西一定是两个环的公共边. 最开始想的是什么如果一个点被访问过三次那它一定是公共边的某一端之类的东西,然后发现被仙人掌叉掉. 然后就 ...
- Codeforces 521 E cycling city
cf的一道题,非常有意思,题目是问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 其实就是判断一下是否为仙人掌就行了,如果不是仙人掌的话肯定就存在,题目难在输出路径上,改了半 ...
- [CF]Cycling City
题目大意:给定一张无向图,问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 解法: 我们可以先走出来一个环,再出环上任意一点走到另外一点.就像这样:
- Codeforces 521E - Cycling City(点双连通分量+分类讨论)
Codeforces 题面传送门 & 洛谷题面传送门 大家都是暴力找生成树然后跳路径,代码不到 50 行(暴论)的一说--好,那本蒟蒻决定提供一种代码 150 行,但复杂度也是线性的分类讨论做 ...
- URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集
F - Cycling Roads Description When Vova was in Shenzhen, he rented a bike and spent most of the ...
- Ural 1966 Cycling Roads
================ Cycling Roads ================ Description When Vova was in Shenzhen, he rented a ...
- URAL 1966 Cycling Roads 计算几何
Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...
- BZOJ 2001: [Hnoi2010]City 城市建设
2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1132 Solved: 555[Submit][ ...
- History lives on in this distinguished Polish city II 2017/1/5
原文 Some fresh air After your time underground,you can return to ground level or maybe even a little ...
随机推荐
- 【c++ Prime 学习笔记】第19章 特殊工具与技术
某些程序对内存分配有特殊要求,不能直接使用标准内存管理机制 重载new和delete算符可控制内存分配的过程 19.1.1 重载new和delete 说法"重载new和delete" ...
- 第三次Scrum Metting
日期:2021年4月27日会议主要内容概述:确定后端和前端接口,前端讨论画图页面,解决两处画图问题 一.进度情况# 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作中遇到的困难 徐宇龙 后端 ...
- [no code][scrum meeting] Alpha 15
项目 内容 会议时间 2020-04-23 会议主题 OCR紧急会议 会议时长 45min 参会人员 PM + OCR组(赵涛,黎正宇) 项目 内容 会议时间 2020-04-24 会议主题 全体测试 ...
- freemarker自定义指令
最近项目中使用了spring boot搭建项目,使用spring security管理项目中的权限,使用freemarker作为视图层.为了将权限控制到按钮上,因此考虑直接使用spring secur ...
- Xpath运算符
5.position定位 >>print tree.xpath('//*[@id="testid"]/ol/li[position()=2]/text()')[0] & ...
- 汇编--LDR
转载:https://my.oschina.net/zengsai/blog/23733 ARM LDR 伪指令的格式: LDR Rn, =expr 如果name是立即数的话LDR R0,=0X123 ...
- prometheus(1)之核心概念
个人理解:prometheus核心在于 1.prom数据类型的理解 (4钟数据类型 与常用的promQL语法 其实很容易) 2.各种服务发现与正则拼接(服务发现的拼接其实官方定义好的 理解就行) 3. ...
- JMeter学习笔记--函数学习(_csvRead 函数)
JMeter函数可以很方便实现一些小功能,几乎可以用于测试计划中的任何元件.一个函数的调用如下:${_functionName(var1,var2,var3)},_functionName匹配函数名, ...
- 组件通信之全局事件总线 & 消息订阅发布
全局事件总线 介绍 一种组件间通信的方式,适用于任意组件间通信. 在使用全局事件总线之前需要一些知识准备 所有组件实例的原型对象的原型对象就是 Vue 的原型对象,即VueComponent.prot ...
- celery config
/* Useful celery config. app = Celery('tasks', broker='redis://localhost:6379', backend='redis://loc ...