Table of Contents
题目大意:
解法:
代码:
Home Web Front-end HTML Tutorial codeforces Round #259(div2) EProblem Solution Report_html/css_WEB-ITnose

codeforces Round #259(div2) EProblem Solution Report_html/css_WEB-ITnose

Jun 24, 2016 am 11:55 AM

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
Copy after login

output

31 2 3
Copy after login

input

5 71 21 31 41 53 43 54 50 1 0 1 0
Copy after login

output

102 1 3 4 5 4 5 4 3 1 
Copy after login

input

2 00 0
Copy after login

output

题目大意:

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

解法:

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

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

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

代码:

#include <cstdio>#include <vector>#define N_max 123456using 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();}
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

What are the security implications of using iframes, and how can I mitigate them? What are the security implications of using iframes, and how can I mitigate them? Mar 18, 2025 pm 02:51 PM

What are the security implications of using iframes, and how can I mitigate them?

How do I use the HTML5 <meter> element to display numerical data within a range? How do I use the HTML5 <meter> element to display numerical data within a range? Mar 12, 2025 pm 04:08 PM

How do I use the HTML5 <meter> element to display numerical data within a range?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

How do I use the HTML5 <time> element to represent dates and times semantically?

See all articles