题意简述

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

(注意不考虑自环)

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

一道二分图染色的讨论题

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

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

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

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

首先讨论添加边条数为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$

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

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define rep(i,n) for(int i=1;i<=n;++i)
#define imax(x,y) (x>y?x:y)
#define imin(x,y) (x<y?x:y)
using namespace std;
const int N=;
int firste[N],nexte[N<<],v[N<<];
int color[N],fa[N],bl[N],wh[N],degree[N];
int n,m,e=,flag=;
long long ans=;
void build_edge(int x,int y)
{
++e;
nexte[e]=firste[x];
firste[x]=e;
v[e]=y;
}
void dfs(int u,int c,int f)
{
color[u]=c;
fa[u]=f;
if(c&)++bl[f];
else ++wh[f];
for(int p=firste[u];p;p=nexte[p])
if(!color[v[p]])dfs(v[p],-c,f);
else if(color[v[p]]==color[u])
{
flag=;
return;
}
}
int main()
{
int x,y;
scanf("%d%d",&n,&m);
if(!m)
{
ans=(long long)n*(n-)*(n-)/;
printf("3 %I64d",ans);
return ;
}
rep(i,m)
{
scanf("%d%d",&x,&y);
build_edge(x,y);
build_edge(y,x);
++degree[x];
++degree[y];
if(degree[x]>||degree[y]>)flag=;
}
if(flag)
{
ans=(long long)m*(n-);
printf("2 %I64d",ans);
return ;
}
int cnt=;
rep(i,n)
if(!color[i])
{
dfs(i,,++cnt);
if(flag)
{
printf("0 1");
return ;
}
}
rep(i,cnt)
ans+=(long long)(wh[i]-)*wh[i]/+(long long)(bl[i]-)*bl[i]/;
printf("1 %I64d",ans);
return ;
}

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. poj1163The Triangle(动态规划,记忆化搜索)

    7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calc ...

  2. python while 循环打印九九乘法表

    方向一 i = 1 while i <= 9: j = 1 while j <= i print('%d*%d = %2d'%( j,i ,i*j),end='') j += 1 prin ...

  3. 微信小程序列表时间戳转换

    第一步先写js   随便命名为times.js function toDate(number){     var n=number * 1000;     var date = new Date(n) ...

  4. (转载)Solr4.x在Tomcat下的部署

    Step1 下载安装包: 下载最新版本安装包 点击此处下载Tomcat    点击此处下载Solr Step2 解压: 解压Tomcat和Solr Step3 拷贝War包: 拷贝\solr-4.x\ ...

  5. node进程一些信号的意义

    1.SIGINT这个信号是系统默认信号,代表信号中断,就是ctrl+c: 2.SIGQUIT 3.SIGTERM 4.exit

  6. secureCRT安装,破解,连接linux机器(含安装包)

    1. 为什么需要安装secureCRT 我们的项目运行在服务器上,如果出现什么问题,或者升级的时候,需要对服务器进行操作,把处理好的项目发布到服务器上.如果我们就在服务器身边,那么直接在服务器上进行操 ...

  7. php 模拟登陆(不带验证码)采集数据

    这里模拟表单登陆窗口 提交代码部分 1,生成session_id保存到 cookie $login_url = 'http://www.96net.com.cn/Login.php';$cookie_ ...

  8. java 进销存 商户管理 系统 管理 库存管理 销售报表springmvc SSM项目

    统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3  SSM 普 ...

  9. 从“中产梦”中醒来,好好打工吧

    "中产"定义 自打"中产阶级/阶层"概念出现,总有人试图给出定义.搞不清何为"中产"却试图定义"中产阶级/阶层",注定是 ...

  10. 解决chrome浏览器安装不上的问题

    1.  打开注册表:  windows键 + R --> 输入regedit -->  回车 (注:windows键在左ctrl附近微软图标的键) 2.  找到 32位:HKEY_LOCA ...