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 ...
随机推荐
- JAVA TCP/IP Socket通信机制以及应用
关于局域网通信(同一wifi下,自己电脑当服务端,同一网络段) 1.例如192.168.1.x,只有x位不相同视为同一网络段 2.当具备了以上条件,即可编写服务端代码,服务端的机制. 3.Server ...
- IOS 获得通讯录中联系人的所有属性 备用参考
ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...
- python 内置函数 getattr
class Getattr_Test(): var_a = 'abc' def methodA(self): var_b = 'xyz' return var_b t = Getattr_Test() ...
- input标签文字点击变颜色
<input type="text" class="ser_input"value="从这里搜索(^_^)" onfocus=&quo ...
- [JavaScript] js判断是否在微信浏览器中打开
用JS来判断了,经过查找资料终于实现了效果, function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(u ...
- [转载]MongoDB学习(二):数据类型和基本概念
数据类型 基本数据类型 MongoDB的文件存储格式为BSON,同JSON一样支持往其它文档对象和数组中再插入文档对象和数组,同时扩展了JSON的数据类型.与数据库打交道的那些应用.例如,JSON没有 ...
- 【Uva 12558】 Egyptian Fractions (HARD version) (迭代加深搜,IDA*)
IDA* 就是iterative deepening(迭代深搜)+A*(启发式搜索) 启发式搜索就是设计估价函数进行的搜索(可以减很多枝哦~) 这题... 理论上可以回溯,但是解答树非常恐怖,深度没有 ...
- javaweb学习总结(四十一)——Apache的DBUtils框架学习
一.commons-dbutils简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化 ...
- S3C2410 ADS实验手册
http://www.evernote.com/shard/s307/sh/b45f0e60-3232-4cbb-99f6-a273236a2faa/bd865ae048ac797585303ef54 ...
- 【HDOJ】3337 Guess the number
神一样的题目.简言之,利用手段获得测试用例的第一行,输出结果.很显然利用wa, TLE, OLE等judge status可以获得测试用例.因此,果断Python写一个acm提交机器人.依赖lxml库 ...