题目大意:给出一个 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. JProfiler连接weblogic

    转 http://blog.csdn.net/xu1314/article/details/7737236

  2. 【Prism】MEF版UIComposition

    引言 UIComposition原版Demo在PrismV5的解压包里面.原Demo用了.net4.5版本的DLL,我改成.net4.0的. RegionContext 这个Demo比之前那几个示例大 ...

  3. MariaDB Galera Cluster环境搭建及高可用测试

    一.服务器概况Galera Cluster需要至少三个节点,在此次实验过程中,三个节点IP地址:192.168.56.101192.168.56.102192.168.56.103OS为centos ...

  4. Spring_总结_03_装配Bean(三)_XML配置

    一.前言 本文承接上一节:Spring_总结_03_装配Bean(二)之Java配置 上一节说到,当需要显示配置时,首选类型安全并且比XML更强大Java配置. 那什么时候使用XML配置呢? (1)维 ...

  5. kylin_异常_01_java.io.FileNotFoundException: /developer/apache-kylin-2.3.0-bin/tomcat/conf/.keystore

    一.异常现象 kylin安装完,启动后,控制正常,kylin后台也能正常访问.但是去看kylin的日志,却发现报错了: SEVERE: Failed to load keystore type JKS ...

  6. LeetCode OJ:Subsets(子集)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  7. sass进阶篇

    @if @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块.在 Sass 中除了 @if 之,还可以配合 @ ...

  8. I.MX6 make menuconfig进入x86模式

    /************************************************************************ * I.MX6 make menuconfig进入x ...

  9. 学习动态性能表(19)--v$undostat

    学习动态性能表 第19篇--V$UNDOSTAT  2007.6.14 本视图监控当前实例中undo空间以及事务如何运行.并统计undo空间开销,事务开销以及实例可用的查询长度. V$UNDOSTAT ...

  10. Eclipse之Web工程探究以及格式化

    1. 关于部署 只要配置了Web Deployment Assembly,可以不需要手工拷贝引用jar到/WEB-INF/lib里面了,之前失败是因为引用工程的output路径有问题导致的,修改完成后 ...