Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)
来源:互联网 发布:sublimetext2 python 编辑:程序博客网 时间:2024/06/15 19:03
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.
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.
1 01
-1
4 50 0 0 -11 22 33 41 42 4
0
2 11 11 2
11
3 30 -1 11 22 31 3
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;}
- Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)
- Codeforces Round #429 (Div. 2) D. Leha and another game about graph
- Codeforces 841D Leha and another game about graph(dfs)
- Codeforces 840B:Leha and another game about graph
- codeforces 840B. Leha and another game about graph(构造,dfs)
- codeforces 841D Leha and another game about graph
- Codeforces 841D Leha and another game about graph 构造
- codeforces Div.2 #840D Leha and another game about graph 顶点权值为度数对应奇偶
- Codeforces Round #390 (Div. 2) B. Ilya and tic-tac-toe game(dfs)
- codeforces Div.2 #429 B.Godsend C. Leha and Function
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
- Codeforces Round #429 (Div. 2) 840A Leha and Function(贪心)
- Codeforces Round #429 (Div. 2):C、&840A、 Leha and Function
- Codeforces Round #268 (Div. 1)B(dfs)
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)-B. Appleman and Card Game
- Codeforces Round #267 (Div. 2) B. Fedor and New Game
- 史上最全的软件工程师资料收集
- 网络判断
- OpenStack dashboard + Apache2.4.27 + HTTPS
- 基于react技术栈的单页应用(SPA)搭建_快速入门实践
- 岛国电影生物科普就是强—生物大跃进
- Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)
- python高级应用-twisted框架
- Hbase入门之三HBase Client API使用入门
- Unity3D
- Java 创建类报错
- Linux driver之input framework
- python(5)future模块
- BZOJ1046 洛谷 P2215 [HAOI2007]上升序列
- 算法竞赛入门经典java版程序CH3 uva1587