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

本文探讨了一个关于图论的问题,即如何在给定的无向图中通过最少的边添加形成奇数长度的简单环,并计算可能的方式数量。文章提供了一种解决思路,包括对输入数据的处理和算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. 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 aibi(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.

分情况讨论。二分图、

重点是 添加一条边的时候。

/* ***********************************************
Author        :guanjun
Created Time  :2016/8/17 22:09:03
File Name     :cf311d.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
vector<int>edge[maxn];
int in[maxn];
int col[maxn];
ll a[4];
bool dfs(int u){
    a[col[u]]++;
    for(int i=0;i<edge[u].size();i++){
        int v=edge[u][i];
        if(col[u]==col[v])return false;
        if(!col[v]){
            col[v]=3-col[u];
            if(!dfs(v))return false;
        }
    }
    return true;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    ll n,m;
    int x,y;
    cin>>n>>m;
    cle(in);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        edge[x].push_back(y);
        edge[y].push_back(x);
        in[x]++;
        in[y]++;
    }
    if(m==0){
        cout<<3<<" "<<n*(n-1)*(n-2)/6<<endl;
    }
    else{
        ll mark=0,cnt=0;
        for(int i=1;i<=n;i++){
            if(in[i]>=2){
                mark=1;break;
            }
            else if(in[i]==1)cnt++;
        }
        if(!mark){
            cout<<2<<" "<<(n-2)*(cnt/2LL)<<endl;return 0;
        }
        cle(col);
        ll ans=0;
        mark=0;
        for(int i=1;i<=n;i++){
            if(col[i]==0){
                col[i]=1;
                cle(a);
                if(dfs(i)){
                    ans+=(ll)(a[1]*(a[1]-1)/2LL+a[2]*(a[2]-1)/2LL);
                }
                else {
                    mark=1;break;
                }
            }
        }
        if(mark) cout<<0<<" "<<1<<endl;
        else cout<<1<<" "<<ans<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/pk28/p/5782362.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值