Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html
给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数。
很容易知道连的边数只能是0,1,2,3。
如果是二分图一定不含长度为奇数的环。
难点是如果是二分图怎么求方案数呢?
二分图染色时能求出每一个联通块。在每一个联通块中把任意两个颜色相同的点连一条边即可达到要求。
如图中红色和绿色的边就是部分可行解

代码(含注释):
/*****************************************************
Memory: 9380 KB Time: 155 MS
Language: GNU G++ 4.9.2 Result: Accepted
*****************************************************/
#include<bits/stdc++.h> using namespace std;
typedef long long ll; const int N = 100005; int color[N];
vector<int> G[N];
int degree[N];
int kind[N];
int sumk[N];
int colk[N]; bool dfs(int v, int clr, int kd)
{
color[v] = clr;
kind[v] = kd;
for (unsigned i = 0; i < G[v].size(); ++i)
{
int u = G[v][i];
if (!color[u])
{
if (!dfs(u, 3 - clr, kd))
return false;
}
else if (color[u] == clr) return false;
}
return true;
} void solve(int n)
{
/** 每一坨中点有多少个 每一坨中颜色为1的点有多少个*/
for (int i = 1; i <= n; ++i)
{
sumk[kind[i]]++;
if (color[i] == 1) colk[kind[i]]++;
}
} int main()
{
std::ios::sync_with_stdio(false);
int n, m;
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);
degree[a]++;
degree[b]++;
} /**没有边,需要随意连接三个点 C(n,3)*/
if (m == 0)
{
cout << "3 " << (ll)n * (n - 1) * (n - 2) / 3 / 2;
return 0;
} /**每个边的长度都等于1,那么随便找一个边再连一个点就好了*/
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (degree[i] > 1)
{
cnt = -1;
break;
}
else if (degree[i] == 1)
{
cnt++;
}
if (cnt != -1)
{
cout << "2 " << (ll)cnt / 2 * (n - 2);
return 0;
} /** 二分图匹配,如果不成 证明有奇长度的环 */
int kd = 0;
for (int i = 1; i <= n; ++i)
{
if (!color[i])
if (!dfs(i, 1, kd++))
{
kd = -1;
break;
}
}
if (kd == -1)
{
cout << "0 1";
return 0;
} /** 如果没有奇数环,所要做的就是找到两个同一堆的点中颜色相同的,随便连 */
ll ans = 0;
solve(n);
for (int i = 0; i < kd; ++i)
{
//cout << colk[i] << " " << sumk[i] << endl;
ans += (ll)colk[i] * (colk[i] - 1) + (ll)(sumk[i] - colk[i]) * (sumk[i] - colk[i] - 1);
}
cout << "1 " << ans / 2; return 0;
}
Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)的更多相关文章
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论
D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环
题目链接: 点这里 题目 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inpu ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #311 (Div. 2) A,B,C,D,E
A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdli ...
- Codeforces Round #311 (Div. 2)题解
A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #330 (Div. 2) A. Vitaly and Night 暴力
A. Vitaly and Night Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/p ...
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset
C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...
随机推荐
- asp.net中对象的序列化,方便网络传输
对象序列化 是将对象状态转换为可保持或传输的格式的过程.反序列化 是将流转换为对象序列化和反序列化相结合 可以使对象数据轻松的存储和传递 在 .NET 中,如果是对象可序列化,需要在 声明对象的开始部 ...
- codeforces 387C George and Number
这道题目的意思是对于每个要删除的数字,向前或向后找到一块连续的数字,而它是其中最小的: 很容易看出对于所有要先删除的数字要从大到小删除: 然后对于每个要删除的字母,要找到比他小的,但是在原数列中又靠它 ...
- android TabActivity的局限性 是否还有存在的必要性
TabActivity的局限性 是否还有存在的必要性 其实谷歌有此举动,我们也应该早就想到了,为什么会这么说呢?那就要从TabActivity的原理开始说起了. 做个假定先: 比如我们最外面的Act ...
- Power Designer 使用技巧总结
1.设置主键自增 在表的属性界面---选择column---双击主键: 2. 为脚本添加注释: 在表的属性界面---选择column分别进行下列设置:
- MongoDB实战指南(四):MongoDB的Journaling日志功能
mongoDB的Journaling日志功能与常见的log日志是不一样的,mongoDB也有log日志,它只是简单记录了数据库在服务器上的启动信息.慢查询记录.数据库异常信息.客户端与数据库服务器连接 ...
- 【Linux安全】防止 root 用户远程登录
防止 root 用户远程登录,在终端输入以下命令: vim /etc/ssh/sshd_config 修改如下行为:no PermitRootLogin no 如图所示:
- Android:MD5加密
/** * @author gongchaobin * * MD5加密 * * @version 2013-8-22 */ public class MD5Util { // 用来将字节转换成 16 ...
- C# ASP.NET FILEUPLOAD详解
显示一个文本框控件和一个浏览按钮,使用户可以选择要上载到服务器的文件. 命名空间: System.Web.UI.WebControls 程序集: System.Web(在 system.web.dll ...
- Leap Years
Description: In this kata you should simply determine, whether a given year is a leap year or not. I ...
- Oracle系列之视图
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 创建视图,把tb_Employee表sal<1000的雇员,映射到该视图( view) create or replace vi ...