19E Fairy
Once upon a time there lived a good fairy A. One day a fine young man B came to her and asked to predict his future. The fairy looked into her magic ball and said that soon the fine young man will meet the most beautiful princess ever and will marry her. Then she drew on a sheet of paper n points and joined some of them with segments, each of the segments starts in some point and ends in some other point. Having drawn that picture, she asked the young man to erase one of the segments from the sheet. Then she tries to colour each point red or blue so, that there is no segment having points of the same colour as its ends. If she manages to do so, the prediction will come true. B wants to meet the most beautiful princess, that's why he asks you to help him. Find all the segments that will help him to meet the princess.
The first input line contains two integer numbers: n — amount of the drawn points and m — amount of the drawn segments (1 ≤ n ≤ 104, 0 ≤ m ≤ 104). The following m lines contain the descriptions of the segments. Each description contains two different space-separated integer numbers v, u (1 ≤ v ≤ n, 1 ≤ u ≤ n) — indexes of the points, joined by this segment. No segment is met in the description twice.
In the first line output number k — amount of the segments in the answer. In the second line output k space-separated numbers — indexes of these segments in ascending order. Each index should be output only once. Segments are numbered from 1 in the input order.
4 4
1 2
1 3
2 4
3 4
4
1 2 3 4
4 5
1 2
2 3
3 4
4 1
1 3
1
5
传送门
分析
我们先对点黑白染色并建树,进行此操作之后将有3种边:
1.树边
2.非树黑白异色边
3.非树同色边
于是产生了以下几种情况:
仅有一条非树同色边就可以删这个边,否则不能删这种边
有非树同色边就不能删非树异色边,否则可删
树边如果在奇环上且在所有奇环上可删,否则不可删
一个没有简单奇环的图,一定没有奇环。所以题目要找的就是覆盖所有简单奇环的边。在dfs树上,简单环与返祖边一一对应。所以在dfs的时候每遇到一个返祖边,就差分标记路径。最后找到被所有路径覆盖的树边。
上面这个只是解法的思路,实现起来有很多细节:
如果某个树边在一个偶环上,删除它之后会产生新的奇环,所以还要差分标记一下偶环个数。
构成奇环的返祖边可以被纳入答案,当且仅当图中只有一个奇环【因为他只能覆盖一个奇环】。
如果图中没有奇环,可以随意删除一条边。
注意原图不联通的情况。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
struct edge{
int next,to,fa,cl;
}e[1100000];
int head[1100000],col[1100000];
bool used[1100000];
int od[1100000],uod[1100000],dep[1100000];
int cnt,n,m,ans,sum,top;
int Ans[1100000];
void add(int u,int v,int no){
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt++;
e[cnt].to=u;
e[cnt].next=head[v];
head[v]=cnt++;
}
/*
cl=-1:树边
cl=1:奇环边
cl=2:偶环边
*/
void dfs(int x,int no){
int i,j,k;
top++;
dep[x]=top;
used[x]=1;
for(i=head[x];~i;i=e[i].next){
if(e[i].cl==-1)continue;
k=e[i].to;
if(!used[k]){//没走过
e[i].cl=e[i^1].cl=-1;
dfs(k,i>>1);
//向下累加奇环和偶环数目
od[no]+=od[i>>1];
uod[no]+=uod[i>>1];
}
else {
//清除重复记录
if(e[i].cl==1)
od[no]--;
else if(e[i].cl==2)
uod[no]--;
else{
if((dep[k]-dep[x])&1){//偶环
e[i].cl=e[i^1].cl=2;
uod[no]++;
}
else {
e[i].cl=e[i^1].cl=1;
od[no]++;
sum++;
}
}
}
}
top--;
}
void go(){
printf("%d\n",m);
for(int i=1;i<=m;i++)
printf("%d ",i);
puts("");
}
int main()
{ int i,j,k,v,u;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v,i);
}
for(i=1;i<=n;i++)
if(!used[i]){
dfs(i,0);
}
if(sum==0){
go();
return 0;
}
for(i=0;i<m;i++){
if(od[i]==sum&&uod[i]==0)Ans[++ans]=i;
else if(sum==1&&e[i<<1].cl==1)Ans[++ans]=i;
}
printf("%d\n",ans);
for(i=1;i<=ans;i++)
printf("%d ",Ans[i]+1);
puts("");
return 0;
}
19E Fairy的更多相关文章
- CF 19E Fairy——树上差分
题目:http://codeforces.com/contest/19/problem/E 去掉一条边,使无向图变成二分图. 该边应该被所有奇环经过,且不被偶环经过. 因为一条非树边一定只在一个环里. ...
- bzoj 4424: Cf19E Fairy && codeforces 19E. Fairy【树形dp】
参考:https://blog.csdn.net/heheda_is_an_oier/article/details/51131641 这个找奇偶环的dp1真是巧妙,感觉像tarjan一样 首先分情况 ...
- Codeforces 19E&BZOJ 4424 Fairy(好题)
日常自闭(菜鸡qaq).不过开心的是看了题解之后1A了.感觉这道题非常好,必须记录一下,一方面理清下思路,一方面感觉自己还没有完全领会到这道题的精髓先记下来以后回想. 题意:给定 n 个点,m 条边的 ...
- ural 1343. Fairy Tale
1343. Fairy Tale Time limit: 1.0 secondMemory limit: 64 MB 12 months to sing and dance in a ring the ...
- BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)
Va爷的胡策题T2 E. Fairy time limit per test1.5 seconds memory limit per test256 megabytes inputstandard i ...
- [jzoj]3506.【NOIP2013模拟11.4A组】善良的精灵(fairy)(深度优先生成树)
Link https://jzoj.net/senior/#main/show/3506 Description 从前有一个善良的精灵. 一天,一个年轻人B找到她并请他预言他的未来.这个精灵透过他的水 ...
- bzoj千题计划229:bzoj4424: Cf19E Fairy
http://www.lydsy.com/JudgeOnline/problem.php?id=4424 图是二分图的条件:没有奇环 所以,如果图不存在奇环,删除任意一条边都可以 如果存在奇环, 对于 ...
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分
C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...
- 【BZOJ4424】Cf19E Fairy DFS树
[BZOJ4424]Cf19E Fairy Description 给定 n 个点,m 条边的无向图,可以从图中删除一条边,问删除哪些边可以使图变成一个二分图. Input 第 1 行包含两个整数 n ...
随机推荐
- 【leetcode刷题笔记】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- JavaScript的Function 类型
一,Function定义 Function实际上是对象,与其他引用类型一样具有属性和方法.Function可以通过三种方法进行定义,分别是函数声明语法定义,函数表达式定义和Function构造函数定义 ...
- Excel 2007中自定义数字格式前要了解的准则
要在Excel 2007中创建自定义数字格式,首先应了解自定义数字格式的准则,并从选择某一内置数字格式开始.然后,可以更改该格式的任意代码部分,从而创建自己的自定义数字格式. 数字格式最多可包含四个代 ...
- hive启动时 Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
错误提示信息如下 [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleC ...
- vc中调用Com组件的所有方法详解
首先,对于Com组件的入门学习,可以看一下<Windows程序设计技术基础——MFC与.NET> 任哲编著的21世纪重点大学规划教材那本书,适合入门(虽然不一定会使用),了解些基础原理. ...
- 京东自营,你TM太坑了。
双12来了,京东自营好坑.昨天(12月6日)看的一条秋裤,89元,今天准备买,居然涨到了119,他大爷的. 京东你大爷的.
- C++(三)— 二维容器
1.二维bool向量 vector<vector<bool>> dp(len, vector<bool>(len, false));
- ES doc_values介绍2——本质是field value的列存储,做聚合分析用,ES默认开启,会占用存储空间
一.doc_values介绍 doc values是一个我们再三重复的重要话题了,你是否意识到一些东西呢? 搜索时,我们需要一个“词”到“文档”列表的映射 排序时,我们需要一个“文档”到“词“列表的映 ...
- Windows 下GitHub 安装和使用
一.官网注册和设置 1.登录官网,注册账号,其中用户名以后会用到. 2.创建仓库.使用公开仓库方式创建,公开仓库免费.(右上角->加号->new repository) 第一行:仓库名字. ...
- BZOJ2733:[HNOI2012]永无乡
浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...