codeforces Round #259(div2) E解决报告

codeforces Round #259(div2) E解决报告

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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.


codeforces Round #259(div2) E解决报告

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 ≤ nui ≠ 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 2
1 2
2 3
1 1 1

output
3
1 2 3

input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0

output
10
2 1 3 4 5 4 5 4 3 1 

input
2 0
0 0

output
0

题目大意:

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

解法:

首先我们能够明白一点,这就是一个图的遍历,找一个点,设为起点,建立一个搜索遍历树,对于树每个点,我们全然能够控制奇偶性,如果:

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

       通过上述的操作,我们能够每次保证子节点的奇偶性符合要求,然而父节点的奇偶性如若不符合要求,则能够通过调整父节点 与 父节点的父节点来调整奇偶性。这样我们就能够奇偶性传递,终于传递到根节点。

根节点如若不符合该怎样调整呢?因为我们是走遍历树。到达叶节点还要回来的,意味着我们要走至少两次根节点。一次是出发。一次是最后一次回归。我们能够将根节点 r1 调整到根节点的当中一个子节点r2,使得奇偶性再次得到调整

代码:

#include <cstdio>
#include <vector>
#define N_max 123456

using namespace std;

int n, m, fa[N_max], want[N_max];
int Odd_n, Odd_x, haveOdd[N_max];
vector <int> 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 < G[now].size(); i++) {
		int nex = G[now][i];
		if (nex == pre)  continue;

		dfs(nex, now);
		ans.push_back(now);
		want[now] ^= 1;

		if (want[nex]) {
			ans.push_back(nex);
			want[nex] ^= 1;
			ans.push_back(now);
			want[now] ^= 1;
		}
	}
}

void solve() {
	if (Odd_n == 0) {
		printf("0\n");
		return;
	}

	if (Odd_n > 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 < ans.size(); i++)
		printf("%d ", ans[i]);
}

int main() {
	init();
	solve();
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117173.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • GridLayout用法「建议收藏」

    GridLayout用法「建议收藏」概述  在Android中,使用的最多的布局是LinearLayout了,它可以让布局界面中的子控件以常见的方式比如水平或者垂直方向对齐。在使用LinearLayout时,开发者应该会记得,会经常遇到复杂的布局结构,所以会时常使用各种LinearLayout进行嵌套,而且应该注意嵌套层次不要过多。  有很多不错的文章(比如有:AndroidLayoutTricks#1,Flattening

  • 面试知识点总结之操作系统

    面试知识点总结之操作系统

  • 管理学第三章_企业集团管理第五章自测

    管理学第三章_企业集团管理第五章自测文章目录主要内容项目范围6个过程范围管理的重要性总表5.1范围管理概述5.2规划范围管理5.3收集需求主要内容项目范围6个过程(1)规划范围管理:对如何定义、确认和控制项目范围的过程进行描述。(2)收集需求:为实现项目目标,明确并记录项目干系人的相关需求的过程。(3)定义范围:详细描述产品范围和项目范围,编制项目范围说明书,作为以后项目决策的基础。(4)刨建工作分解结构(WBS):把整个项目工作分解为较小的、易于管理的组成部分,形成一个自上而下的分解结构。(5)确认范围:正式验收已完成的可交付

  • Windows 网络通信套接字技术

    Windows 网络通信套接字技术一、TCP/IP介绍1、TCP/IP体系结构TCP/IP协议实际上就是在物理网上的一组完整的网络协议。其中TCP是提供传输 层服务,而IP则是提供网络层服务。TCP/IP协议包括如下协议,其结构如图所示。IP: 网间协议(Internet Protocol) 负责主机间数据的路由和网络上数据的存储。 同时为ICMP,TCP,UDP提供分组发送服务。用户进程通常不需要涉及这一层。ARP: …

  • HTML5小游戏之见缝插针

    今天给大家带来的就是一款叫做《见缝插针》的游戏。有空你就往里插,直到你无处可插!看你能过多少关!简洁大气黑白搭配游戏画面非常的简洁,米白色的背景中央,放置着一个不断旋转的太阳状的球体,周边网状似的放

    2021年12月21日
  • JavaScript数组-冒泡排序

    JavaScript数组-冒泡排序数组的冒泡排序算法也算一道经典面试题了,这里也给大家分享一下JavaScript中关于数组的冒泡排序的写法和思路:先给大家上代码:<script>//冒泡排序:将数组中的数字按照从大到小或从小到大的顺序排序vararr=[2,4,5,1,3];for(vari=0;i<arr.length-1;i++){//外层循环管趟数,即数组的全部项数都排好一共需要比较多少次一趟排好一个,注意趟

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号