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. 浅谈GFC

    Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Columns)”.“伸缩布局(Fle ...

  2. 通过Oracle函数SQL实现C# String.Format字符串格式化功能

    语言国际化要求,开发上要求Oracle数据库SQL中对应的返回信息-Message,实现一个通用函数调用,比如:提示信息内容:条码123456当前工站在FCT!”,即通用的信息内容格式化标准为:“条码 ...

  3. Leetcode747至少是其他数字两倍的最大数

    Leetcode747至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 .查找数组中的最大元素是否至少是数组中每个其他数字的两倍.如果是,则返回最大元素的索引,否则返回-1 ...

  4. Oracle中的COALESCE,NVL,NVL2,NULLIF函数

    http://jingyan.baidu.com/article/fa4125acaf898e28ac7092b9.html

  5. <SpringMvc>入门二 常用注解

    1.@RequestMapping @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME ...

  6. JDK的下载---官方

    1.去到官方网站 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 根据箭头选择, ...

  7. mysql运维常用

    一.用户授权 用户授权主要指: 1.可以限制用户访问那些库.表 2.可以限制用户对库.表执行select.create.delete.alter.drop等操作 3.可以限制用户登陆的IP.IP段.或 ...

  8. linux(Ubuntu16)下切换python2和python3(转)

    采用update-alternatives 切换版本 1.打开终端:Ctrl+Alt+T 2.查看update-alternatives的帮助信息:update-alternatives --help ...

  9. ORM优化

    orm优化数据库访问:https://docs.djangoproject.com/en/1.11/topics/db/optimization/ 一.QuerySet 可迭代 querysey=mo ...

  10. 52.基于doc value正排索引的聚合内部原理

    主要知识点: 本节没有太懂,以后复习时补上       聚合分析的内部原理是什么????aggs,term,metric avg max,执行一个聚合操作的时候,内部原理是怎样的呢?用了什么样的数据结 ...