[ABC244G] Construct Good Path
Problem Statement
You are given a simple connected undirected graph with $N$ vertices and $M$ edges. (A graph is said to be simple if it has no multi-edges and no self-loops.)
For $i = 1, 2, \ldots, M$, the $i$-th edge connects Vertex $u_i$ and Vertex $v_i$.
A sequence $(A_1, A_2, \ldots, A_k)$ is said to be a path of length $k$ if both of the following two conditions are satisfied:
- For all $i = 1, 2, \dots, k$, it holds that $1 \leq A_i \leq N$.
- For all $i = 1, 2, \ldots, k-1$, Vertex $A_i$ and Vertex $A_{i+1}$ are directly connected with an edge.
An empty sequence is regarded as a path of length $0$.
You are given a sting $S = s_1s_2\ldots s_N$ of length $N$ consisting of $0$ and $1$.
A path $A = (A_1, A_2, \ldots, A_k)$ is said to be a good path with respect to $S$ if the following conditions are satisfied:
- For all $i = 1, 2, \ldots, N$, it holds that:
- if $s_i = 0$, then $A$ has even number of $i$'s.
- if $s_i = 1$, then $A$ has odd number of $i$'s.
Under the Constraints of this problem, it can be proved that there is at least one good path with respect to $S$ of length at most $4N$.
Print a good path with respect to $S$ of length at most $4N$.
Constraints
- $2 \leq N \leq 10^5$
- $N-1 \leq M \leq \min\lbrace 2 \times 10^5, \frac{N(N-1)}{2}\rbrace$
- $1 \leq u_i, v_i \leq N$
- The given graph is simple and connected.
- $N, M, u_i$, and $v_i$ are integers.
- $S$ is a string of length $N$ consisting of $0$ and $1$.
Input
Input is given from Standard Input in the following format:
$N$ $M$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_M$ $v_M$
$S$
Output
Print a good path with respect to $S$ of length at most $4N$ in the following format.
Specifically, the first line should contain the length $K$ of the path, and the second line should contain the elements of the path, with spaces in between.
$K$
$A_1$ $A_2$ $\ldots$ $A_K$
Sample Input 1
6 6
6 3
2 5
4 2
1 3
6 5
3 2
110001
Sample Output 1
9
2 5 6 5 6 3 1 3 6
The path $(2, 5, 6, 5, 6, 3, 1, 3, 6)$ has a length no greater than $4N$, and
- it has odd number ($1$) of $1$
- it has odd number ($1$) of $2$
- it has even number ($2$) of $3$
- it has even number ($0$) of $4$
- it has even number ($2$) of $5$
- it has odd number ($3$) of $6$
so it is a good path with respect to $S = 110001$.
Sample Input 2
3 3
3 1
3 2
1 2
000
Sample Output 2
0
An empty path $()$ is a good path with respect to $S = 000000$.
Alternatively, paths like $(1, 2, 3, 1, 2, 3)$ are also accepted.
真的要图吗?可以尝试只经过图里面的一棵生成树
考虑数中序列节点相邻的在序列相邻是什么东西?欧拉环游序!
但是欧拉环游序的奇偶不一定正确,怎么办?
想一下如何改变一个位置的奇偶。可以先向他父亲走一步,然后走回来,然后再向父亲走,好像就满足了。
但是 1 没有父亲?
反正一开始都搜到了,在最后回去的时候,特判一下,不走就行了
#include<cstdio>
const int N=1e5+5;
int n,m,k,idx,a[N<<2],b[N<<2],f[N],t[N],fa[N],hd[N],e_num,u,v,c[N];
char s[N];
struct edge{
int v,nxt;
}e[N<<2];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
void dfs(int x,int y)
{
a[++idx]=x;
for(int i=hd[x];i;i=e[i].nxt)
{
if(e[i].v!=y)
{
dfs(e[i].v,x);
a[++idx]=x;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
if(find(u)!=find(v))
fa[find(u)]=find(v),add_edge(u,v),add_edge(v,u);
}
scanf("%s",s+1);
dfs(1,0);
for(int i=idx;i>=1;i--)
if(!t[a[i]])
t[a[i]]=1,f[i]=1;;
for(int i=1;i<idx;i++)
{
if(f[i])
if((c[a[i]]&1)==(s[a[i]]-'0'))
b[++k]=a[i],b[++k]=a[i+1],c[a[i+1]]++,c[a[i]]++;
b[++k]=a[i];
c[a[i]]++;
}
if((c[1]&1)!=(s[1]-'0'))
b[++k]=1;
printf("%d\n",k);
for(int i=1;i<=k;i++)
printf("%d ",b[i]);
}
[ABC244G] Construct Good Path的更多相关文章
- Path Creation and Path Painting
[Path Creation and Path Painting] Path creation and path painting are separate tasks. First you crea ...
- imutils.path
from imutils import paths # 要在哪条路径下查找 path = '...' # 查找图片,得到图片路径 imagePaths = list(imutils.paths.lis ...
- alfresco category searches...
From page 475 of the Alfresco Developer Guide- Category searches use the PATH field, but you constru ...
- ArrowLayer : A coustom layer animation
Since my other answer (animating two levels of masks) has some graphics glitches, I decided to try r ...
- Java fundamentals of basic IO
IO is a problem difficult to handle in various of systems because it always becomes a bottleneck in ...
- react native mac install
Mac上使用react native tips: 1. 安装Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/ ...
- boost操作xml 5分钟官方教程
Five Minute Tutorial This tutorial uses XML. Note that the library is not specifically bound to XML, ...
- Device trees, Overlays and Parameters of Raspberry Pi
Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...
- hdfs api读写文写件个人练习
看下hdfs的读写原理,主要是打开FileSystem,获得InputStream or OutputStream: 那么主要用到的FileSystem类是一个实现了文件系统的抽象类,继承来自org. ...
- Android測试环境变量配置表
要改动的文件是~/bash_profile这个配置文件,内容例如以下: # Setting PATH for Java JAVA_HOME="/Library/Java/JavaVirtua ...
随机推荐
- python flask 简单应用开发
转载请注明出处: Flask 是一个基于 Python 的微型 Web 框架,它提供了一组简洁而强大的工具和库,用于构建 Web 应用程序.Flask 的主要作用是帮助开发者快速搭建轻量级的.灵活的 ...
- Excel中的数值四舍五入方法详解
在日常工作和数据处理中,我们经常需要对数值进行四舍五入操作.Excel作为一款强大的电子表格软件,提供了多种方法来实现数值的四舍五入.本文将介绍Excel中常用的四舍五入函数及其基本使用方法. ROU ...
- 通过商品API接口获取到数据后的分析和应用
一.如果你想要分析商品API接口获取到的数据,可以按照如下的步骤进行: 了解API接口返回值的格式,如JSON格式.XML格式.CSV格式等,选择适合你的数据分析方式. 使用API请求工具(如Post ...
- WPF 自定义窗体(一)
.Net默认的窗体样式只有四种:None.SingleBorderWindow.ThreeDBorderWindow.ToolWindow,都比较"丑".而很多时候,我们希望自定义 ...
- Go 常用命令介绍
Go 常用命令 目录 Go 常用命令 一.Go 常用命令 1.1 go build 1.1.1 指定输出目录 1.1.2 常用环境变量设置编译操作系统和 CPU 架构 1.1.3 查看支持的操作系统和 ...
- git Failed to connect to 127.0.0.1 port xxxx: Connection refused 的问题。
问题描述在使用 git 拉取.提交代码的时候,会出现 git Failed to connect to 127.0.0.1 port xxxx: Connection refused 的问题. 原因: ...
- 入门篇-其之五-Java运算符(上)
一元运算符之正负号 Java支持多种一元运算符,一元运算符中的"一元"是指一个操作数.我们初中学过的正负号就属于一元运算符,因为正负号后面只有一个数字. 正数使用+表示,其中+可以 ...
- DHorse v1.4.2 发布,基于 k8s 的发布平台
版本说明 优化特性 在集群列表增加集群版本: 修改Jvm的GC指标名: 解决问题 解决shell脚本换行符的问题: 解决部署历史列表页,环境名展示错误的问题: 解决指标收集功能的异常: 升级指南 升级 ...
- My Code Style
大家都在写,跟风. 头文件 万能头.因为我刚学 OI 的时候怎么都背不住 algorithm 怎么拼( 变量 数组开全局,一些前后重名/只在某一部分用的变量开局部. 尽量不使用指针/ stl 迭代器等 ...
- 为何 DevOps 会给开发人员带来压力和倦怠?
企业正在享受 DevOps 实施带来的好处,但这也是有代价的.开发人员需要承担额外的责任,可能会导致他们感到疲惫不堪.因此我们可以采取一些方法来确保 DevOps 工程师的满意度. DevOps 的支 ...