HDU-5215 Cycle(边双/判奇偶环)
题目
网上那个啥dfs的垃圾做法随便弄组数据已经hack掉了
做法
纯奇环偶环通过dfs树上,染色判断(由于偶环可能有两个奇环,通过一点相交,dfs树上并不能判完)
两环如果相交必定形成偶环,由于不可以重复经过边,把每个边双提出来判断一下是否存在两个环以上即可
Code
为增加代码的可读性写得比较冗长
#include<bits/stdc++.h>
typedef int LL;
const LL maxn=1e6+9;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3)+(x<<1)+c-'0'; c=getchar();
}return x*f;
}
struct node{
LL to,nxt;
}dis[maxn];
LL n,num,odd,even,tot1,tot2,tim,m,T;
LL head[maxn],cir[maxn],col[maxn],dfn[maxn],low[maxn],visit[maxn];
inline void Add(LL u,LL v){
dis[++num]=(node){v,head[u]}; head[u]=num;
}
void Dfs1(LL u,LL ff){
for(LL i=head[u];i!=-1;i=dis[i].nxt){
LL v(dis[i].to);if(v==ff) continue;
if(!col[v]){
col[v]=3-col[u]; Dfs1(v,u);
}else{
if(col[v]==col[u]) odd=true;
else even=true;
}
}
}
void Dfs2(LL u,LL ff){
dfn[u]=low[u]=++tim;
for(LL i=head[u];i!=-1;i=dis[i].nxt){
LL v(dis[i].to); if(v==ff) continue;
if(!dfn[v]){
Dfs2(v,u); low[u]=std::min(low[v],low[u]);
if(low[v]>dfn[u]){
cir[i]=cir[i^1]=true;
}
}else low[u]=std::min(low[u],dfn[v]);
}
}
void Dfs3(LL u,LL ff){
++tot1; visit[u]=true;
for(LL i=head[u];i!=-1;i=dis[i].nxt){
if(cir[i]) continue;
LL v(dis[i].to); tot2++;
if(v==ff || visit[v]) continue;
Dfs3(v,u);
}
}
int main(){
T=Read();
while(scanf("%d%d",&n,&m)==2){
num=-1;
for(LL i=1;i<=n;++i) head[i]=-1;
for(LL i=1;i<=n;++i) visit[i]=col[i]=dfn[i]=low[i]=0;
for(LL i=0;i<=(m<<1);++i) cir[i]=0;
odd=even=tim=0;
for(LL i=1;i<=m;++i){
LL u(Read()),v(Read());
Add(u,v); Add(v,u);
}
for(LL i=1;i<=m;++i){
if(!dfn[i]) col[i]=1,Dfs1(i,0),Dfs2(i,0);
}
for(LL i=1;i<=n;++i){
if(!visit[i]){
tot1=tot2=0;
Dfs3(i,0);
tot2>>=1;
if(tot1!=1){
if(tot2>tot1) even=true;
}
}
}
if(odd) puts("YES");else puts("NO");
if(even) puts("YES"); else puts("NO");
}
}
HDU-5215 Cycle(边双/判奇偶环)的更多相关文章
- HDU.5215.Cycle(判环)
题目链接 \(Description\) 给定\(n\)个点\(m\)条边的无向图,问是否存在一个长度为奇数/偶数的简单环. \(n\leq 10^5,m\leq 3\times 10^5\). \( ...
- UVA11090 Going in Cycle!!(二分判负环)
UVA11090 Going in Cycle!! 二分答案,用spfa判负环. 注意格式:图不一定连通. 复杂度$O(nmlog(maxw-minw))$ #include<iostream& ...
- UVA11090 Going in Cycle (二分+判负环)
二分法+spfa判负环.如果存在一个环sum(wi)<k*x,i=0,1,2...,k,那么每条边减去x以后会形成负环.因此可用spfa来判负环. 一般spfa判负环dfs最快,用stack次之 ...
- hdu 5215 Cycle
题意:找到一个图中是否含有奇环和偶环 题解: 1.用了两种发法.一个就是跟bc给的答案一样,先求弱联通分量.再在环中找奇偶环 2.我想到的一个略微省些代码量的方法.边求联通分量,边推断是否含有奇环偶环 ...
- HDU 5215 Cycle(dfs判环)
题意 题目链接 \(T\)组数据,给出\(n\)个点\(m\)条边的无向图,问是否存在一个奇环/偶环 Sol 奇环比较好判断吧,直接判是否是二分图就行了.. 偶环看起来很显然就是如果dfs到一个和他颜 ...
- HDU 5215 BestCoder"杯中国大学生程序设计冠军赛” 边双连通分量取出子图+二分染色判图内奇偶环
Cycle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)
/** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...
随机推荐
- Visual Studio 2012 VC下 OpenGL 配置与使用
Windows环境下的GLUT下载地址:(大小约为150k) Download 1 32位Windows环境下安装GLUT的步骤1.将glut.h复制到C:\Program Files (x86 ...
- 华为 mate30 安装谷歌助手
最近入手了 华为 mate30 pro, 作为一个8年的老果粉,在使用2天 mate30p 之后,给了耳目一新的感觉,不得不说这款手机真的很强大,各种优点我也不多说了,可以看网上各种专业的测评 但是手 ...
- CSS-盒模型与文本溢出笔记
注意点: 文本居中: text-align:center:文本左右居中 line-heigh:30px; 等于容器高度时,单行文本上下居中 margin:0 auto: 浏览器居中 清除margin ...
- ETC1/DXT1 compressed textures are not supported when publishing to iPhone
Build application in Unity 2017.20f3 用Unity2017/2018编译iPhone版本出现以下错误: ETC1(or DXT1) compressed textu ...
- Jenkins使用过程中注意事项
jenkins自动部署注意事项: 安装jenkins https://blog.csdn.net/qq_37372007/article/details/81586751 1.当提示错误ERROR: ...
- MySQL MGR--MGR部署
MGR部署 场景描述: 使用三台服务器搭建一个简单MGR集群,使用MySQL 5.7.24版本,服务器列表为: 192.168.1.147 192.168.1.148 192.168.1.149 1. ...
- MySQL MHA工作原理
MHA工作组件 MHA(Master High Availability)是一种MySQL高可用解决方案,由日本DeNA公司开发,主要用于在故障切换和主从提升时进行快速切换,并最大程度保证数据一致性. ...
- golang读写文件
1. 标准输入输出 os提供了标准输入输出文件: Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") Stdout = Ne ...
- Justice(HDU6557+2018年吉林站+二进制)
题目链接 传送门 题意 给你\(n\)个数,每个数表示\(\frac{1}{2^{a_i}}\),要你把这\(n\)个数分为两堆,使得每堆的和都大于等于\(\frac{1}{2}\). 思路 首先我们 ...
- linux卸载及安装mysql 5.7以上
删除: 1.rpm -qa|grep -i mysql 查看安装的mysql 2./usr/local/mysql/support-files/mysql.server stop 停止mys ...