Leha and another game about graph CodeForces - 840B (dfs)
大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值
首先要注意到该题只需要考虑dfs树即可, 因为反向边一定不会产生贡献
存在权值为-1的点, 则直接以权值为-1的点为根dfs
若无权值为-1的点, 则答案不一定存在, 任选一个点为根dfs即可
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <vector>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define x first
#define y second
#define pb push_back
using namespace std;
typedef pair<int,int> pii; const int N = 4e5+10, INF = 0x3f3f3f3f;
int a[N], b[N], c[N], vis[N], f[N], n, m, k, t;
vector<pii> g[N]; void dfs(int x) {
if (vis[x]) return;
vis[x]=1;
for (auto e:g[x]) {
dfs(e.x);
if (a[e.x]==1) a[e.x]=0,f[e.y]^=1,a[x]^=1;
}
} int main() {
scanf("%d%d", &n, &m);
int rt = 1;
REP(i,1,n) scanf("%d", a+i),a[i]==-1?rt=i:0;
REP(i,1,m) {
int u, v;
scanf("%d%d",&u,&v);
g[u].pb({v,i}),g[v].pb({u,i});
}
dfs(rt);
if (a[rt]==1) return puts("-1"),0;
int cnt = 0;
REP(i,1,m) cnt += f[i];
printf("%d\n", cnt);
REP(i,1,m) if (f[i]) printf("%d ",i);
puts("");
}
Leha and another game about graph CodeForces - 840B (dfs)的更多相关文章
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - 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 ]
PROBLEM A/_ - Generous Kefa 题 OvO http://codeforces.com/contest/841/problem/A cf 841a 解 只要不存在某个字母,它的 ...
- 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的度数为偶数 ...
- Codeforces 841 D - Leha and another game about graph
D - Leha and another game about graph 思路:首先,如果所有点的度数加起来是奇数,且没有-1,那么是不可以的. 其他情况都可以构造,我们先dfs出一个生成树,然后从 ...
- Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- 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 ...
随机推荐
- Python-sys模块,异常
习题1:题目:给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. #encoding=utf-8 while True: try: num=int(raw_input(&quo ...
- C++ Word Count 发布程序
前段时间,模仿 Linux 系统下的 wc 程序,在 Windows 系统环境下使用 C/C++ 实现了一个相似的 WC 程序,只不过有针对性,针对的是 C/C++,Java 等风格的源代码文件. 此 ...
- Linux基础命令---mke2fs
mke2fs 在磁盘分区上创建ext2.ext3.ext4文件系统,默认情况下会创建ext2.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...
- ACM题目————Sunscreen
Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her ...
- Confluence5.8部分空间名称显示为问号的解决方案
Confluence5.8部分空间名称显示为问号的解决方案 原因: 连接MySQL的时候,有没有在连接串中指定&useUnicode=true&characterEncoding=ut ...
- uboot 网络驱动模型
原文:https://blog.csdn.net/zhouxinlin2009/article/details/45390065 UBOOT的PHYCHIP配置 PHYCHIP的配置位于 includ ...
- SQLSERVER 数据从一张那个表复制到另一张表
insert into 表名1 ( 字段A ,字段B ,字段C) SELECT 字段A ,字段B ,字段C FROM 表名2 (where条件看情况而定)
- C简介与环境配置
C 语言是一种通用的高级语言,最初是由丹尼斯·里奇在贝尔实验室为开发 UNIX 操作系统而设计的.C 语言最开始是于 1972 年在 DEC PDP-11 计算机上被首次实现. 在 1978 年,布莱 ...
- Dubbo学习参考
参考博客: 小宝鸽:https://blog.csdn.net/u013142781/article/details/50387583 https://blog.csdn.net/u013142781 ...
- Netty原理剖析
1. Netty简介 Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作 ...