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(二分图染色应用)的更多相关文章

  1. 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 ...

  2. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环

    题目链接: 点这里 题目 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inpu ...

  3. 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 ...

  4. Codeforces Round #311 (Div. 2) A,B,C,D,E

    A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdli ...

  5. Codeforces Round #311 (Div. 2)题解

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Centos 6.2 32位机器安装新的JDK和Weblogic

    一.首先卸载自带的JDK 1.查看自带的java版本. root@admin]#java -version java version "1.6.0" OpenJDK Runtime ...

  2. stm32类型cl、vl、xl、ld、md、hd的含义

    - startup_stm32f10x_ld_vl.s: for STM32 Low density Value line devices - startup_stm32f10x_ld.s: for ...

  3. javascript debut trick, using the throw to make a interrupt(breakpoint) in your program

    console.log('initialize'); try { throw "breakPoint"; } catch(err) {} when I debug the extj ...

  4. VS Extension: Create a txt file and set the content

    使用 Visual Studio Extension 创建一个文本文件,并填入内容. 需要引用 EnvDTE C:\Program Files (x86)\Microsoft Visual Studi ...

  5. KVC vs KVO(内容为转载记录,整合大家的总结为我所用)

    KVC即key-value coding的缩写, KVO即key-value observing的缩写 假如需要掌握Key-Value Observing机制,那么需要阅读本文应该有帮助.本文提供了K ...

  6. 为sublime text2 添加SASS语法高亮

    以前写CSS时,都是直接写样式,没有任何的第三方工具,后面发现越是面向大网站,越难管理,上次参加完携程UED大会后,发现SASS对于前端团队多人协作和站点代码维护上很有帮助,很多同学都开始用了,我还是 ...

  7. throw 与 throws的应用

    throws---------->把异常交给调用处. 可以结合throw来同时使用. throws 用在方法声明处,表示本方法不处理异常.可以结合throw使用 throw 表示在方法中手工抛出 ...

  8. AT&T 和 Intel 汇编语法的主要区别

    转自AT&T 和 Intel 汇编语法的主要区别 作为一个爱折腾的大好青年,补番之余还要补一些 Linux 下的基础,比如 GDB 的正确使用方法.但无论是看 gdb 还是 gcc -S 里的 ...

  9. c#中virtual, abstract和override的区别和用法

    virtual是把一个方法声明为虚方法,使派生类可重写此方法,一般建立的方法是不能够重写的,譬如类A中有个方法protected void method(){ 原代码....;}类B继承自类A,类B能 ...

  10. MySQL db优化

    http://blog.csdn.net/likika2012/article/details/38816037 http://www.nowamagic.net/librarys/veda/deta ...