【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0。如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值。让你输出一个留边的方案。
首先如果图内有-1,那么必有解。否则如果初始不合法的点数为偶数,那么必有解,否则无解。因为删一条边,要么使图中不合法的点数+2,要么不变,要么-2。
如果有解,构造图的任意一个生成树,如果有-1,就让-1为根,否则任意结点为根。然后从叶子向根定每个点的入度数,由于自底向上,一个结点的儿子边都被处理完后,只需要决定父边是否删除即可。可以想见,根节点不用判,必然合法(前提我们已经判断其有解;如果无解,当然根节点就无法合法咯)。
实际操作时,不用构造生成树,因为DFS,用DFS树即可。
#include<cstdio>
#include<algorithm>
using namespace std;
bool vis[300005];
int v[600005],next[600005],first[300005],e,id[600005];
void AddEdge(int U,int V,int ID){
v[++e]=V;
id[e]=ID;
next[e]=first[U];
first[U]=e;
}
int n,m,d[300005],du[300005],anss[300005],ans;
bool cho[300005];
void dfs(int U,int fa,int fa_edge){
vis[U]=1;
int cnt=0;
for(int i=first[U];i;i=next[i]){
if(!vis[v[i]]){
dfs(v[i],U,id[i]);
if(cho[id[i]]){
++cnt;
}
}
}
if(d[U]!=-1 && cnt%2!=d[U]){
cho[fa_edge]=1;
anss[++ans]=fa_edge;
}
}
int main(){
int x,y;
//freopen("b.in","r",stdin);
int fu1_node=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%d",&d[i]);
if(d[i]==-1){
fu1_node=i;
}
}
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
AddEdge(x,y,i);
AddEdge(y,x,i);
++du[x];
++du[y];
}
int cnt=0;
for(int i=1;i<=n;++i){
if(d[i]!=-1 && du[i]%2!=d[i]){
++cnt;
}
}
if(fu1_node){
dfs(fu1_node,0,0);
}
else if(cnt%2==0){
dfs(1,0,0);
}
else{
puts("-1");
return 0;
}
sort(anss+1,anss+ans+1);
printf("%d\n",ans);
for(int i=1;i<ans;++i){
printf("%d ",anss[i]);
}
if(ans){
printf("%d\n",anss[ans]);
}
return 0;
}
【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph的更多相关文章
- 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 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad
题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...
- DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas
题目传送门 /* DFS:按照长度来DFS,最后排序 */ #include <cstdio> #include <algorithm> #include <cstrin ...
- 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) 补题
A. Generous Kefa 题意:n个气球分给k个人,问每个人能否拿到的气球都不一样 解法:显然当某种气球的个数大于K的话,就GG了. #include <bits/stdc++.h> ...
- dfs Codeforces Round #356 (Div. 2) D
http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...
随机推荐
- 如何彻底关闭退出vmware虚拟机
如何彻底关闭退出vmware虚拟机 每次使用虚拟机之后退出时,它都会在系统托盘区留下一个虚拟机图标,该如何彻底关闭退出vmware虚拟机呢? 首先我们需要运行一下虚拟机程序 1:我们如果要对虚拟机进行 ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- Redis -- 过期时间 和 缓存 例子
1.设置 key的生存时间,过期自动删除 exprire key seconds 设置过期时间 秒数 ttl key 查询剩余时间 如果 设置了过期时间.对key进行 set 操作,会清除 ...
- linux下不解包查看tar包文件内容
为减少日志文件占用的空间,很多情况下我们会将日志文件以天或周为周期打包成tar.gz 包保存.虽然这样做有利空间充分利用,但当我们想查看压缩包内的内容时确很不方便.如果只是一个tar.gz文件,可以将 ...
- Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { TreeLinkNode ...
- 史上最全的web前端系统学习教程!
这份资料整理花了近7天,如果感觉有用,可以分享给更有需要的人. 在看接下的介绍前,我先说一下整理这份资料的初衷: 我的初衷是想帮助在这个行业发展的朋友和童鞋们,在论坛博客等地方少花些时间找资料,把有限 ...
- Nmap误报1720端口开放的原因
在使用Nmap扫描服务器开放端口(全连接扫描)时,一直会发现误报1720端口开放,telnet也有时会连接成功.而实际上服务器并未开启此端口.经过查阅资料,确定原因如下: H.323协议在负载中放入了 ...
- python版GetTickCount()
time.clock() return the current processor time as a floating point number expressed in seconds. 即返回一 ...
- python 正则表达式匹配中文(转)
网上的一篇文章,做了整理,作者已无从考证,谢谢了 s=""" en: Regular expression is a powerful tool for manipula ...
- LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))
#6279. 数列分块入门 3 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 3 题目描述 给 ...