D - Leha and another game about graph

思路:首先,如果所有点的度数加起来是奇数,且没有-1,那么是不可以的。

其他情况都可以构造,我们先dfs出一个生成树,然后从叶子节点开始往上处理

对于节点u和v,边u -> v,如果d[v]等于1,那么就要选这条边,d[u]取反(改变状态)

这样的话v就可以不用管了,一直这样下去直到根节点,出了根节点,其他点都能满足

如果存在-1,就把-1放在根节点,如果没有的话,以任意节点为根都可以,因为奇偶性

不变。

  1. #pragma GCC optimize(2)
  2. #pragma GCC optimize(3)
  3. #pragma GCC optimize(4)
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. #define fi first
  7. #define se second
  8. #define pi acos(-1.0)
  9. #define LL long long
  10. //#define mp make_pair
  11. #define pb push_back
  12. #define ls rt<<1, l, m
  13. #define rs rt<<1|1, m+1, r
  14. #define ULL unsigned LL
  15. #define pll pair<LL, LL>
  16. #define pii pair<int, int>
  17. #define piii pair<pii, int>
  18. #define mem(a, b) memset(a, b, sizeof(a))
  19. #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  20. #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
  21. //head
  22.  
  23. const int N = 3e5 + ;
  24. int d[N];
  25. vector<pii> g[N];
  26. vector<int> ans;
  27. bool vis[N];
  28. void dfs(int u) {
  29. vis[u] = true;
  30. for (int i = ; i < g[u].size(); i++) {
  31. int v = g[u][i].fi;
  32. if(!vis[v]) {
  33. dfs(v);
  34. if(d[v] == ) {
  35. ans.pb(g[u][i].se);
  36. if(d[u] != -) d[u] ^= ;
  37. }
  38. }
  39. }
  40. }
  41. int main() {
  42. int n, m, u, v, sum = , rt = ;
  43. scanf("%d %d", &n, &m);
  44. for (int i = ; i <= n; i++) scanf("%d", &d[i]);
  45. for (int i = ; i <= n; i++) {
  46. if(d[i] != -) sum += d[i];
  47. else {
  48. rt = i;
  49. sum = ;
  50. break;
  51. }
  52. }
  53. for (int i = ; i <= m; i++) {
  54. scanf("%d %d", &u, &v);
  55. g[u].pb({v, i});
  56. g[v].pb({u, i});
  57. }
  58. if(sum&) puts("-1");
  59. else {
  60. dfs(rt);
  61. printf("%d\n", (int)ans.size());
  62. for (int i = ; i < ans.size(); i++) printf("%d\n", ans[i]);
  63. }
  64. return ;
  65. }

Codeforces 841 D - Leha and another game about graph的更多相关文章

  1. 【CodeForces】841D. Leha and another game about graph(Codeforces Round #429 (Div. 2))

    [题意]给定n个点和m条无向边(有重边无自环),每个点有权值di=-1,0,1,要求仅保留一些边使得所有点i满足:di=-1或degree%2=di,输出任意方案. [算法]数学+搜索 [题解] 最关 ...

  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 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

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

  4. 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的度数为偶数 ...

  5. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  6. 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  ...

  7. Leha and another game about graph CodeForces - 840B (dfs)

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

  8. 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph

    题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...

  9. 【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))

    [题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最 ...

随机推荐

  1. oci7编译报’olog’等未定义的引用问题

    前一天在测试一个数据导出的时候,发现oci7编译的时候报了一大堆类似"’oparse’未定义的引用问题",这通常是因为找不到实现库的原因,但是oci相关的库又都是存在的,用oci7 ...

  2. 苹果笔记本充不进电怎么办_macbook充不进电解决办法

    使用苹果Macbook的用户可能会遇到这种情况,使用一段时间后自己的苹果笔记本充不进电了,虽然充电器指示灯依然亮着,但是电池电脑一直充不进去,断开充电器后就直接关机的情况.通常碰到这种情况,很多用户都 ...

  3. oracle安全应用角色例子

    今天在做看OCP的时候有道题是关于应用安全角色的,不是很明白,在网上找了个例子按照步骤验证了下.QUESTION 48You want to create a role to meet these r ...

  4. win7安装mysql-8.0.13-winx64

    这里展示一下,由于需要安装一个版本测试一下数据,其实就是超简单的啦. 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedi ...

  5. 20165211 2017-2018-2 《Java程序设计》课程总结

    20165211 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告博客链接汇总 预备作业1:我期望的师生关系 预备作业2:学习基础和C语言调查 预备作业3:Li ...

  6. kali linux 64bit 2019.1a下启动bbqsql:No module named coros

    kali linux 64bit 2019.1a下bbqsql启动失败,错误: File "/usr/local/lib/python2.7/dist-packages/bbqsql/lib ...

  7. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  8. Eclipse使用maven命令安装第三方jar包

    使用原因: 使用maven时,有些第三方jar包是不能从maven远程仓库中下载得到,因此导致在pom.xml中添加jar包依赖时会怎么添加都会报错(Missing artifact ojdbc:oj ...

  9. fedora安装了phpmyadmin后, mariadb无法启动?

    参考:http://www.linuxidc.com/Linux/2015-10/123945.htm where, which, when,等不但可以用在从句中, 而且可以用在 动词不定式中, 如: ...

  10. The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)

    The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE) Today I tried ...