题意:如标题

思路:对于奇环,一个二分图判定就ok了,有奇环<=>非二分图。对于偶环,考虑环必定出现在双联通分量里面,可以先求出图的双联通分量,对于一个双联通分量,对于双联通分量里面的每个环,如果是偶环,则偶环已找到,否则假定存在多个奇环,则可以任选两个奇环,把共享边去掉,一定可以得到一个新偶环,这种情况下偶环也是存在的。所以不存在偶环的情况只可能是双联通分量是一个大奇环,特点是:边数=点数,且为奇。于是先dfs一下标记所有桥,用并查集标记所有双联通分量,对每个双联通分量,计算它的点数,对每条边,如果它的两个端点属于同一个双联通分量,则对应双联通分量边数+1。由于是无向边,每条边会被考虑两次。对每个双联通分量,条件改成!((cnt_v*2=cnt_e)&1),如果上述式子为true,则表示存在偶环。

 #pragma comment(linker, "/STACK:102400000,102400000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility>
#include <vector> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e5 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct UFS {
vector<int> F;
void init(int n) { F.resize(n + ); for (int i = ; i <= n; i ++) F[i] = i; }
int get(int u) { if (F[u] == u) return u; return F[u] = get(F[u]); }
void add(int u, int v) { F[get(u)] = get(v); }
}; struct Graph {
vector<vector<int> > G;
void clear() { G.clear(); }
void resize(int n) { G.resize(n + ); }
void add(int u, int v) { G[u].push_back(v); }
vector<int> & operator [] (int u) { return G[u]; }
}; Graph G;
int n, m; int color[maxn];
bool BG_chk(int u, int c) {
color[u] = c;
int sz = G[u].size();
rep_up0(i, sz) {
int v = G[u][i];
if (color[v] == c) return false;
if (color[v]) continue;
if (!BG_chk(v, - c)) return false;
}
return true;
} UFS us;
int pre[maxn], low[maxn], dfs_clock;
int getBridge(int u, int fa) {
int lowu = pre[u] = ++ dfs_clock;
int child = , sz = G[u].size();
rep_up0(i, sz) {
int v = G[u][i];
if (!pre[v]) {
child ++;
int lowv = getBridge(v, u);
min_update(lowu, lowv);
if (lowv <= pre[u]) us.add(u, v);
}
else {
if (pre[v] < pre[u] && v != fa) {
min_update(lowu, pre[v]);
}
}
}
return low[u] = lowu;
} int cnt_v[maxn], cnt_e[maxn], vis[maxn];
bool findEvenRing() {
dfs_clock = ;
mem0(pre);
rep_up1(i, n) {
if (!pre[i]) {
getBridge(i, );
}
}
mem0(cnt_e);
mem0(cnt_v);
rep_up1(i, n) {
int u = us.get(i);
cnt_v[u] ++;
}
rep_up1(i, n) {
int sz = G[i].size();
rep_up0(j, sz) {
int u = G[i][j], tmp;
if ((tmp = us.get(i)) == us.get(u)) {
cnt_e[tmp] ++;
}
}
}
mem0(vis);
rep_up1(i, n) {
int u = us.get(i);
if (vis[u]) continue;
vis[u] = true;
if ((cnt_v[u] * != cnt_e[u] || !(cnt_v[u] & )) && cnt_v[u] >= ) return true;
}
return false;
} int main() {
//freopen("in.txt", "r", stdin);
int T;
cin >> T;
while (T --) {
cin >> n >> m;
G.clear();
G.resize(n);
us.init(n);
rep_up0(i, m) {
int u, v;
sint2(u, v);
G.add(u, v);
G.add(v, u);
}
bool odd = false, even = findEvenRing();
mem0(color);
rep_up1(i, n) {
if (!color[i]) {
odd = odd || !BG_chk(i, );
}
}
puts(odd? "YES" : "NO");
puts(even? "YES" : "NO");
}
return ;
}

[hdu5215]无向图找奇偶环的更多相关文章

  1. HDU-5215 Cycle 无向图判奇环偶环

    题意:给一个无向图,判断这个图是否存在奇环和偶环. 解法:网上有一种只用dfs就能做的解法,但是我不太理解. 这里用的是比较复杂的.首先奇环很简单可以用二分图染色判断.问题是偶环怎么判断?这里我们想, ...

  2. zstu.4191: 无向图找环(dfs树 + 邻接表)

    4191: 无向图找环 Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 117  Solved: 34 Description 给你一副无向图,每条边有 ...

  3. 牛客D-Where are you /// kruskal+tarjan找无向图内的环

    题目大意: https://ac.nowcoder.com/acm/contest/272/D 在一个无向图中,给定一个起点,从起点开始走遍图中所有点 每条边有边权wi,表示第一次经过该道路时的花费( ...

  4. HDU 5215 BestCoder"杯中国大学生程序设计冠军赛” 边双连通分量取出子图+二分染色判图内奇偶环

    Cycle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  5. Network POJ - 3694 无向图找桥

    题意: 给你一个无向图,你需要找出来其中有几个桥 桥: 1.存在重边必定不为桥 2.low[v]>dfn[u] 代码: //题意很清晰 //就是这个需要先找出来原无向图中的桥个数,然后在判断添加 ...

  6. SPFA找负环(DFS) luogu3385

    SPFA找负环的基本思路就是如果一个点被访问两次说明成环,如果第二次访问时所用路径比第一次短说明可以通过一直跑这个圈将权值减为负无穷,存在负环 有bfs和dfs两种写法,看了一些博客,在bfs和dfs ...

  7. HDU-5215 Cycle(边双/判奇偶环)

    题目 HDU-5215 Cycle 网上那个啥dfs的垃圾做法随便弄组数据已经hack掉了 做法 纯奇环偶环通过dfs树上,染色判断(由于偶环可能有两个奇环,通过一点相交,dfs树上并不能判完) 两环 ...

  8. hdu 6041 I Curse Myself 无向图找环+优先队列

    I Curse Myself Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. cf290-2015-2-3总结与反思(dfs判断无向图是否有环)

    bool dfs(int i,int pre) { visit[i]=true; ;j<=v;j++) if(g[i][j]) { if(!visit[j]) return dfs(j,i); ...

随机推荐

  1. vue2.x学习笔记(十八)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12629705.html. 处理边界情况 这里记录的都是和处理边界情况有关的功能,即一些需要对vue的规则做一些小调 ...

  2. 牛顿迭代法的理解与应用( x 的平方根)

    题目来源与LeetCode算法题中的第69题,具体内容如下(点击查看原题): 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只 ...

  3. Python修改paramiko模块开发运维审计保垒机

    目前市面上,专门做IT审计堡垒机的厂商有很多,他们的产品都有一个特点,那就是基本上每台的售价都在20万以上.像我们做技术的,不可能每次待的公司都是大公司,那么在小公司,是不太可能投资20多万买一台硬件 ...

  4. 前端js传值JSON.stringify(obj)

    用bootstrap-talbe前端传值 首先直接传肯定是不行的; 其次做一个全局变量也不行,因为这里的问题的是用bootstrap-table进行生成的操作HTML,从这里datass = row ...

  5. JDK12的五大重要新特性

    文章目录 JDK12的五大重要新特性 引入JVM常量API 扩展了switch语句 支持Unicode 11.0 为日本Reiwa Era提供了方形字符支持 NumberFormat增加了对以紧凑格式 ...

  6. 怎么将swagger API导出为HTML或者PDF

    文章目录 将swagger API导出为HTML或者PDF 什么是Asciidoc swagger2markup-maven-plugin asciidoctor-maven-plugin 使用命令行 ...

  7. 3.PEP 8是什么?

    PEP 8是什么? PEP 8 is a coding convention, a set of recommendation, about how to write your Python code ...

  8. spark下dataframe转为rdd格式

    dataframe可以实现很多操作,但是存储到本地的时候,只能存 parquest格式 需要存储源格式,需要转换为rdd类型 将dataframe中的每一行都map成有逗号相连的string,就变为了 ...

  9. Swift-Realm数据库的使用详解

    Swift-Realm数据库的使用详解 概述 Realm 是一个跨平台的移动数据库引擎,其性能要优于 Core Data 和 FMDB - 移动端数据库性能比较, 我们可以在 Android 端 re ...

  10. 网络流--最大流--POJ 2139(超级源汇+拆点建图+二分+Floyd)

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...