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 ...
随机推荐
- Mac下破解百度网盘限速(Chrome + Aria2GUI)
基本原理是利用Aria2GUI的多线程下载来达到提速的目的,具体步骤如下: 1.下载Aria2GUI客户端(注意,客户端文件要放入‘应用程序’,否则会报错),使用时注意修改线程数,默认为16,不够用, ...
- Linux命令之grep用法详解:grep与正则表达式 [转]
正则表达式与通配符不一样,它们表示的含义并不相同. grep命令的选项用于对搜索过程进行补充说明.grep命令的模式十分灵活,可以是字符串.变量,还可以是正则表达式. 无论模式是何种形式,只要模式中包 ...
- hash模块MD5加密
MD5加密:获取32位加密字符串: 示例(MD5加密'123456')import hashlibhashObject=hashlib.md5(b'123456') #实例化,加密字符串不能直接加密, ...
- Java学习之==>泛型
一.什么是泛型 泛型,即“参数化类型”,在不创建新的类型的情况下,通过泛型指定的不同类型来控制形参具体限制的类型.也就是说在泛型使用过程中,操作的数据类型被指定为一个参数,这种参数类型可以用在类.接口 ...
- Metinfo5.1 /member/getpassword.php SQL注入
- tableau desktop
参考: 入门指南: https://help.tableau.com/current/guides/get-started-tutorial/zh-cn/get-started-tutorial-co ...
- etcd api常用操作
如果需要使用v2 version api,启动etcd时候需要加入“ETCD_ENABLE_V2=true”参数,否则会报错“404 page not found” 获取etcd信息 版本信息 # c ...
- Python_2day
选择 布尔类型.数值和表达式 注意:比较运算符的相等是两个等号,一个等到代表赋值 在Python中可以用整型0来代表False,其他数字来代表True 后面还会讲到 is 在判断语句中的用发 In [ ...
- Callable+ThreadPoolExecutor实现多线程并发并获得返回值(转)
出处:https://blog.csdn.net/kity9420/article/details/80740466 前言 经常会遇到一些性能问题,比如调用某个接口,可能要循环调用100次,并且需要拿 ...
- 看电视剧<潜伏>有感
前几天看了老电视剧-潜伏,有一些感慨. 一,立场和真相都不重要,形式才是最重要的. 二.历史在不断的轮回中. 好汉历经千辛万苦杀掉了为害一方的恶霸,好汉的威望达到了顶峰,自然的成了村庄的守护者和掌控者 ...