题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种。

解题关键:黑白染色求奇环,利用数量分析求解。

奇环:含有奇数个点的环。

二分图不存在奇环。反之亦成立。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int maxm=1e5+; int c[maxn]; //color,每个点的黑白属性,-1表示还没有标记,0/1表示黑白,比vis数组多一个作用
ll num[]; //在一次DFS中的黑白点个数
bool f=; //判断是否出现奇环 int head[maxn],tot,n,m,a,b;
struct edge{
int to;
int nxt;
}e[*maxm];
void add_edge(int u,int v){
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
void init(){
memset(head,-,sizeof head);
tot=;
memset(c,-,sizeof c);
} void dfs(int u,int x){
if(f)return;
c[u]=x;
num[x]++;
for(int i=head[u];~i;i=e[i].nxt){
int j=e[i].to;
if(c[j]==-) dfs(j,!x);
else if(c[j]==x){//存在奇环
f=;
return;
}
}
}
int main(){
init();
cin>>n>>m;
for(int i=;i<=m;i++){
cin>>a>>b;
add_edge(a,b);
add_edge(b,a);
}
ll ans=,ans2=;
if(m==){
ans=(ll)n*(n-)*(n-)/;
printf("3 %lld\n",ans);
return ;
}
for(int i=;i<=n&&(!f);i++){
if(c[i]==-){
num[]=num[]=;
dfs(i,);
ans+=(num[]*(num[]-)+num[]*(num[]-))/;
if(num[]==&&num[]==){
ans2+=n-;
}
}
}
if(f) printf("0 1\n");
else if(ans) printf("1 %lld\n",ans);
else printf("2 %lld\n",ans2);
return ;
}

[cf557d]Vitaly and Cycle(黑白染色求奇环)的更多相关文章

  1. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

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

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

  3. fzu2181(点的双连通分量+求奇环)

    求出每个点双连通分量,如果在一个点双连通分量中有奇环,则这个分量每个点都在一个奇环中.  关键是要知道怎么求点双连通分量以及点双连通的性质. fzu2181 http://acm.fzu.edu.cn ...

  4. Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

    题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...

  5. codeforce D. Shortest Cycle(floyd求最短环)

    题目链接:http://codeforces.com/contest/1206/problem/D 给n个点,如果点a[ i ] &a[ j ] 不为0,则点a[ i ] 和 a[ j ] 直 ...

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

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

  7. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

  8. hdu 1689 求奇环bfs关键是层次图

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> usin ...

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

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

随机推荐

  1. POJ - 2195 最小费用最大流

    题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...

  2. Git之(一)Git是什么[转]

    为什么使用Git 孔子曾经曰过的,名正则言顺 言顺则事成. 我们在学习一项新技术之前,弄清楚为什么要学它至关重要,至于为什么要学习Git,我用一段if-else语句告诉你原因: if(你相信我){ 我 ...

  3. SQL Compare

    http://pan.baidu.com/s/1jGIEEm6 来源:数据库同步工具: Red Gate Sql Compare

  4. asp.net上传文件大小限制

    <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedConte ...

  5. 打印控件Lodop

    官网:http://www.lodop.net/demo.html Lodop.C-Lodop使用说明及样例   Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又 ...

  6. BZOJ4808: 马

    BZOJ4808: 马 https://lydsy.com/JudgeOnline/problem.php?id=4808 分析: 黑白染色,求二分图最大匹配即可. 代码: #include < ...

  7. virtualvm一次插件安装想到的

    在麒麟操作系统visualvm安装插件失败,因为使用的内网,所以在官网下载了插件到本地:因为本地安装的jdk1.6,为了享受jdk1.8,在visualvm文件中增加了对于jdk1.8的引用: exp ...

  8. asp.net给按钮添加删除确认

    if (!IsPostBack) { BtnDel.Attributes["onclick"] = "javascript:return window.confirm(' ...

  9. Mina APR传输与串口传输讲解---zhengli

    原文地址:Mina Transports(Apache Mina user guide Chapter6 Transport) 1.1.1.APR传输 1.1.1.1.介绍 APR (Apache P ...

  10. perform-maintence-on-replica-set-members

    https://docs.mongodb.com/v3.0/tutorial/perform-maintence-on-replica-set-members/ 1 oplog 改变大小 --详见mo ...