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/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=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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==1)
        a++;
    if(c==2)
        b++;
    for(int i=0;i<e[x].size();i++)
    {
        if(!f[e[x][i]])
            dfs(e[x][i],2-c+1);
    }
}
int main()
{
    n=read(),m=read();
    if(m==0)
    {
        printf("3 %lld
",(ll)n*(n-1)*(n-2)/6);
        return 0;
    }
    int flag=0;
    for(int i=1;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]>1||d[b]>1)
            flag=1;
        v[i]=make_pair(a,b);
    }
    if(!flag)
    {
        printf("%d %lld
",2,(ll)(n-2*m)*m+(ll)(2*m)*(m-1));
        return 0;
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!f[i])
        {
            a=0,b=0;
            t++;
            dfs(i,1);
            ans+=(ll)a*(a-1)/2+(ll)b*(b-1)/2;
        }
    }
    for(int i=1;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 0;
        }
    }
    cout<<"1"<<" "<<ans<<endl;
}
原文地址:https://www.cnblogs.com/qscqesze/p/4612226.html