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

D. Vitaly and Cycle
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

分这么几种情况
第一种没有边
第二张度数最多1
第三种就是树

为啥分这三种的原因是因为第一种明显是特判不多bb
第二种只有两条不能更少
第三种一条不能更少

#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int>tu[100001];
int kuai[100001],ranse[100001],ggen[100001],ji[100001],ou[100001],er[100001];
void dfs(int gen,int kua)
{
    kuai[gen]=kua;
    er[kua]++;
    for(int a=0;a<tu[gen].size();a++)
    {
        if(kuai[tu[gen][a]])continue;
        dfs(tu[gen][a],kua);
    }
}
int rrr=0;
void rrs(int gen,int die,int rs)
{
    ranse[gen]=rs;
    for(int a=0;a<tu[gen].size();a++)
    {
        if(tu[gen][a]==die)continue;
        if(ranse[tu[gen][a]])
        {
            if(ranse[tu[gen][a]]%2!=(ranse[gen]+1)%2)
            {
                rrr=1;
            }
            continue;
        }
        rrs(tu[gen][a],gen,rs+1);
    }
}
main()
{
    int n,m;
    cin>>n>>m;
    int q,w;
    for(int a=1;a<=m;a++)
    {
        scanf("%I64d%I64d",&q,&w);
        tu[q].push_back(w);
        tu[w].push_back(q);
    }
    int st=0;
    for(int a=1;a<=n;a++)
    {
        if(kuai[a])continue;
        ggen[++ggen[0]]=a;
        dfs(a,++st);
    }
    int qd0=1,qd1=1;
    for(int a=1;a<=n;a++)
    {
        if(tu[a].size()>0)qd0=0;
        if(tu[a].size()>1)qd1=0;
    }
    if(qd0)
    {
        cout<<3<<" "<<n*(n-1)*(n-2)/6;
        return 0;
    }
    if(qd1)
    {
        cout<<2<<" ";
        int qwe=0;
        for(int a=1;a<=n;a++)
        {
            if(tu[a].size())qwe++;
        }
        qwe/=2;
        cout<<qwe*(n-2);
        return 0;
    }
    for(int a=1;a<=ggen[0];a++)
    {
        if(er[a]<=2)continue;
        rrs(ggen[a],0,1);
        if(rrr)
        {
            cout<<0<<" "<<1;
            return 0;
        }
    }
    for(int a=1;a<=n;a++)
    {
        if((!ranse[a])||er[kuai[a]]<3)continue;
        if(ranse[a]%2)
        {
            ji[kuai[a]]++;
        }
        else ou[kuai[a]]++;
    }
    int jieg=0;
    for(int a=1;a<=ggen[0];a++)
    {
        if(er[a]<3)continue;
        jieg+=(ji[a]*(ji[a]-1)+ou[a]*(ou[a]-1))/2;
    }
    cout<<1<<" "<<jieg;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值