codeforces 557D Vitaly and Cycle
2015-07-19 17:55
417 查看
新技能get
二分图染色居然可以用来判断奇偶环。。
如果能构成二分图,说明没有奇环
对于任意二分图,其包含的环一定全部是偶环!(充要可证)
可以证明,含有奇数条边的环一定有两个在相同集合内的点有边相连!
也就是说——二分图的bfs子树一定不含奇环!
下面讨论摘自http://www.cnblogs.com/sagitta/p/4612214.html
首先讨论添加边条数为3——即原原图边数m为0时
ns=n*(n-1)*(n-2)/6
再讨论添加边条数为2——即原图中所有边都没有公共端点/所有点度数<=1 时
ans=m*(n-2)
再讨论添加边数为0——即原图中存在奇环时
ans=1
最后讨论添加边数为1——即原图中只有树以及偶环
ans=Σ[ (white[i]-1)*white[i]/2+(black[i]-1)*black[i]/2 ]
看的matrix大牛代码
dfs中直接求ans
View Code
待做。。
http://www.hardbird.net/hdu-5215-cycle%E4%BA%8C%E5%88%86%E5%9B%BE%E6%9F%93%E8%89%B2%E5%88%A4%E5%A5%87%E5%81%B6%E7%8E%AF/
二分图染色居然可以用来判断奇偶环。。
如果能构成二分图,说明没有奇环
对于任意二分图,其包含的环一定全部是偶环!(充要可证)
可以证明,含有奇数条边的环一定有两个在相同集合内的点有边相连!
也就是说——二分图的bfs子树一定不含奇环!
下面讨论摘自http://www.cnblogs.com/sagitta/p/4612214.html
首先讨论添加边条数为3——即原原图边数m为0时
ns=n*(n-1)*(n-2)/6
再讨论添加边条数为2——即原图中所有边都没有公共端点/所有点度数<=1 时
ans=m*(n-2)
再讨论添加边数为0——即原图中存在奇环时
ans=1
最后讨论添加边数为1——即原图中只有树以及偶环
ans=Σ[ (white[i]-1)*white[i]/2+(black[i]-1)*black[i]/2 ]
看的matrix大牛代码
dfs中直接求ans
/*Author :usedrose */ /*Created Time :2015/7/19 17:01:15*/ /*File Name :2.cpp*/ #include <cstdio> #include <iostream> #include <algorithm> #include <sstream> #include <cstdlib> #include <cstring> #include <climits> #include <vector> #include <string> #include <ctime> #include <cmath> #include <deque> #include <queue> #include <stack> #include <set> #include <map> #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) #define MAXN 100110 #define OK cout << "ok" << endl; #define o(a) cout << #a << " = " << a << endl #define o1(a,b) cout << #a << " = " << a << " " << #b << " = " << b << endl using namespace std; typedef long long LL; int n, m; bool marked[MAXN], ok; int sz[2]; int col[MAXN]; vector<int > G[MAXN]; LL ans; void dfs(int v, int c) { marked[v] = true; ans += sz[c-1]; sz[c-1]++; col[v] = c; for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; if (!marked[u]) { col[u] = 3 - c; dfs(u, 3 - c); } else if (col[u] == col[v]) ok = true; } } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; int a, b; for (int i = 0;i < m; ++ i) { cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } for (int i = 1; i <= n; ++ i) if (!marked[i]) { sz[0] = sz[1] = 0; dfs(i, 1); } if (ok) cout << 0 << " " << 1 << endl; else if (ans == 0) { if (m == 0) { long long ans = n; ans *= n - 1; ans *= n - 2; ans /= 6; cout << 3 << " " << ans << endl; } else cout << 2 << " " << (long long)m * (n - 2) << endl; } else cout << 1 << " " << ans << endl; return 0; }
View Code
待做。。
http://www.hardbird.net/hdu-5215-cycle%E4%BA%8C%E5%88%86%E5%9B%BE%E6%9F%93%E8%89%B2%E5%88%A4%E5%A5%87%E5%81%B6%E7%8E%AF/
相关文章推荐
- HTTP头部解释,HTTP头部详细分析,最全HTTP头部信息
- javaThread源码
- JAVA基础_Java反射
- hdu 1528 Card Game Cheater 最大匹配
- 屏蔽控制台应用程序窗口
- bootstrap 笔记01
- GCC 参数详解
- Linux环境下段错误的产生原因及调试方法小结
- IOS计算文字高度
- bootstrap模态框和select2合用时input无法获取焦点
- badboy+jMeter+jProfile软件测试工具介绍(一)
- 【thinkphp3.1.x】thinkphp3.1.x中有关redis缓存相关的文件
- 关于hibernate的一些恶心的小地方报错(持续更新)
- 系统重装后,Mysql数据库重装加载原来数据库
- Linux下的GitHub安装与简单配置教程
- Tachyon0.6.4+Spark1.3+hadoop2.6.0 配置教程详解
- 基于 OpenFlow 实现网络虚拟化
- 在Windows7上安装MySQL5.6后没有服务,无法启动的问题,报错10061
- piwik阅读(整体结构)
- 股票学习27