D. Vitaly and Cycle

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/557/problem/D

Description

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.

Sample Input

4 4
1 2
1 3
4 2
4 3

Sample Output

1 2

HINT

题意

给你一个无向图,让你加最少的边,形成一个奇数环

问最少加的边数,以及加边方法数

题解:

m=0的话,要加三条边

方案数是C(n,3)

然后检查是否有某个点度数>=2

若否,要加2条边

方案数m*(n-2)

剩下就dfs黑白染色乱搞就行了,有奇环就不加边,一个方案

否则黑白染色,每个连通块对方案数的贡献是C(黑点数,2)+C(白点数,2)

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 300005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** vector<int> e[maxn];
int d[maxn];
int f[maxn],p[maxn];
int a,b,t,n,m;
pair<ll,ll> v[maxn];
void dfs(ll x,ll c)
{
f[x]=c;
p[x]=t;
if(c==)
a++;
if(c==)
b++;
for(int i=;i<e[x].size();i++)
{
if(!f[e[x][i]])
dfs(e[x][i],-c+);
}
}
int main()
{
n=read(),m=read();
if(m==)
{
printf("3 %lld\n",(ll)n*(n-)*(n-)/);
return ;
}
int flag=;
for(int i=;i<=m;i++)
{
int a=read(),b=read();
e[a].push_back(b);
e[b].push_back(a);
d[a]++;
d[b]++;
if(d[a]>||d[b]>)
flag=;
v[i]=make_pair(a,b);
}
if(!flag)
{
printf("%d %lld\n",,(ll)(n-*m)*m+(ll)(*m)*(m-));
return ;
}
ll ans=;
for(int i=;i<=n;i++)
{
if(!f[i])
{
a=,b=;
t++;
dfs(i,);
ans+=(ll)a*(a-)/+(ll)b*(b-)/;
}
}
for(int i=;i<=m;i++)
{
if(p[v[i].first]==p[v[i].second]&&f[v[i].first]==f[v[i].second])
{
cout<<"0 1"<<endl;
return ;
}
}
cout<<""<<" "<<ans<<endl;
}

Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论的更多相关文章

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

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

  2. 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 ...

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

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

  4. Codeforces Round #311 (Div. 2) A,B,C,D,E

    A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdli ...

  5. Codeforces Round #311 (Div. 2)题解

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #330 (Div. 2) A. Vitaly and Night 暴力

    A. Vitaly and Night Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/p ...

  7. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  8. Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset

    C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  9. Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题

    B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...

随机推荐

  1. 从date中获取相应信息

    创建测试用表: CREATE OR REPLACE VIEW v AS SELECT TO_DATE('2015-5-5 13:14:15', 'YYYY-MM-DD HH24:MI:SS') AS ...

  2. 项目管理及自动构建工具Maven

    项目管理及自动构建工具Maven 一.Maven安装.目录结构.cmd命令1.下载安装apache-maven-3.2.3-bin.zip下载:http://maven.apache.org/down ...

  3. R 环境内存限制的更改

    由于R语言非常消耗内存,所以做较大数据的处理时需要增加内存空间,有以下种方式: 一. 在未开启R之前,在cmd中,输入下面指令 r −−max−mem− s i z e =4Gb 二. 在开启R之后, ...

  4. 说说Python 中的文件操作 和 目录操作

    我们知道,文件名.目录名和链接名都是用一个字符串作为其标识符的,但是给我们一个标识符,我们该如何确定它所指的到底是常规文件文件名.目录名还是链接名呢?这时,我们可以使用os.path模块提供的isfi ...

  5. WCF基礎

    參考:http://www.cnblogs.com/MeteorSeed/archive/2012/04/24/2399455.html http://www.cnblogs.com/scy25114 ...

  6. s3c2440串口裸板驱动(使用fifo)

    使用fifo的好处有: 1:串口的数据发送的数据量较大时,使用fifo可以大大降低MCU的开销.(有点类似串入并出的cput处理模型,本质上还是串行收发) 2:在某些特殊场合,例如制定较复杂的协议时, ...

  7. struts2类库下载

    struts2开发包下载 到http://struts.apache.org/download.cgi#struts2014下载struts-2.x.x-all.zip,目前最新版为2.1.6.下载完 ...

  8. Jersey Rest服务类型

    在Rest服务中,资源类是接收Rest请求并完成响应的核心类,而资源类由Rest服务的“提供者”来调度的,这一定义类似于自定义Servlet类,该类会奖请求分派给指定的Controller/Actio ...

  9. 使用gulp、yeoman、bower建站

    前端建站工具 标签 : 工具 *** 脚手架:yeoman 用途 快速搭建新项目 为项目增加新部分 创建模块或者包 引导新服务 ... 开始 安装yo和generator npm i -g yo np ...

  10. HDU-3401 Trade 单调队列优化DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...