D - Leha and another game about graph

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

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

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

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

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

不变。

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 3e5 + ;
int d[N];
vector<pii> g[N];
vector<int> ans;
bool vis[N];
void dfs(int u) {
vis[u] = true;
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i].fi;
if(!vis[v]) {
dfs(v);
if(d[v] == ) {
ans.pb(g[u][i].se);
if(d[u] != -) d[u] ^= ;
}
}
}
}
int main() {
int n, m, u, v, sum = , rt = ;
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &d[i]);
for (int i = ; i <= n; i++) {
if(d[i] != -) sum += d[i];
else {
rt = i;
sum = ;
break;
}
}
for (int i = ; i <= m; i++) {
scanf("%d %d", &u, &v);
g[u].pb({v, i});
g[v].pb({u, i});
}
if(sum&) puts("-1");
else {
dfs(rt);
printf("%d\n", (int)ans.size());
for (int i = ; i < ans.size(); i++) printf("%d\n", ans[i]);
}
return ;
}

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. P2114 [NOI2014]起床困难综合症(二进制)

    P2114 [NOI2014]起床困难综合症 我们开始设俩数,一个二进制表示全是1,另一个全是0(就是2147483647 和 0 辣) 蓝后跑一遍门 于是最后有4种情况 1->0,1-> ...

  2. ssh 工具

    ssh 使用 rsa key 实现无密码访问 server A 要用 rsa key 验证的方式访问 server B 在A上创建/root/.ssh/目录 > chmod -R 700 /ro ...

  3. mysql同步之otter/canal环境搭建完整详细版

    接上一篇mysql 5.7多源复制(用于生产库多主库合并到一个查询从库). 这一篇详细介绍otter/canal环境搭建以及当同步出现异常时如何排查.本文主要参考https://blog.csdn.n ...

  4. netty集成ssl完整参考指南(含完整源码)

    虽然我们在内部rpc通信中使用的是基于认证和报文头加密的方式实现安全性,但是有些时候仍然需要使用SSL加密,可能是因为对接的三方系统需要,也可能是由于open的考虑.中午特地测了下netty下集成ss ...

  5. MyBatis 与 Hibernate 到底哪个更快?

    前言 由于编程思想与数据库的设计模式不同,生出了一些ORM框架. 核心都是将关系型数据库和数据转成对象型.当前流行的方案有Hibernate与myBatis. 两者各有优劣.竞争激烈,其中一个比较重要 ...

  6. Centos7编译安装zabbix-4.0.1

    架构组合:nginx1.9.10+php7.0.32+mysql5.7.22+zabbix4.0.1 nginx1.9.10 先装依赖 openssl-1.1.0f tar zxvf openssl- ...

  7. 一元二次方程解法的实现(Python)

    请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程: ax2 + bx + c = 0的两个解. 提示:计算平方根可以调用math.sqrt()函数   # -*- c ...

  8. Codeforces 839A Arya and Bran

    Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going ...

  9. 1.多表查询 => 转化为一张联合大表 2.可视化工具 3.pymysql模块

    多表数据 create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); crea ...

  10. 复旦大学2016--2017学年第二学期高等代数II期末考试情况分析

    一.期末考试成绩班级前十五名 林晨(93).朱民哲(92).何陶然(91).徐钰伦(91).吴嘉诚(91).于鸿宝(91).宁盛臻(90).杨锦文(89).占文韬(88).章俊鑫(87).颜匡萱(87 ...