链接

大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值

首先要注意到该题只需要考虑dfs树即可, 因为反向边一定不会产生贡献

存在权值为-1的点, 则直接以权值为-1的点为根dfs

若无权值为-1的点, 则答案不一定存在, 任选一个点为根dfs即可

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <math.h>
  4. #include <cstdio>
  5. #include <vector>
  6. #define REP(i,a,n) for(int i=a;i<=n;++i)
  7. #define x first
  8. #define y second
  9. #define pb push_back
  10. using namespace std;
  11. typedef pair<int,int> pii;
  12.  
  13. const int N = 4e5+10, INF = 0x3f3f3f3f;
  14. int a[N], b[N], c[N], vis[N], f[N], n, m, k, t;
  15. vector<pii> g[N];
  16.  
  17. void dfs(int x) {
  18. if (vis[x]) return;
  19. vis[x]=1;
  20. for (auto e:g[x]) {
  21. dfs(e.x);
  22. if (a[e.x]==1) a[e.x]=0,f[e.y]^=1,a[x]^=1;
  23. }
  24. }
  25.  
  26. int main() {
  27. scanf("%d%d", &n, &m);
  28. int rt = 1;
  29. REP(i,1,n) scanf("%d", a+i),a[i]==-1?rt=i:0;
  30. REP(i,1,m) {
  31. int u, v;
  32. scanf("%d%d",&u,&v);
  33. g[u].pb({v,i}),g[v].pb({u,i});
  34. }
  35. dfs(rt);
  36. if (a[rt]==1) return puts("-1"),0;
  37. int cnt = 0;
  38. REP(i,1,m) cnt += f[i];
  39. printf("%d\n", cnt);
  40. REP(i,1,m) if (f[i]) printf("%d ",i);
  41. puts("");
  42. }

Leha and another game about graph CodeForces - 840B (dfs)的更多相关文章

  1. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  2. 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 ]

    PROBLEM A/_ - Generous Kefa 题 OvO http://codeforces.com/contest/841/problem/A cf 841a 解 只要不存在某个字母,它的 ...

  3. Codeforces Round #429 (Div. 2) - D Leha and another game about graph

    Leha and another game about graph 题目大意:给你一个图,每个节点都有一个v( -1 , 0 ,1)值,要求你选一些边,使v值为1 的点度数为奇数,v值为0的度数为偶数 ...

  4. Codeforces 841 D - Leha and another game about graph

    D - Leha and another game about graph 思路:首先,如果所有点的度数加起来是奇数,且没有-1,那么是不可以的. 其他情况都可以构造,我们先dfs出一个生成树,然后从 ...

  5. Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列

    Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...

  6. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  7. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

  8. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  9. Codeforces 841D 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  ...

随机推荐

  1. [转载] My97DatePicker日历实现开始日期小于结束日期验证

    <tr align='center'> <td align="right">开始日期: </td> <td align="lef ...

  2. Php cli模式下执行报错/usr/bin/php: /usr/local/lib/libxml2.so.2: no version information available (required by /usr/bin/php)

    centos下php cli模式报错 /usr/bin/php: /usr/local/lib/libxml2.so.2: no version information available (requ ...

  3. windows 上安装redis和windows上redis与php扩展

    1.下载redis压缩包(自己选择想要的版本,1,2地址任意选一个) 下载window版本地址1:https://github.com/dmajkic/redis/downloads 下载window ...

  4. [Linux 003]——用户和用户组以及 Linux 权限管理(一)

    嗬!没想到吧!学习 Linux 的第三天,我们已经开始接触用户管理,用户组管理,以及权限管理这几个逼格满满的关键字.这几个关键字对于前端程序猿的我来说真的是很高大上有木有,以前尝试学 Linux 的时 ...

  5. 20145327《网络对抗》——注入shellcode并执行和Return-to-libc攻击深入

    20145327<网络对抗>--注入shellcode并执行 准备一段Shellcode 老师的shellcode:\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68 ...

  6. 0x30、0x37

    1.write_date(0x30+shi)加0x30是什么意思 答: 将数字0-9转化为字符'0'-'9' 1.write_date(0x37+bai)加0x37是什么意思 答: 将大于9的数字转化 ...

  7. Duilib应用修改程序图标方法(转载)

    转载:http://www.cnblogs.com/lanzhi/p/6468596.html 本文向大家介绍如何修改duilib应用图标,对于win32或者mfc应用来说,我们可以在注册窗口类时指定 ...

  8. POJ 2029 (二维树状数组)题解

    思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...

  9. session的理解和使用

    Session的使用与Session的生命周期 1.HttpSession的方法 Object getAttribute(String); Enumeration<String> getA ...

  10. URL重写与URL路由

    要介绍这两个内容,必须要从ASP.NET管线说起. ASP.NET管线 管线(Pipeline)这个词形象地说明了每个Asp.net请求的处理过程: 请求是在一个管道中,要经过一系列的过程点,这些过程 ...