CodeForces - 841D Leha and another game about graph
给出一个连通图,并给每个点赋一个d值0或1或-1,要求选出一个边的集合,使得所有的点i要么d[i] == -1,要么
dgree[i] % 2 == d[i],dgree[i]代表i结点的度数。
考虑一条边都不选的情况,此时所有d[i] == 0的i都满足了题目要求,
此时如果有d[i] == 1的点,我们就要加一条边.
我们考虑用dfs维护这个过程,在dfs序形成的搜索树上,若对于某个节点u,其子节点v有d[v] == 1,那么我们就将u和v之间的边的选取状态取反,即原来选变成不选,不选变成选,然后令d[v] = 0,代表该节点已经满足题意(你愿意赋值成其他值也无所谓,注意别弄混就行),
若d[u] != -1,那么d[u]取反(因为u增加了一度,满足题意的状态肯定变化),可以发现,对于每个点,都能通过改变其和其父节点之间边的选取状态而改变其满足题意的状态,只有根节点没有父节点而例外,
因此dfs完成后,如果根节点的状态为d[root] == 1,那么我们就要再选取一个d[i] == -1的结点i,将其到父节点的路径上所有边的选取状态取反,这样只会改变i和root结点的度数,路径上其他结点的度数并不受影响(因此满足题意的状态也不会改变),而对i来说度数改变是无所谓的,因此这样就只将d[root]变成了0。
那么什么时候输出-1呢,显然当不存在d[i] == -1的i并且d[root] == 1的时候
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int> P;
const int MAXN = ;
vector<P> mp[MAXN];
P fa[MAXN];
int d[MAXN];
bool book[MAXN], select[MAXN];
void dfs(int u, int pre)
{
int v, id;
for(int i = ; i < mp[u].size(); i++)
{
v = mp[u][i].first;
id = mp[u][i].second;
if(v == pre || book[v]) continue;
book[v] = ;
fa[v] = P(u, id);
dfs(v, u);
if(d[v] == )
{
d[v] = ;
select[id] ^= ;
if(d[u] != -)
d[u] ^= ;
}
}
}
int main()
{
int n, m, u, v;
int node = , ans = ;
cin >> n >> m;
for(int i = ; i <= n; i++)
{
scanf("%d", d + i);
if(d[i] == -) node = i;
}
for(int i = ; i <= m; i++)
{
scanf("%d %d", &u, &v);
mp[u].push_back(P(v, i));
mp[v].push_back(P(u, i));
}
dfs(, -);
if(d[] == )
{
if(node == )
{
cout << -;
return ;
}
while(node != )
{
select[fa[node].second] ^= ;
node = fa[node].first;
}
}
for(int i = ; i <= m; i++)
if(select[i])
ans++;
cout << ans << endl;
for(int i = ; i <= m; i++)
if(select[i])
printf("%d ", i);
return ;
}
CodeForces - 841D Leha and another game about graph的更多相关文章
- 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 ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- 【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,输出任意方案. [算法]数学+搜索 [题解] 最关 ...
- 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出一个生成树,然后从 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
随机推荐
- VC++获取CPU序列号 CPU ID
CString strCPUID; unsigned long s1, s2; __asm{ mov eax, 01h xor edx, edx cpuid mov s1, edx mov s2, e ...
- slub
1.前言 在Linux中,伙伴系统(buddy system)是以页为单位管理和分配内存.但是现实的需求却以字节为单位,假如我们需要申请20Bytes,总不能分配一页吧!那岂不是严重浪费内存.那么该如 ...
- python MySQLdb 如何设置读超时read_timeout
在python中,经常用到 MySQLdb操作MySQL数据库. 在实现上,MySQLdb并不是纯python的,而是封装了MySQL C API库_mysql. 对于MySQLdb是否支持read_ ...
- Git-Runoob:Git 服务器搭建
ylbtech-Git-Runoob:Git 服务器搭建 1.返回顶部 1. Git 服务器搭建 上一章节中我们远程仓库使用了 Github,Github 公开的项目是免费的,但是如果你不想让其他人看 ...
- ListView 如何提高其效率?
① 复用 ConvertView② 自定义静态类 ViewHolder③ 使用分页加载 ④ 使用 WeakRefrence 引用 ImageView 对象 ViewHolder 为什么要声明为静态类? ...
- win10 点击开始按钮无反应
本人亲身经历 由于安装软件时需要注册表权限,在一顿猛如虎的操作下,将注册表中 HKEY_CURRENT_USER 的权限出问题.而导致无法打开 开始菜单 ----------------以下是本人为了 ...
- linux使用du查看文件夹大小
du命令用来查看目录或文件所占用磁盘空间的大小.常用选项组合为:du –sh -s不显示该目录下面的文件大小,只显示该目录的大小 -h以人类可读的方式显示. 比如显示work下面的Apache-tom ...
- vue按需引入Element UI的方法
在我们的实际项目开发中,多数是采用按需引入的模式来进行开发的,那么具体应该如何操作呢,可能会有许多新人傻傻分不清楚,具体将在下文讲到. 首先我们先vue create demo创建一个项目出来此时我们 ...
- 二叉搜索树倒序O(nlogn)建树
由于在某些糟糕情况下,二叉查找树会退化成链,故而朴素建树过程其复杂度可能会退化成\(O(n^2)\). 采用倒序连边建树的方法可以使得二叉查找树建树复杂度稳定在\(O(nlogn)\). 具体思路如下 ...
- Hive Error : Java heap space 解决方案
Java heap space问题一般解决方案: 设置 set io.sort.mb=10; 排序所使用的内存数量,默认值是100M,和mapred.child.java.opts相对应,opts默认 ...