热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

codeforcesRound#259(div2)E解题报告_html/css_WEBITnose

E.LittlePonyandSummerSunCelebrationtimelimitp


E. Little Pony and Summer Sun Celebration

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.

Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?

Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.

Input

The first line contains two integers n and m (2?≤?n?≤?105; 0?≤?m?≤?105) ? the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui,?vi (1?≤?ui,?vi?≤?n; ui?≠?vi), these integers describe a road between places ui and vi.

The next line contains n integers: x1,?x2,?...,?xn (0?≤?xi?≤?1) ? the parity of the number of times that each place must be visited. If xi?=?0, then the i-th place must be visited even number of times, else it must be visited odd number of times.

Output

Output the number of visited places k in the first line (0?≤?k?≤?4n). Then output k integers ? the numbers of places in the order of path. Ifxi?=?0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.

If there is no required path, output -1. If there multiple possible paths, you can output any of them.

Sample test(s)

input

3 21 22 31 1 1

output

31 2 3

input

5 71 21 31 41 53 43 54 50 1 0 1 0

output

102 1 3 4 5 4 5 4 3 1

input

2 00 0

output

题目大意:

给出一张图,有N个点,M条边,并给出每个点要求访问次数的奇偶性。,要求输出访问路径。

解法:

首先我们可以明确一点,这就是一个图的遍历,找一个点,设为起点,建立一个搜索遍历树,对于树每一个点,我们完全可以控制奇偶性,假设:

目前访问的点为v,父节点为fa,如若点v不符合当前的奇偶性,则就让父节点到v绕一次,这样 odd[v] ^= 1, fa[v] ^= 1,这样我们可以完全保证完全控制子节点,将不符合要求的奇偶性调整成符合要求的奇偶性。同时父节点的奇偶性也在改变。

通过上述的操作,我们可以每次保证子节点的奇偶性符合要求,然而父节点的奇偶性如若不符合要求,则可以通过调整父节点 与 父节点的父节点来调整奇偶性,这样我们就可以奇偶性传递,最终传递到根节点。根节点如若不符合该如何调整呢?由于我们是走遍历树,到达叶节点还要回来的,意味着我们要走至少两次根节点,一次是出发,一次是最后一次回归。我们可以将根节点 r1 调整到根节点的其中一个子节点r2,使得奇偶性再次得到调整

代码:

#include #include #define N_max 123456using namespace std;int n, m, fa[N_max], want[N_max];int Odd_n, Odd_x, haveOdd[N_max];vector G[N_max], ans;int getf(int x) { return (fa[x] == x) ? x : fa[x] = getf(fa[x]);}void addedge(int x, int y) { G[x].push_back(y);}void init() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); int tmpx=getf(x); int tmpy=getf(y); if (tmpx != tmpy) { fa[tmpx] = tmpy; addedge(x, y); addedge(y, x); } } for (int i = 1; i <= n; i++) { scanf("%d", &want[i]); if (want[i]) haveOdd[getf(i)] = 1; } for (int i = 1; i <= n; i++) if (haveOdd[i]) { Odd_n++; Odd_x = i; }}void dfs(int now, int pre) { ans.push_back(now); want[now] ^= 1; for (int i = 0; i 1) { printf("-1\n"); return; } dfs(Odd_x, -1); int x = 0; if (want[Odd_x]) x=1; printf("%d\n", ans.size() - x); for (int i = x; i

推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文介绍了如何将CIM_DateTime解析为.Net DateTime,并分享了解析过程中可能遇到的问题和解决方法。通过使用DateTime.ParseExact方法和适当的格式字符串,可以成功解析CIM_DateTime字符串。同时还提供了关于WMI和字符串格式的相关信息。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Inno Setup区段之Components篇相关知识详解
    本文详细介绍了Inno Setup区段之Components篇相关的知识,包括Components和Types的使用方式以及各个参数的说明,希望对读者有一定的参考价值。内容涵盖了ComponentsName、Description、Types、ExtraDiskSpaceRequired、ExtraDiskSpaceRequiredFlags等多个关键词,帮助读者更好地理解和应用Inno Setup区段之Components篇的知识。 ... [详细]
  • WebSocket与Socket.io的理解
    WebSocketprotocol是HTML5一种新的协议。它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送 ... [详细]
author-avatar
胡鹏锦_289
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有