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 ...
随机推荐
- 0522 HTML表单 CSS基础
一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,“无序列表”的意思. li:lis ...
- Python 3 socket 编程
Python 3 socket编程 一 客户端/服务器架构 互联网中处处是C/S架构 1.C/S结构,即Client/Server(客户端/服务器)结构 2.在互联网中处处可见c/s架构 比如说浏览器 ...
- 通过vSphere API获取对象Statistics
预备知识点: 1.vim.PerformanceManager.MetricId() 通过counter_id获取到metric_id 2.vim.PerformanceManager.Quer ...
- codeforces 54A
题意:收到礼物的规则为每个假日必收到一份礼物,每K天里至少收到一份礼物,求出N天中收到的礼物的最小数量. 思路:将N天根据假日所在天数分为一段段,当假日与假日之间间隔天数hol[i]>-hol[ ...
- linux bash缓存
http://www.xuebuyuan.com/296675.html 在已经运行的系统中更改了某些以前存在于PATH环境变量所指明的目录中的程序的存放目录后可能出现No such file or ...
- linux dd、echo 、watch、fuser命令
一.dd命令 以数据流进行复制,cp命令则是以文件为单位进行复制 if=数据来源 of=数据存储目标 bs=# 复制的字节数 count=# 复制几个bs seek=# 跳过多 ...
- Oracle数据库的三种验证机制
关于超级管理员登陆不需要密码因为: 数据库的三种验证机制: 操作系统验证(具有sysdba和sysopera的用户) 密码文件验证(具有sysdba和sysopera的用户) 数据库验证(普通用户) ...
- 命令行执行大sql文件
mysql -h localhost -u root -p 123456 < F:/hello world/niuzi.sql
- php中五种常见的设计模式
设计模式 一书将设计模式引入软件社区,该书的作者是 Erich Gamma.Richard Helm.Ralph Johnson 和 John Vlissides Design(俗称 “四人帮”).所 ...
- 让网站变灰的CSS代码(支持IE、FIREFOX和CHROME)(转)
方法1:支持IE <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xht ...