Leha and another game about graph
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 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.
In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
The first line contains two integers n, m (1 ≤ n ≤ 3·105, n - 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.
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.
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter, deque, defaultdict
from functools import lru_cache, reduce
from heapq import heappush, heappop, heapify, heappushpop, _heappop_max, _heapify_max

def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)

def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        (item, heap[0]) = (heap[0], item)
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

class Graph:

    def __init__(self, V, edges=False, graph=False, directed=False, weighted=False, inf=float('inf')):
        self.V = V
        self.directed = directed
        self.weighted = weighted
        self.inf = inf
        if graph:
            self.graph = graph
            self.edges = []
            for i in range(self.V):
                if self.weighted:
                    for (j, d) in self.graph[i]:
                        if self.directed or (not self.directed and i <= j):
                            self.edges.append((i, j, d))
                else:
                    for j in self.graph[i]:
                        if self.directed or (not self.directed and i <= j):
                            self.edges.append((i, j))
        else:
            self.edges = edges
            self.graph = [[] for i in range(self.V)]
            if weighted:
                for (i, j, d) in self.edges:
                    self.graph[i].append((j, d))
                    if not self.directed:
                        self.graph[j].append((i, d))
            else:
                for (i, j) in self.edges:
                    self.graph[i].append(j)
                    if not self.directed:
                        self.graph[j].append(i)

    def SIV_DFS(self, s, bipartite_graph=False, cycle_detection=False, directed_acyclic=False, euler_tour=False, linked_components=False, lowlink=False, parents=False, postorder=False, preorder=False, subtree_size=False, topological_sort=False, unweighted_dist=False, weighted_dist=False):
        seen = [False] * self.V
        finished = [False] * self.V
        if directed_acyclic or cycle_detection or topological_sort:
            dag = True
        if euler_tour:
            et = []
        if linked_components:
            lc = []
        if lowlink:
            order = [None] * self.V
            ll = [None] * self.V
            idx = 0
        if parents or cycle_detection or lowlink or subtree_size:
            ps = [None] * self.V
        if postorder or topological_sort:
            post = []
        if preorder:
            pre = []
        if subtree_size:
            ss = [1] * self.V
        if unweighted_dist or bipartite_graph:
            uwd = [self.inf] * self.V
            uwd[s] = 0
        if weighted_dist:
            wd = [self.inf] * self.V
            wd[s] = 0
        stack = [(s, 0)] if self.weighted else [s]
        while stack:
            if self.weighted:
                (x, d) = stack.pop()
            else:
                x = stack.pop()
            if not seen[x]:
                seen[x] = True
                stack.append((x, d) if self.weighted else x)
                if euler_tour:
                    et.append(x)
                if linked_components:
                    lc.append(x)
                if lowlink:
                    order[x] = idx
                    ll[x] = idx
                    idx += 1
                if preorder:
                    pre.append(x)
                for y in self.graph[x]:
                    if self.weighted:
                        (y, d) = y
                    if not seen[y]:
                        stack.append((y, d) if self.weighted else y)
                        if parents or cycle_detection or lowlink or subtree_size:
                            ps[y] = x
                        if unweighted_dist or bipartite_graph:
                            uwd[y] = uwd[x] + 1
                        if weighted_dist:
                            wd[y] = wd[x] + d
                    elif not finished[y]:
                        if (directed_acyclic or cycle_detection or topological_sort) and dag:
                            dag = False
                            if cycle_detection:
                                cd = (y, x)
            elif not finished[x]:
                finished[x] = True
                if euler_tour:
                    et.append(~x)
                if lowlink:
                    bl = True
                    for y in self.graph[x]:
                        if self.weighted:
                            (y, d) = y
                        if ps[x] == y and bl:
                            bl = False
                            continue
                        ll[x] = min(ll[x], order[y])
                    if x != s:
                        ll[ps[x]] = min(ll[ps[x]], ll[x])
                if postorder or topological_sort:
                    post.append(x)
                if subtree_size:
                    for y in self.graph[x]:
                        if self.weighted:
                            (y, d) = y
                        if y == ps[x]:
                            continue
                        ss[x] += ss[y]
        if bipartite_graph:
            bg = [[], []]
            for tpl in self.edges:
                (x, y) = tpl[:2] if self.weighted else tpl
                if uwd[x] == self.inf or uwd[y] == self.inf:
                    continue
                if not uwd[x] % 2 ^ uwd[y] % 2:
                    bg = False
                    break
            else:
                for x in range(self.V):
                    if uwd[x] == self.inf:
                        continue
                    bg[uwd[x] % 2].append(x)
        retu = ()
        if bipartite_graph:
            retu += (bg,)
        if cycle_detection:
            if dag:
                cd = []
            else:
                (y, x) = cd
                cd = self.Route_Restoration(y, x, ps)
            retu += (cd,)
        if directed_acyclic:
            retu += (dag,)
        if euler_tour:
            retu += (et,)
        if linked_components:
            retu += (lc,)
        if lowlink:
            retu = (ll,)
        if parents:
            retu += (ps,)
        if postorder:
            retu += (post,)
        if preorder:
            retu += (pre,)
        if subtree_size:
            retu += (ss,)
        if topological_sort:
            if dag:
                tp_sort = post[::-1]
            else:
                tp_sort = []
            retu += (tp_sort,)
        if unweighted_dist:
            retu += (uwd,)
        if weighted_dist:
            retu += (wd,)
        if len(retu) == 1:
            retu = retu[0]
        return retu
(N, M) = map(int, readline().split())
D = list(map(int, readline().split()))
edges = []
idx = {}
for i in range(M):
    (u, v) = map(int, readline().split())
    u -= 1
    v -= 1
    edges.append((u, v))
    idx[u, v] = i
    idx[v, u] = i
G = Graph(N, edges=edges)
(parents, tour) = G.SIV_DFS(0, parents=True, postorder=True)
if D.count(1) % 2:
    for i in range(N):
        if D[i] == -1:
            D[i] = 1
            break
for i in range(N):
    if D[i] == -1:
        D[i] = 0
if D.count(1) % 2:
    print(-1)
    exit()
ans_lst = []
for x in tour[:-1]:
    if D[x]:
        ans_lst.append(idx[x, parents[x]] + 1)
        D[x] ^= 1
        D[parents[x]] ^= 1
print(len(ans_lst))
if ans_lst:
    print(*ans_lst, sep='\n')
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
d = list(map(int, input().split()))
g = {}

def push(g, u, v, i):
    if u not in g:
        g[u] = []
    if v not in g:
        g[v] = []
    g[u].append([v, i + 1])
    g[v].append([u, i + 1])
for i in range(m):
    (u, v) = map(int, input().split())
    push(g, u - 1, v - 1, i)

def solve(d, g):
    pos = []
    S = 0
    for (i, x) in enumerate(d):
        if x == -1:
            pos.append(i)
        else:
            S += x
    if S % 2 == 1:
        if len(pos) == 0:
            return (False, None)
        d[pos[0]] = 1
        for x in pos[1:]:
            d[x] = 0
    else:
        for x in pos:
            d[x] = 0
    root = 0
    S = [root]
    edge = [-1]
    used = [0] * n
    used[root] = 1
    p = [None] * n
    i = 0
    while i < len(S):
        u = S[i]
        if u in g:
            for (v, edge_) in g[u]:
                if v == p[u] or used[v] == 1:
                    continue
                p[v] = u
                used[v] = 1
                S.append(v)
                edge.append(edge_)
        i += 1
    ans = []
    r = [0] * n
    for (v, edge_) in zip(S[1:][::-1], edge[1:][::-1]):
        if r[v] ^ d[v] == 1:
            u = p[v]
            r[u] = 1 - r[u]
            ans.append(str(edge_))
    return (True, ans)
(flg, ans) = solve(d, g)
if flg == False:
    print(-1)
else:
    print(len(ans))
    print('\n'.join(ans))
import sys
(n, m) = map(int, sys.stdin.readline().split())
d = list(map(int, sys.stdin.readline().split()))
gph = [[] for _ in range(n)]
for _ in range(m):
    (u, v) = map(int, sys.stdin.readline().split())
    u -= 1
    v -= 1
    gph[u].append((v, _))
    gph[v].append((u, _))
t = -1
if d.count(1) % 2 == 1:
    if -1 not in d:
        print(-1)
        exit(0)
    t = d.index(-1)
ans = [False] * m
vis = [False] * n
ed = [(-1, -1)] * n
rets = [d[u] == 1 or u == t for u in range(n)]
stk = [[0, iter(gph[0])]]
while len(stk) > 0:
    u = stk[-1][0]
    vis[u] = True
    try:
        while True:
            (v, i) = next(stk[-1][1])
            if not vis[v]:
                ed[v] = (u, i)
                stk.append([v, iter(gph[v])])
                break
    except StopIteration:
        (p, e) = ed[u]
        if p >= 0 and rets[u]:
            rets[p] = not rets[p]
            ans[e] = True
        stk.pop()
        pass
print(ans.count(True))
print('\n'.join([str(i + 1) for i in range(m) if ans[i]]))