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 ...
随机推荐
- 关于eclipse安装插件的问题
使用eclipse(luna)不行,其为安装了ee有关插件的 使用eclipse(Juno)可以,其为原生版本
- 清除表单input输入框内数据
清除表单input输入框内数据 1. $(':input','#addVoucherType') //'#addVoucherType'表单id .not(':button') .val('') .r ...
- 【ABAP系列】SAP BOM反查
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP BOM反查 前言部分 ...
- P2951 【[USACO09OPEN]捉迷藏Hide and Seek】
典型的最短路,而且只要再加一点点操作,就能得到答案 所以可以直接套模板 具体看程序:: #include<cstdio> #include<queue>//队列专属头文件 #i ...
- HCL试验2
PC端配置:配置ip地址 交换机1配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-type a ...
- mysql——单表查询——其它整理示例00
), sname ), sage ), ssex ) ); ','zhaolei','1990-01-01','nan'); ','qiandian','1990-12-21','nan'); ',' ...
- java 依据文件名判断mime类型
依据文件名称判断mime类型 import java.util.HashMap; import java.util.Map; /** * 依据文件名获取MimeType */ public class ...
- CDH的ntp时间同步
云服务器: ntpq -p ntpdate -u 10.52.255.1 #手动同步 自建NTP服务器: https://www.cnblogs.com/yinzhengjie/p/9480665. ...
- start-all.sh启动HDFS,datanode没有启动
第一次格式化dfs后,启动并使用hadoop,之后如果再次重新执行了格式化(hdfs namenode -format) start-all.sh启动时就会遇到datanode无法启动的问题,通常情况 ...
- 滑雪(dp或记忆化搜索)
题意:给你一个二维数组,求最长的递减路线的长度,只能向四个方向延伸. 解法1.dp[i][j]以i.j结尾的最长路线长度.边界:每个数初值为1, 转移:从四周向i.j转移,if(a[i][j]> ...