[hdu5215]无向图找奇偶环

题意:如标题

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

  1 #pragma comment(linker, "/STACK:102400000,102400000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <ctime>
 13 #include <cctype>
 14 #include <set>
 15 #include <bitset>
 16 #include <functional>
 17 #include <numeric>
 18 #include <stdexcept>
 19 #include <utility>
 20 #include <vector>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #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) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d\n", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48 
 49 typedef long long LL;
 50 typedef pair<int, int> pii;
 51 typedef vector<int> vi;
 52 
 53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 55 const int maxn = 1e5 + 7;
 56 const int md = 10007;
 57 const int inf = 1e9 + 7;
 58 const LL inf_L = 1e18 + 7;
 59 const double pi = acos(-1.0);
 60 const double eps = 1e-6;
 61 
 62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 67 int make_id(int x, int y, int n) { return x * n + y; }
 68 
 69 struct UFS {
 70     vector<int> F;
 71     void init(int n) { F.resize(n + 5); for (int i = 0; i <= n; i ++) F[i] = i; }
 72     int get(int u) { if (F[u] == u) return u; return F[u] = get(F[u]); }
 73     void add(int u, int v) { F[get(u)] = get(v); }
 74 };
 75 
 76 struct Graph {
 77     vector<vector<int> > G;
 78     void clear() { G.clear(); }
 79     void resize(int n) { G.resize(n + 2); }
 80     void add(int u, int v) { G[u].push_back(v); }
 81     vector<int> & operator [] (int u) { return G[u]; }
 82 };
 83 
 84 Graph G;
 85 int n, m;
 86 
 87 int color[maxn];
 88 bool BG_chk(int u, int c) {
 89     color[u] = c;
 90     int sz = G[u].size();
 91     rep_up0(i, sz) {
 92         int v = G[u][i];
 93         if (color[v] == c) return false;
 94         if (color[v]) continue;
 95         if (!BG_chk(v, 3 - c)) return false;
 96     }
 97     return true;
 98 }
 99 
100 UFS us;
101 int pre[maxn], low[maxn], dfs_clock;
102 int getBridge(int u, int fa) {
103     int lowu = pre[u] = ++ dfs_clock;
104     int child = 0, sz = G[u].size();
105     rep_up0(i, sz) {
106         int v = G[u][i];
107         if (!pre[v]) {
108             child ++;
109             int lowv = getBridge(v, u);
110             min_update(lowu, lowv);
111             if (lowv <= pre[u]) us.add(u, v);
112         }
113         else {
114             if (pre[v] < pre[u] && v != fa) {
115                 min_update(lowu, pre[v]);
116             }
117         }
118     }
119     return low[u] = lowu;
120 }
121 
122 int cnt_v[maxn], cnt_e[maxn], vis[maxn];
123 bool findEvenRing() {
124     dfs_clock = 0;
125     mem0(pre);
126     rep_up1(i, n) {
127         if (!pre[i]) {
128             getBridge(i, 0);
129         }
130     }
131     mem0(cnt_e);
132     mem0(cnt_v);
133     rep_up1(i, n) {
134         int u = us.get(i);
135         cnt_v[u] ++;
136     }
137     rep_up1(i, n) {
138         int sz = G[i].size();
139         rep_up0(j, sz) {
140             int u = G[i][j], tmp;
141             if ((tmp = us.get(i)) == us.get(u)) {
142                 cnt_e[tmp] ++;
143             }
144         }
145     }
146     mem0(vis);
147     rep_up1(i, n) {
148         int u = us.get(i);
149         if (vis[u]) continue;
150         vis[u] = true;
151         if ((cnt_v[u] * 2 != cnt_e[u] || !(cnt_v[u] & 1)) && cnt_v[u] >= 4) return true;
152     }
153     return false;
154 }
155 
156 int main() {
157     //freopen("in.txt", "r", stdin);
158     int T;
159     cin >> T;
160     while (T --) {
161         cin >> n >> m;
162         G.clear();
163         G.resize(n);
164         us.init(n);
165         rep_up0(i, m) {
166             int u, v;
167             sint2(u, v);
168             G.add(u, v);
169             G.add(v, u);
170         }
171         bool odd = false, even = findEvenRing();
172         mem0(color);
173         rep_up1(i, n) {
174             if (!color[i]) {
175                 odd = odd || !BG_chk(i, 1);
176             }
177         }
178         puts(odd? "YES" : "NO");
179         puts(even? "YES" : "NO");
180     }
181     return 0;
182 }
View Code

 

posted @ 2015-05-08 01:18  jklongint  阅读(587)  评论(0)    收藏  举报