题目大意:给出一个 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. java maven、springmvc、mybatis 搭建简单Web项目学习笔记

    前言: 空余的时间,学学 Java,没准哪天用的到: 环境搭建折腾了好几天,总算搞顺了,也做个学习笔记,以防后面会忘记: 一.安装文件及介绍 JDK:jdk1.8.0 77 eclipse-maven ...

  2. Python学习之路day4-列表生成式、生成器、Iterable和Iterator

    一.列表生成式 顾名思义,列表生成式就是用于生成列表的特殊语法形式的表达式. 1.1 语法格式 [exp for iter_var in iterable] 工作过程: 1.通过iter_var迭代i ...

  3. AI探索(二)Tensorflow环境准备

    Python + Tensorflow环境安装 Tensorflow支持Windows/Mac/Linux等三种操作系统, 其中windows下python需要安装3.5以上的版本 Mac/Linux ...

  4. android中shape的属性

    <shape> <!– 实心 –> <solid android:color=”#ff9d77″/> <!– 渐变 –> <gradient an ...

  5. 让camera实现类似cs第一人称视角旋转和位移

    直接把这个脚本挂在摄像机上就可: using System.Collections; using System.Collections.Generic; using UnityEngine; /* * ...

  6. UVA 11291 Smeech

    [来源]https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. Wireshark图解教程(简介、抓包、过滤器)【转载】

    原文网址:http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据, ...

  8. vue.js初学(二)

    1:构造器 var app = new Vue ( { //选项 }) 注意点: (1) 之后会经常用vm代表Vue实例 (2)实例化之后 需要传入一个选项对象,它可以包括数据.模板.挂载元素.方法. ...

  9. es6变量的解构赋值学习笔记

    1. 解构赋值的规则是,只要等号右边的值不是对象,就先将其转为对象.由于undefined和null无法转为对象,所以对它们进行解构赋值,都会报错. let { prop: x } = undefin ...

  10. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...