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 ...
随机推荐
- leetcode 64. 最小路径和Minimum Path Sum
很典型的动态规划题目 C++解法一:空间复杂度n2 class Solution { public: int minPathSum(vector<vector<int>>&am ...
- leetcode 454 四数相加
采用一个哈希表存储两个数的和,再遍历另外两个数组的和,time O(n2) space O(n2) class Solution { public: int fourSumCount(vector&l ...
- phpmyadmin python mysql全部正常显示中文的关键
1. 建表.列时在phpmyadmin中将编码设置为utf8_general_ci 2. python中使用sql连接时设定charset为utf8,注意不能是utf-8! 例如: def Conne ...
- BCNF/3NF 数据库设计范式简介
数据库设计有1NF.2NF.3NF.BCNF.4NF.5NF.从左往右,越后面的数据库设计范式冗余度越低. 满足后一个设计范式也必定满足前一个设计范式. 1NF只要求每个属性是不可再分的,基本每个数据 ...
- python中_new_()与_init_()的区别
__new__方法的使用 只有继承于object的新式类才能有__new__方法,__new__方法在创建类实例对象时由Python解释器自动调用,一般不用自己定义,Python默认调用该类的直接父类 ...
- Linux_RHEL7_YUM
目录 目录 前言 RPM rpm常用指令 YUM yum常用指令RHEL7 最后 前言 yum:yellow dog updater modifier(黄狗包管理器),是RHEL默认的基于RPM包的软 ...
- 阶段3 2.Spring_01.Spring框架简介_05.spring的优势
- ntp同步报错解决
服务端:192.168.1.204 主机名: www.test.com 客户端:192.168.1.206 主机名: www.test3.com 客户端同步服务端报错如下: [root@www etc ...
- 采用WPF技术开发截图程序
前言 QQ.微信截图功能已很强大了,似乎没必要在开发一个截图程序了.但是有时QQ热键就是被占用,不能快速的开启截屏:有时,天天挂着QQ,领导也不乐意.既然是程序员,就要自己开发截屏工具,功能随心所欲 ...
- Pytorch笔记 (2) 初识Pytorch
一.人工神经网络库 Pytorch ———— 让计算机 确定神经网络的结构 + 实现人工神经元 + 搭建人工神经网络 + 选择合适的权重 (1)确定人工神经网络的 结构: 只需要告诉Pytorc ...