Codeforces Round #429 (Div. 2) D. Leha and another game about graph
来源:互联网 发布:淘宝客服是什么意思 编辑:程序博客网 时间:2024/06/08 07:21
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 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
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;}
- Codeforces Round #429 (Div. 2) D. Leha and another game about graph
- Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)
- codeforces 841D Leha and another game about graph
- Codeforces 841D Leha and another game about graph 构造
- Codeforces 841D Leha and another game about graph(dfs)
- codeforces Div.2 #840D Leha and another game about graph 顶点权值为度数对应奇偶
- Codeforces 840B:Leha and another game about graph
- codeforces 840B. Leha and another game about graph(构造,dfs)
- Codeforces Round #429 (Div. 2) 840A Leha and Function(贪心)
- Codeforces Round #429 (Div. 2):C、&840A、 Leha and Function
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- Codeforces Round #304 (Div. 2) D Soldier and Number Game
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- Codeforces Round #304 (Div. 2) D - Soldier and Number Game
- Codeforces Round #304 (Div. 2)D. Soldier and Number Game
- Codeforces Round #280 (Div. 2)-D. Vanya and Computer Game
- C. Leha and Function(Round #429 (Div. 2)
- codeforces Div.2 #429 B.Godsend C. Leha and Function
- xx = [example[x] for example in xxx]功能解析
- webform弹窗子页面简单实例
- 在Kubernetes的3个node上部署redis cluster
- 爬虫正则
- 编码码率控制方法
- Codeforces Round #429 (Div. 2) D. Leha and another game about graph
- caioj.1097 树状数组
- Tensorflow之Variable
- 爬虫表单
- 素数判断
- linux服务器上部署tomcat
- 基于SSM 开发
- 基于docker的CI/CD
- 第一次写博客。。。