【推导】【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的立方体,且每 ...
随机推荐
- HDU 1599 find the mincost route (最短路 floyd)
题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...
- grunt、Browsersync服务及weinre远程调试
一.grunt server服务 前端开发时,经常需要把静态文件映射成web服务,传统的做法是丢到apache,但太重太不友好了.开发angular的时候,官方的chrome插件对file:///的支 ...
- javascript 变量类型判断
一.typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 "); typeof arr ...
- linux下 vi中[noeol]以及出现 feff 的问题
"uptime.py" [noeol] 69L, 2311C"system/uptime.py" 69L, 2312C 'noeol' 就是 'no end-o ...
- hadoop2.4.1伪分布式环境搭建
注意:所有的安装用普通哟用户安装,所以首先使普通用户可以以sudo执行一些命令: 0.虚拟机中前期的网络配置参考: http://www.cnblogs.com/qlqwjy/p/7783253.ht ...
- yocto 离线编译
使用yocto编译一个软件包时,一般会先在本地寻找下载好的源码包,如果不存在则根据配置从网络下载. 添加本地源码包 为了支持离线编译,添加一个包的配置文件后,需要在本地也准备好源码包. 可以先打开网络 ...
- 【POJ3254】coinfield
状压dp初步. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- UIResponder简介
1.简介 在使用设备的时候我们大多时候是但手指触摸控件了进行的,比如点击密码按钮解锁,上下浏览网页等动作.你肯定也摇动过iphone抢红包和***等等,我们的系统可以处理这些事件则都需要去使用UIRe ...
- [ python ] 类中的一些特殊方法
item系列 __getitem__(self, item) 对象通过 object[key] 触发 __setitem__(self, key, value) 对象通过 object[key] = ...
- tab切换 jQuery
$('p.guidan-load1').click(function(){ $("p.guidan-load1").removeClass("guidan-load12& ...