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 1)
题意:
选择一个边集合,满足某些点度数的奇偶性
分析:
将d = 1的点连成一颗树,不在树上的点都不连边。
可以发现,当某个节点u的所有子节点si均可操控 (u, si) 来满足自身要求
即整棵树上至多只有1个点不满足自身要求,就是根节点,此时需要在树中任意位置接入 d=-1 的一个节点 然后研究如何在这棵树上选边,考虑增量法
每次选择两个d=1的点加入树中,并将这棵树上两点间路径上所有的边选择状态反转
易证对于新加入的节点满足要求,而路径上原有节点仍满足要求
最后若只剩一个d=1的节点,则和一个d=-1的节点组成一对
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
struct Edge {
int v, i;
};
vector<Edge> edge[N];
vector<int> c1, c2;
int n, m;
int d[N];
bool vis[N], flag[N];
vector<int> ans;
void dfs(int u)
{
vis[u] = 1;
for (auto& e : edge[u])
{
if (vis[e.v]) continue;
dfs(e.v);
if (flag[e.v]) ans.push_back(e.i);
flag[u] ^= flag[e.v];
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &d[i]);
if (d[i] == 1) c1.push_back(i);
else if (d[i] == -1) c2.push_back(i);
}
for (int i = 1; i <= m; i++)
{
int u, v; scanf("%d%d", &u, &v);
edge[u].push_back(Edge{v, i});
edge[v].push_back(Edge{u, i});
}
if (c1.size()%2 && c2.empty())
{
puts("-1"); return 0;
}
int k = c1.size()/2;
for (int i = 0; i < k; i++)
{
flag[c1[i]] = flag[c1[k+i]] = 1;
}
if (c1.size() % 2)
{
flag[c1[c1.size()-1]] = flag[c2[0]] = 1;
}
dfs(1);
printf("%d\n", ans.size());
for (auto& x : ans) printf("%d ", x);
puts("");
}
CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)的更多相关文章
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- 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 Round #429 (Div. 2) 补题
A. Generous Kefa 题意:n个气球分给k个人,问每个人能否拿到的气球都不一样 解法:显然当某种气球的个数大于K的话,就GG了. #include <bits/stdc++.h> ...
- Leha and another game about graph CodeForces - 840B (dfs)
链接 大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值 首先要注 ...
- 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 ...
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-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,输出任意方案. [算法]数学+搜索 [题解] 最关 ...
随机推荐
- linux下安装lnmp集成环境
linux下安装lnmp集成环境 教程地址:https://www.cnblogs.com/peteremperor/p/6750204.html 必须要用root用户,否则权限不够无法安装 安装最新 ...
- java-TheadPoolExecutor
Executor的两极调度模型 第一级:java多线程程序把应用分解为若干个任务,然后使用用户级的调度器(Executor框架)将这些任务映射为固定数量的线程: 第二级:操作系统内核将这些线程映射到处 ...
- 用c++ sttring检测名字是否有空格
name.find(' ') == std::string::npos//npos==-1,表示没找到
- golang跨平台编译
// 目标平台linux 64 SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build //目标平台windows SET CGO_ENA ...
- linux时间同步ntpdate
1.安装ntpdate,执行以下命令 yum install ntpdate -y 2.手工同步网络时间,执行以下命令,将从time.nist.gov同步时间 ntpdate 0.asia.pool. ...
- IDEA忽略不必要提交的文件
1.在idea中安装插件用来生成和管理 .gitignore 文件,安装成功后重启idea 2.新建.gitignore 文件 3.将不需要提交的文件添加到.gitignore 4.删除缓冲文件 . ...
- (六)lucene之其他查询方式(组合查询,制定数字范围、指定字符串开头)
本章使用的是lucene5.3.0 指定数字范围查询 package com.shyroke.test; import java.io.IOException; import java.nio.fil ...
- (一)Lucene简介以及索引demo
一.百度百科 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查 ...
- 动手实现CNN卷积神经网络
数据集采用的是手写数据集(http://yann.lecun.com/exdb/mnist/): 本文构建的CNN网络图如下: 像素点:28*28 = 784,55000张手写数字图片. # -*- ...
- Python练习_函数进阶_day10
1. 1.作业 1,写函数,接收n个数字,求这些参数数字的和.(动态传参) 2,读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么? a=10 b=20 def test5(a,b): p ...