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 ...
随机推荐
- Struts2WebUtil
一个简单的实用工具类 package cn.jorcen.commons.util; import javax.servlet.http.HttpServletRequest; import org. ...
- input标签文字点击变颜色
<input type="text" class="ser_input"value="从这里搜索(^_^)" onfocus=&quo ...
- sjtu1285 时晴时雨
Description Taring 喜欢晴天,也喜欢雨天. Taring说:我想体验连续的\(K\)天的晴朗,去远足,去放歌:我还想再这\(K\)个晴天之后,再去体验连续的K天的云雨,去感受落雨时的 ...
- PHP漏洞全解(九)-文件上传漏洞
本文主要介绍针对PHP网站文件上传漏洞.由于文件上传功能实现代码没有严格限制用户上传的文件后缀以及文件类型,导致允许攻击者向某个可通过 Web 访问的目录上传任意PHP文件,并能够将这些文件传递给 P ...
- 对 APM 用户的一次真实调查分析(上)
一.前言 国内的 APM 行业这两年刚刚起步,但是在国外却比较成熟了,并且由于这两年人力成本的快速提高,国内外涌现了几家非常不错的 APM 企业,例如,OneAPM,APPdynamic,Dynami ...
- POJ 2186 Popular Cows(Tarjan)
http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...
- mysql 监控工具monyog使用总结
1. 下载安装 2. 登录之后,查看 locked queries 2. 慢查询
- 131. Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- (转载)NET流操作
http://www.oseye.net/user/kevin/blog/86 概念 数据流(Stream)是对串行传输数据的一种抽象表示,是对输入/输出的一种抽象.数据有来源和目的地,衔接两者的就是 ...
- 判断微信内置浏览器的UserAgent
要区分用户是通过"微信内置浏览器"还是"原生浏览器"打开的WebApp, 可以通过navigator.userAgent来进行判断. 以下是对各种平台上微信内置 ...