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 ...
随机推荐
- LISP
LISP是一种通用高级计算机程序语言,长期以来垄断人工智能领域的应用.LISP作为因应人工智能而设计的语言,是第一个声明式系内函数式程序设计语言,有别于命令式系内过程式的C.Fortran和面向对象的 ...
- 《机器学习实战》学习笔记第十二章 —— FP-growth算法
主要内容: 一. FP-growth算法简介 二.构建FP树 三.从一颗FP树中挖掘频繁项集 一. FP-growth算法简介 1.上次提到可以用Apriori算法来提取频繁项集,但是Aprior ...
- django 更改默认语言和时间
django的默认语言为英语,默认时间为UTC. 在settings.py中更改语言和时间 LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' 改为 LANGUAGE_ ...
- C趣味题目
http://www.cnblogs.com/lua5/archive/2010/12/05/1896755.html c语言趣味题目 http://www.cppblog.com/OnTheWa ...
- JdbcUtils针对事务问题作出的第三次修改
DAO中的事务 其实在DAO中处理事务真的是“小菜一碟” try{ con.commit(); }catch(Exception e){ con.rollback(); } 但是dao层中只能是对账户 ...
- 动态规划 最长回文子串 leetcode5
public static String longestPalindrome(String s) { if(null==s||s.length()==0) return s; int n=s.leng ...
- [SHOI2017]期末考试
题目描述 有n位同学,每位同学都参加了全部的m门课程的期末考试,都在焦急的等待成绩的公布. 第i位同学希望在第ti天或之前得知所有课程的成绩.如果在第ti天,有至少一门课程的成绩没有公布,他就会等待最 ...
- IDEA使用maven中tomcat插件来启动服务器配置
一 .在项目pom文件中配置tomcat 先添加如下配置: <!-- 配置Tomcat插件 --> <build> <plugins> <plugin> ...
- Apache+OpenSSL实现证书服务器提供HTTPS
通过 Linux+Apache+OpenSSL 实现 SSL ( Secure Socket Layer )证书服务器,提供安全的 HTTPS ( Hypertext Transfer Protoco ...
- python 3中对list进行sort时,返回值为None
进行在用python的list结构时, 发现一个问题: methods = ['blogger.deletePost', 'blogger.get ...