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 ...
随机推荐
- 吴恩达机器学习笔记(三) —— Regularization正则化
主要内容: 一.欠拟合和过拟合(over-fitting) 二.解决过拟合的两种方法 三.正则化线性回归 四.正则化logistic回归 五.正则化的原理 一.欠拟合和过拟合(over-fitting ...
- logstash的output插件
logstash 的output插件 nginx,logstash和redis在同一台机子上 yum -y install redis,vim /etc/redis.conf 设置bind 0.0.0 ...
- Spark- RDD持久化
官方原文: RDD Persistence One of the most important capabilities in Spark is persisting (or caching) a d ...
- python3字符串属性(二)
1.S.isdecimal() -> bool Return True if there are only decimal characters in S, False otherwise ...
- DELPHI 动态 创建和释放 多个 EDIT 控件
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...
- 如何让DIV可编辑、可拖动
1.可编辑: <div id="move" contentEditable="true">可编辑</div> 设置contentEdit ...
- php 代码中的箭头“ ->”与“=>”是什么意思?
类是一个复杂数据类型,这个类型的数据主要有属性.方法两种东西. 属性其实是一些变量,可以存放数据,存放的数据可以是整数.字符串,也可以是数组,甚至是类. 方法实际上是一些函数,用来完成某些功能. 引用 ...
- [原]NYOJ-6174问题-57
大学生程序代写 /*6174问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b ...
- 每天一个linux命令(10):touch命令
版权声明更新:2017-05-14博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- codevs1060 搞笑世界杯
题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...