Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)

来源:互联网 发布:sublimetext2 python 编辑:程序博客网 时间:2024/06/15 19:03

B. 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 m edges. 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 01
output
-1
input
4 50 0 0 -11 22 33 41 42 4
output
0
input
2 11 11 2
output
11
input
3 30 -1 11 22 31 3
output
12


题意:

n个点m条无向边,有重边但不能有自环,每个点有一个权值-1,1or0

现在让你保留一些边,使得整张图的所有点满足①权值为0的点度数必须是偶数;②权值为1的点度数必须是奇数

问有没有合法情况,没有输出-1,否则输出任意一种(第一个数字是边的数量,后面若干个边的编号)


当且仅当图中没有-1的点,并且权值为1的点个数为奇数时无解,否则都有解

如果某个点是-1,那么所有与该点相连的点都可以通过连不连这条边来调整合法性

并且这个性质可以传递

也就是说只要从某个-1的点开始一套乱搜就好了(嘿嘿)

如果没有-1的点,那么1点的个数为偶数,从任何一个点开始搜最后一定可以合法


#include<stdio.h>#include<vector>using namespace std;typedef struct{int v;int id;}Road;Road now;vector<Road> G[300005];int val[300005], use[300005], vis[300005];int Sech(int u){int i, cur;Road temp;vis[u] = 1;cur = val[u];for(i=0;i<G[u].size();i++){temp = G[u][i];if(vis[temp.v])continue;if(Sech(temp.v)){cur ^= 1;use[temp.id] = 1;}}if(val[u]==-1)return 0;return cur;}int main(void){int n, m, i, c, d, x, y, ans;scanf("%d%d", &n, &m);c = d = 0;for(i=1;i<=n;i++){scanf("%d", &val[i]);if(val[i]==-1)  c = i;else  d ^= val[i];}if(d==1 && c==0){printf("-1\n");return 0;}for(i=1;i<=m;i++){scanf("%d%d", &x, &y);now.id = i, now.v = y;G[x].push_back(now);now.v = x;G[y].push_back(now);}Sech(max(c, 1));ans = 0;for(i=1;i<=m;i++){if(use[i])ans++;}printf("%d\n", ans);for(i=1;i<=m;i++){if(use[i])printf("%d\n", i);}return 0;}


阅读全文
1 0
原创粉丝点击