Codeforces Round #259 (Div. 2) E. Little Pony and Summer Sun Celebration

题目链接:http://codeforces.com/contest/454/problem/E

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 ≤ 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


题意:已知一张图,对其中的某些点要求经过奇数次,其他点要求经过偶数次,输出一条符合的路径,如果不存在则输出-1,路径长度不能超过点数*4。

题解:
     稍微推敲一下可以发现路径的首尾至少有一个点是奇数点。
     可以从任意一个奇数点出发,试试能不能找到路径。
     对于每个点,如果它是奇数度点,就可以通过走回它的父节点再走到它来改变它的次数的奇偶性,这样子的操作同时会改变它的父节点的奇偶性,它父结点的奇偶性则可以通过走回它父节点的父节点来完成。
     递归操作即可。
     需要注意的是最后需要判断一下整张图的所有点是不是都变成偶数度点了。如果没有则说明图不联通,输出-1即可。(这里坑了一下TAT)


#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <vector>
using namespace std;
vector <int> v[100005];
bool f[100005];
int p[400005];
bool check[100005];
int sta;
int sum;
void dfs(int num,int fa)
{
	check[num]=1;
	p[sum++]=num;
	f[num]=!f[num];
	for (int i=0;i<v[num].size();i++)
	{
		if (v[num][i]!=fa&&!check[v[num][i]]) 
		{
			dfs(v[num][i],num);
			p[sum++]=num;
			f[num]=!f[num];
		}
	}
	if (f[num])
	{
		if (fa)
		{
			p[sum++]=fa;
			p[sum++]=num;
			f[fa]=!f[fa];
			f[num]=!f[num];
		}
		else 
		{
			if (sta) sta=0;
			else sta=1;
			f[num]=!f[num];
		}
	}
	return;
}
int main()
{
	int n,m;
	int l,r;
	while (~scanf("%d%d",&n,&m))
	{
		memset(check,0,sizeof(check));
		for (int i=1;i<=n;i++) v[i].clear();
		for (int i=1;i<=m;i++)
		{
			scanf("%d%d",&l,&r);
			v[l].push_back(r);
			v[r].push_back(l);
		}
		for (int i=1;i<=n;i++) scanf("%d",&f[i]);
		sum=0;
		sta=0;
		for (int i=1;i<=n;i++)
		{
			if (f[i])
			{
				dfs(i,0);
				break;
			}
		}
		for (int i=1;i<=n;i++)
		{
			if (f[i]) 
			{
				printf("-1\n");
				goto A;
			}
		}
		printf("%d\n",sum-sta);
		for (int i=sta;i<sum;i++)
		{
			if (i!=sta) printf(" %d",p[i]);
			else printf("%d",p[i]);
		}
		A:;
		printf("\n");
	}
	return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值