链接

大意: 给定无向连通图, 每个点有权值$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)的更多相关文章

  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. linux常用命令:find 命令概览

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  2. mybatis插入语句空值没有设置jdbcType报错

    Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: Error setti ...

  3. 手撕vue-cli配置——webpack.prod.conf.js篇

    'use strict' const path = require('path') const utils = require('./utils') const webpack = require(' ...

  4. jdbc连接池c3p0/dbcp强制连接超过设置时间后失效

    通常来说,各种技术实现的优化参数或者选项或者歪门邪道之所以能被想出来,通常是因为开发者或者实现的贡献者曾经遇到过导致此结果的问题,所以才出了对应的策略选项. 在有些情况下,比如存在客户端或者服务端连接 ...

  5. 20145322 Exp5 利用nmap扫描

    20145322 Exp5 利用nmap扫描 实验过程 使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsole开启m ...

  6. CSAPP 第三章 读书笔记

    程序的机器级表示 AT&T与Intel格式的汇编代码 我们的表述是ATT(根据"AT&T"命名的, AT&T是运营贝尔实验室多年的公 司)格式的汇编代码,这 ...

  7. ubuntu搭建discuz论坛

    a.安装mysql database 1.安装mysql服务端 sudo apt-get install mysql-server  (在此过程中要求为mysql的root用户设置一个密码) 2.安装 ...

  8. SpringBoot中的Quartz应用

    Spring自带定时器任务: code: import org.springframework.beans.factory.annotation.Configurable; import org.sp ...

  9. 格子中输出|2015年蓝桥杯B组题解析第四题-fishers

    StringInGrid函数会在一个指定大小的格子中打印指定的字符串. 要求字符串在水平.垂直两个方向上都居中. 如果字符串太长,就截断. 如果不能恰好居中,可以稍稍偏左或者偏上一点. 下面的程序实现 ...

  10. [bzoj 1270][BeijingWc2008]雷涛的小猫

    Description 雷涛的小猫雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样的行为是违反学 生宿舍管理条例的).  在他的照顾下,小猫很快恢复了健康,并且愈发的活泼可 ...