【推导】【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的立方体,且每 ...
随机推荐
- 2018 Multi-University Training Contest 1-1002 -Balanced Sequence(括号匹配+贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6299 题目: 题意:t组数据,每组数据给你一个n表示给你n个括号串,这n个括号串之间进行组合,求能够匹 ...
- 2009 Round2 A Crazy Rows (模拟)
Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...
- poj 2762 tarjan缩点+拓扑序
2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...
- arduino 用电位器调节LED闪烁频率
int dianwei; int led = 13; void setup() { // put your setup code here, to run once: Serial.begin(9 ...
- php快速入门总结
因为本人已经接触了C和C++两年多了,虽然真正用它们的机会很少,但是基本的语法还是相对熟悉的.半年前的课程设计用了PHP,所以当初我也只是现学先用, 学得很粗糙,现在,跟一个同学合作搞一个比赛的项目, ...
- linux驱动基础系列--Linux下Spi接口Wifi驱动分析
前言 本文纯粹的纸上谈兵,我并未在实际开发过程中遇到需要编写或调试这类驱动的时候,本文仅仅是根据源码分析后的记录!基于内核版本:2.6.35.6 .主要是想对spi接口的wifi驱动框架有一个整体的把 ...
- linux命令面试题
文件管理命令 (1)Linux的文件系统目录配置要遵循FHS规范,规范定义的两级目录规范如下: /home 每个账号在该目录下都有一个文件夹,进行数据的管理 /usr 有 ...
- 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...
- Scrapy爬虫:抓取大量斗图网站最新表情图片
一:目标 第一次使用Scrapy框架遇到很多坑,坚持去搜索,修改代码就可以解决问题.这次爬取的是一个斗图网站的最新表情图片www.doutula.com/photo/list,练习使用Scrapy ...
- vue有关绑定的知识点
一.v-bind绑定class 1.对象的形式 <div :class="{text-danger:true}"></div> 数组形式绑定class &l ...