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刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Recovery模式【转】
本文转载自:http://tieba.baidu.com/p/2299027486 Recovery模式是手机系统的一个工程模式,作用是恢复和清除.用户进入这个模式之后,可以对当前系统的一些数据进行清 ...
- Spark- Spark内核架构原理和Spark架构深度剖析
Spark内核架构原理 1.Driver 选spark节点之一,提交我们编写的spark程序,开启一个Driver进程,执行我们的Application应用程序,也就是我们自己编写的代码.Driver ...
- POJ-1741(树分治)
树的点分治 给出详细的讲解!!点这里打开论文-分治算法在树的路径问题中的应用 本题目是他讲的第一个例题: 我的理解:每次都找树的重心,计算以重心为根的子树之间所贡献的答案.不断这样下去:如果这棵树是一 ...
- tensorflow CUDA 9.0安装成功
berli@berli-dev:~/tensorflow$ bazel-bin/tensorflow/examples/label_image/label_image 2017-12-18 00:04 ...
- PS 滤镜— —Marble 效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- 如何实现1080P延迟低于500ms的实时超清直播传输技术
再来当一次技术搬运工,内容来自高可用框架,学霸君工程师袁荣喜的如何实现1080P延迟低于500ms的实时超清直播传输技术. 导语:视频直播是很多技术团队及架构师关注的问题,在实时性方面,大部分直播是准 ...
- 交互式 shell 玩转 Python
Python 编程语言已经成为 IT 中使用的最流行的语言之一.成功的一个原因是它可以用来解决各种问题.从网站开发到数据科学.机器学习到任务自动化,Python 生态系统有丰富的框架和库.本文将介绍 ...
- Git远程克隆仓库出现Permission denied (publickey)
$ git clone git@github.com:DavidWanderer/test1.git Cloning into 'test1'... Warning: Permanently adde ...
- VC++6.0注释快捷键的添加使用
在eclipse等编辑工具中都有注释代码的快捷键,但是vc++6.0没有. vc++是以VB为脚本来控制的,在网上找到了一个VB的脚本供大家使用. 工具/原料 VC++6.0 方法/步骤 打开VC ...