Codeforces Round #429 (Div. 2) D. Leha and another game about graph

时间:2023-02-02 19:19:17

D. Leha and another game about graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Leha plays a computer game, where is on each level is given a connected graph with n vertices and medges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 01 or  - 1. To pass the level, he needs to find a «good» subset of edges of the graph or say, that it doesn't exist. Subset is called «good», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, di =  - 1 or it's degree modulo 2 is equal to di. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them.

Input

The first line contains two integers nm (1 ≤ n ≤ 3·105n - 1 ≤ m ≤ 3·105) — number of vertices and edges.

The second line contains n integers d1, d2, ..., dn ( - 1 ≤ di ≤ 1) — numbers on the vertices.

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) — edges. It's guaranteed, that graph in the input is connected.

Output

Print  - 1 in a single line, if solution doesn't exist. Otherwise in the first line k — number of edges in a subset. In the next k lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1.

Examples
input
1 0
1
output
-1
input
4 5
0 0 0 -1
1 2
2 3
3 4
1 4
2 4
output
0
input
2 1
1 1
1 2
output
1
1
input
3 3
0 -1 1
1 2
2 3
1 3
output
1
2
Note

In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.



一个连通图,要求选择一些边,使得每个点的度数的奇偶性符合所给的要求。


首先,每条边带来两个度数,所以要求总度数为奇数是不可能的。

这样总度数就为偶数。

对于这样的一个连通图,先dfs求出任意一个生成树,再从叶子向上推。

对于生成树的每条边,按照后dfs到的点的奇偶性要求,判断是否取这条边。最后就能构造出一个符合要求的图。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=300005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);  
int head[maxn],d[maxn];
bool visit[maxn],f[maxn];
int num,sum;

struct Edge {
	int from,to,pre;
};
Edge edge[maxn*2];

void addedge(int from,int to) {
	edge[num]=(Edge){from,to,head[from]};
	head[from]=num++;
	edge[num]=(Edge){to,from,head[to]};
	head[to]=num++;
}

void dfs(int now,int pre,int fa) {
	visit[now]=1;
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!visit[to]) dfs(to,i,now);
	}
	if (now==1||d[now]==0) return;
	sum++;
	f[pre/2+1]=1;
	d[fa]=(d[fa]+1)%2;
}

int main() {
	int n,i,j,m,x,y,cnt=0;
	sum=0;
	scanf("%d%d",&n,&m);
	num=0;
	memset(head,-1,sizeof(head));
	for (i=1;i<=n;i++) {
		scanf("%d",&d[i]);
		if (d[i]==-1) cnt++; else sum+=d[i];
	}
	for (i=1;i<=m;i++) {
		scanf("%d%d",&x,&y);
		addedge(x,y);
	}
	if (sum%2&&cnt==0) {
		cout << -1;return 0;
	} else {
		for (i=1;i<=n;i++) 
			if (d[i]==-1) 
				if (sum%2) d[i]=1,sum++; else d[i]=0;
	}
	mem0(visit);
	mem0(f);
	sum=0;
	dfs(1,-1,-1);
	printf("%d\n",sum);
	for (i=1;i<=m;i++) 
		if (f[i]) printf("%d ",i);
	return 0;
}