[codeforces] 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.

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.

Examples
input
Copy
3 2
1 2
2 3
1 1 1
output
Copy
3
1 2 3
input
Copy
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
output
Copy
10
2 1 3 4 5 4 5 4 3 1 
input
Copy
2 0
0 0
output
Copy
0

代码:

#include<iostream>
#include<vector>
#include<string.h>
using namespace std;

vector<int> e[100000];
int time[100000];
int visit[100000];
int res[400000];
int n;
int m;
int ans = 0;

void dfs(int index){
	time[index] = 1 - time[index];
	res[ans++] = index;
	
	visit[index] = 1;
	for(int i = 0; i < e[index].size(); i++){
		
		if(visit[e[index][i]]) continue;
		
		dfs(e[index][i]);
		time[index] = 1 - time[index];
		res[ans++] = index;
		
		if(time[e[index][i]]){
			time[e[index][i]] = 1 - time[e[index][i]];
			res[ans++] = e[index][i];
			time[index] = 1 - time[index];
			res[ans++] = index;
		}
	}
}

int main(void){
	int start = 0;
	memset(visit, 0, sizeof(visit));
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int s;
		int end;
		cin >> s >> end;
		e[s].push_back(end);
		e[end].push_back(s);
	}
	for(int i = 1; i <= n; i++){
		cin >> time[i];
		if(time[i] == 1) start = i;
	}
	dfs(start);
	if(time[start]){
		time[start] = 0;
		ans--;
	}
	int flag = 1;
	for(int i = 1; i <= n; i++){
		if(time[i]){
			flag = 0;
			break;
		}
	}
	if(flag){
		cout << ans << endl;
		if(ans != 0){
			for(int i = 0; i < ans; i++){
				cout << res[i] << " ";
			}
		}
	}
	else cout << "-1\n";
	return 0;
} 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值