Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)
2017-08-19 11:14
399 查看
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 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.
Input
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.
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
output
input
output
input
output
input
output
题意:
n个点m条无向边,有重边但不能有自环,每个点有一个权值-1,1or0
现在让你保留一些边,使得整张图的所有点满足①权值为0的点度数必须是偶数;②权值为1的点度数必须是奇数
问有没有合法情况,没有输出-1,否则输出任意一种(第一个数字是边的数量,后面若干个边的编号)
当且仅当图中没有-1的点,并且权值为1的点个数为奇数时无解,否则都有解
如果某个点是-1,那么所有与该点相连的点都可以通过连不连这条边来调整合法性
并且这个性质可以传递
也就是说只要从某个-1的点开始一套乱搜就好了(嘿嘿)
如果没有-1的点,那么1点的个数为偶数,从任何一个点开始搜最后一定可以合法
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 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.
Input
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.
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 -11 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
题意:
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; }
相关文章推荐
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
- 【Codeforces Round #429 (Div. 1) B】Leha and another game about graph
- Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]
- Codeforces Round #429 (Div. 2) D. Leha and another game about graph
- codeforces Div.2 #840D Leha and another game about graph 顶点权值为度数对应奇偶
- DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas
- Codeforces Round #228 (Div. 2) B Fox and Cross(DFS)
- Codeforces Round #419 (Div. 2) C.Karen and Game 思维
- Codeforces Round #267 (Div. 2) B. Fedor and New Game(位运算)
- Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数论 筛表法
- AIM Tech Round (Div. 2) C. Graph and String(二分图染色 | 贪心)
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 数学
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
- Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
- hdu 4647 Another Graph Game 博弈
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game【交互题+BFS】
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
- 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons