题意简述

给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数

(注意不考虑自环)

-----------------------------------------------------------------------------

一道二分图染色的讨论题

比赛时只会用二分图染色判断树以及偶环 忘记用这个来判奇环。。。

二分图染色这种联赛知识点的题目现在也不会写了。。。

------------------------------------------------------------------------------

我们可以按需要添加边的条数来讨论这题

首先讨论添加边条数为3——即原原图边数m为0时

$ans=n*(n-1)*(n-2)/6$

再讨论添加边条数为2——即原图中所有边都没有公共端点/所有点度数<=1 时

$ans=m*(n-2)$

再讨论添加边数为0——即原图中存在奇环时

$ans=1$

最后讨论添加边数为1——即原图中只有树以及偶环

$ans=\sum(white[i]-1)*white[i]/2+(black[i]-1)*black[i]/2$

其实思路清晰后实现起来就很容易了

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. #define rep(i,n) for(int i=1;i<=n;++i)
  6. #define imax(x,y) (x>y?x:y)
  7. #define imin(x,y) (x<y?x:y)
  8. using namespace std;
  9. const int N=;
  10. int firste[N],nexte[N<<],v[N<<];
  11. int color[N],fa[N],bl[N],wh[N],degree[N];
  12. int n,m,e=,flag=;
  13. long long ans=;
  14. void build_edge(int x,int y)
  15. {
  16. ++e;
  17. nexte[e]=firste[x];
  18. firste[x]=e;
  19. v[e]=y;
  20. }
  21. void dfs(int u,int c,int f)
  22. {
  23. color[u]=c;
  24. fa[u]=f;
  25. if(c&)++bl[f];
  26. else ++wh[f];
  27. for(int p=firste[u];p;p=nexte[p])
  28. if(!color[v[p]])dfs(v[p],-c,f);
  29. else if(color[v[p]]==color[u])
  30. {
  31. flag=;
  32. return;
  33. }
  34. }
  35. int main()
  36. {
  37. int x,y;
  38. scanf("%d%d",&n,&m);
  39. if(!m)
  40. {
  41. ans=(long long)n*(n-)*(n-)/;
  42. printf("3 %I64d",ans);
  43. return ;
  44. }
  45. rep(i,m)
  46. {
  47. scanf("%d%d",&x,&y);
  48. build_edge(x,y);
  49. build_edge(y,x);
  50. ++degree[x];
  51. ++degree[y];
  52. if(degree[x]>||degree[y]>)flag=;
  53. }
  54. if(flag)
  55. {
  56. ans=(long long)m*(n-);
  57. printf("2 %I64d",ans);
  58. return ;
  59. }
  60. int cnt=;
  61. rep(i,n)
  62. if(!color[i])
  63. {
  64. dfs(i,,++cnt);
  65. if(flag)
  66. {
  67. printf("0 1");
  68. return ;
  69. }
  70. }
  71. rep(i,cnt)
  72. ans+=(long long)(wh[i]-)*wh[i]/+(long long)(bl[i]-)*bl[i]/;
  73. printf("1 %I64d",ans);
  74. return ;
  75. }

codeforces 557D Vitaly and Cycle的更多相关文章

  1. CodeForces - 557D Vitaly and Cycle(二分图)

    Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. codeforces 557D. Vitaly and Cycle 二分图染色

    题目链接 n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式. 具体看代码解释 #include<bits/stdc++.h> using names ...

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

  4. codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图

    D. Vitaly and Cycle       time limit per test 1 second memory limit per test 256 megabytes input sta ...

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

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

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

  7. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)

    http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...

  9. codeforces 518A. Vitaly and Strings

    A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. Convolutional Neural Networks(2):Sparse Interactions, Receptive Field and Parameter Sharing

    Sparse Interactions, Receptive Field and Parameter Sharing是整个CNN深度网络的核心部分,我们用本文来具体分析其原理. 首先我们考虑Feedf ...

  2. [Linux] 013 其他文件搜索命令

    1. 文件搜索命令:locate 命令名称:locate 命令所在路径:/bin/locate 执行权限:所有用户 语法:locate 文件名 功能描述:在文件资料库中查找文件 范例: $ locat ...

  3. from、includes、indexOf

    from.includes.indexOf:https://blog.csdn.net/j59580/article/details/53897630?utm_source=blogxgwz1 语法 ...

  4. 1、引言(Introduction)

    1.1 欢迎 在生活中用到的机器学习算法: (1)打开谷歌.必应搜索到你需要的内容,正是因为他们有良好的学习算法 (2)每次您阅读您的电子邮件垃圾邮件筛选器,可以帮你过滤大量的垃圾邮件 机器学习为什么 ...

  5. LayaBox 常用技巧

    1.修改IDE的菜单 找到安装路径的LayaAirIDE\resources\app\out\vs\layaEditor\renders\laya.editorUI.xml 注意事项: 1.mask的 ...

  6. window下 nginx 80端口被占用

    问题:启动nginx没有反应,查看日志提示 bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in ...

  7. JS中对象数据类型的基本结构和操作

    Object类型 ECMAScript中的队形其实就是一组数据和功能的集合.对象可以通过执行new操作符后跟要创建的对象类型的名称来创建.而创建Object类型的示例并为其添加属性和(或)方法,就可以 ...

  8. constructor、prototype、isPrototypeOf、instanceof、in 、hasOwnProperty

    constructor.prototype.isPrototypeOf.instanceof.in .hasOwnProperty等等 constructor:对象构造器.存在于原型对象中?,相当于p ...

  9. python 异常处理【转载】

    什么是异常?异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当Python脚本 ...

  10. shell简单的菜单功能