Codeforce 453 C. Little Pony and Summer Sun Celebration


从一个奇数点开始走(对森林统一考虑),如果走过一个偶数点在回溯回来的时候,再重复走一下这一段把标记消掉,奇数就不用了。

C. 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.

Codeforce 453 C. Little Pony and Summer Sun Celebration_第1张图片

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. If xi = 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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn=100100;

vector<int> G[maxn],ans;

int n,m,c[maxn];
bool vis[maxn];

void dfs(int u)
{
	c[u]^=1; ans.push_back(u);
	vis[u]=true;
	for(int i=0,sz=G[u].size();i<sz;i++)
	{
		int v=G[u][i];
		if(vis[v]) continue;

		dfs(v);
		c[u]^=1; ans.push_back(u);

		if(c[v])
		{
			c[v]^=1; ans.push_back(v);
			c[u]^=1; ans.push_back(u);
		}
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	int root=-1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",c+i);
		if(c[i]) root=i;
	}
	if(root==-1) {puts("0"); return 0;}
	dfs(root);
	if(c[root]==1)	
	{
		c[root]^=1;
		ans.pop_back();
	}	
	bool flag=true;
	for(int i=1;i<=n;i++)
	{
		if(c[i])
		{
			flag=false ; break;
		}
	}
	if(flag==true)
	{
		int sz=ans.size();
		printf("%d\n",sz);
		for(int i=0;i<sz;i++)
		{
			printf("%d ",ans[i]);
		}
	}
	else puts("-1");
	return 0;
}





你可能感兴趣的:(Codeforce 453 C. Little Pony and Summer Sun Celebration)