Codeforces Round #311 (Div. 2) D DFS+染色



链接:戳这里


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.


题意:

给出一个图,要求边最少数目的边使得图上存在奇环,保证不会有重边和自环


思路:

四种情况:第一种是已经存在奇环

第二种是0条边,这两种都好求

第三种是u->v->w:只需要添加一条u->w的边就会使存在奇环,染色这样的位置有多少个,然后C(num,2)

第四种是只有u->v:需要补充两条边的情况,直接没出现一次ans+=n-2


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
struct edge{
    int v,next;
}e[200100];
int tot=0,head[100100];
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
int flag=0,color[100100];
int num1=0,num2=0;
void DFS(int u){
    if(color[u]==1) num1++;
    else num2++;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(color[v]==-1){
            color[v]=color[u]^1;
            DFS(v);
        } else if(color[v]==color[u]){
            flag=1;
            return ;
        }
    }
}
int main(){
    mst(head,-1);
    mst(color,-1);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        Add(u,v);
        Add(v,u);
    }
    if(m==0){
        ll ans=(ll)(n-1)*(n-2)*n/6;
        cout<<3<<" "<<ans<<endl;
        return 0;
    }
    ll ans1=0,ans2=0;
    for(int i=1;i<=n;i++){
        num1=num2=0;
        if(color[i]==-1){
            color[i]=1;
            DFS(i);
        } else continue;
        if(flag){
            cout<<0<<" "<<1<<endl;
            return 0;
        }
        if(e[head[i]].next==-1) ans2+=n-2;
        ans1+=(ll)num1*(num1-1)/2+(ll)num2*(num2-1)/2;
    }
    if(ans1) printf("1 %I64d\n",ans1);
    else printf("2 %I64d\n",ans2);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值