Vitaly and Cycle

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

After Vitaly was expelled from the university, he became interested in the graph theory.

Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.

Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w — the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.

Two ways to add edges to the graph are considered equal if they have the same sets of added edges.

Since Vitaly does not study at the university, he asked you to help him with this task.

Input

The first line of the input contains two integers n and m ( — the number of vertices in the graph and the number of edges in the graph.

Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n) — the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.

It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.

Output

Print in the first line of the output two space-separated integers t and w — the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.

Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
1 2
Input
3 3
1 2
2 3
3 1
Output
0 1
Input
3 0
Output
3 1
Note

The simple cycle is a cycle that doesn't contain any vertex twice.

题意:给你一个n个节点m条边的图 问你是不是存在一个奇数环(就是环中的节点个数为奇数个)

如果存在输出0 1

如果不存在 输出最少加多少条边使得存在一个奇数环 并输出他的方案数

当一个图是二分图的话  他是一定不存在奇数环的  反之  他就一定存在奇数环

0 1染色判断是不是二分图

如果是二分图的话 也许是多个联通块  所以我们只需要统计各个联通块中0 1中的个数 a[i] b[i]  答案就是各个联通块的 a(a-1)/2+b(b-1)/2的和

当然 有两种情况是要讨论的  m=0 不存在边  所以就是任意三个点可以组成一个奇数环 边就是加3条

还是一种已经所有联通块中节点数最多就只有两个  答案就是 (n-2)*m

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int N=+;
int head[N];
int tot;
struct node{
int to,next;
}edge[N<<];
int color[N];
int vis[N];
int a[N];
int b[N];
int num[N];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int DFS(int u,int t){
if(vis[u]==){
if(color[u]==)a[t]++;
if(color[u]==)b[t]++;
num[t]++;
}
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(color[v]==){
color[v]=color[u]^;
if(DFS(v,t)==)return ;
}
else if(color[u]==color[v]){
return ;
}
}
return ;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
if(m==){
cout<<<<" "<<(ll)n*(n-)*(n-)/<<endl;return ;
}
memset(color,,sizeof(color));
memset(vis,,sizeof(vis));
int flag=;
int t=;
for(int i=;i<=n;i++){
if(color[i]==&&vis[i]==){
if(DFS(i,++t)==){
color[i]=;
flag=;
break;
}
}
}/*
for(int i=1;i<=n;i++){
cout<<color[i]<<" "<<endl;
}
for(int i=1;i<=t;i++){
cout<<a[i]<<" "<<b[i]<<" "<<num[i]<<endl;
}*/
if(flag==){
cout<<<<" "<<<<endl;return ;
}
ll ans=;
flag=;
for(int i=;i<=t;i++){
if(num[i]<=){
flag++;continue;
}
ans=ans+(ll)a[i]*(a[i]-)/+(ll)b[i]*(b[i]-)/;
//cout<<ans<<endl;
}
if(flag!=t)cout<<<<" "<<ans<<endl;
else{
cout<<<<" "<<(ll)m*(n-)<<endl;
} }

CodeForces - 557D Vitaly and Cycle(二分图)的更多相关文章

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

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

  2. codeforces 557D Vitaly and Cycle

    题意简述 给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数 (注意不考虑自环) ---------------------------------------------------- ...

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

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

  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: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  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. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环

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

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

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

  9. CodeForces 173B Chamber of Secrets 二分图+最短路

    题目链接: http://codeforces.com/problemset/problem/173/B 题意: 给你一个n*m的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...

随机推荐

  1. 并发和多线程(三)--并发容器J.U.C和lock简介

    AQS: 是AbstractQueuedSynchronizer的简称,JUC的核心 底层是sync queue双向链表,还可能有condition queue单向链表,使用Node实现FIFO队列, ...

  2. ThinkPHP---thinkphp视图(V)

    配置文件分3类:系统配置文件,分组配置文件,应用配置文件 ①系统配置文件ThinkPHP/Conf/convention.php: ②分组 / 模块 /平台配置文件Home/Conf/config.p ...

  3. C语言编辑编译及集成开发环境

    C语言编辑编译及集成开发环境 编辑器 在不同的操作系统上使用不同的编辑器,保存源代码文件时,文件名应指出程序的功能扩展名应为.c. 编译器 编译器把源代码编译成机器语言的二进制指令即目标代码生成目标文 ...

  4. Java基础概念语法

    Java基础概念语法 注释 单行注释 //行注释说明 多行注释 /* 多行注释说明 */ 文档注释 /** *@author 程序的作者 *@version 源文件的版本 *@param 方法的参数说 ...

  5. 04Oracle Database 登陆

    Oracle Database 登陆 EM Express Login https://localhost:5500/em/login cmd sqlplus SQL/PLUS system/code ...

  6. 使用Sophus练习李群SO3、SE3以及对应的李代数so3、se3

    这是高博<视觉SLAM14讲,从理论到实践>第4章的练习.加了一些注释和理解: #include <iostream>#include <cmath>using n ...

  7. POJ P2096 Collecting Bugs

    思路 分类讨论,不妨先设$DP[i][j]$表示已经发现$i$种子系统中有$n$种$bug$无非只有四种情况 发现的$bug$在旧的系统旧的分类,概率$p1$是$(i/s)*(j/n)$. 发现的$b ...

  8. 07.C语言:结构体、共用体、枚举

    一.结构体 是一种复合的数据类型,由多个不同类型的数据(为结构体的成员)组成的集合. 在c语言中没有给出结构体这种类型具体的形式(名称),但是给出类定义该结构体类型的方法(格式). 在使用结构体类型时 ...

  9. 782B The Meeting Place Cannot Be Changed(二分)

    链接:http://codeforces.com/problemset/problem/782/B 题意: N个点,需要找到一个点使得每个点到这个点耗时最小,每个点都同时开始,且都拥有自己的速度 题解 ...

  10. 聊聊餐饮: 2016年,是我做生意9年来,最差的1年 by某老板

    晚上忙完事,在小区里点了个菜.  今年在这个小店点菜,基本没有等过.  比较好奇,就问了下老板,最近怎么没人. 经常在这个店吃饭,老板就和我多聊了几句. 2016年,是我做生意9年来,最差的1年.还好 ...