Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图

来源:互联网 发布:数据挖掘应用案例 编辑:程序博客网 时间:2024/05/07 05:20

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.

Sample test(s)
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:如果只有单个的点形成的图,那任选三个点就可以组成一个奇连环,总个数就是c(n,3);
2:最多只有两个点连在一起(只需要判定入度最大只有1就可以了),两个点连在一起的个数是m个,则这两个点,再加上任意别的一个点可以组成一条奇边环,最大只要加二条边,方案数是m * (n-2);
3:存在三个点一上的图,这样的图如果存在奇环,那么不要加边答案就是0,1,如何判断是否有奇环呢,有奇环就不是二分图,如果没有奇环,就一定是二分图了,所有只要dfs,染色成x y集合,如果能构成二分图,说明没有奇环,答案就是sum(c(x,2)+ c(y,2)),也就是在x集合任选两点连一条边构成奇环,y集合任选两点连一条边构成奇环。
全部就解决了,总的复杂度为o(m),因为每个点,边,最多走一次。

#define N 100050#define M 100050#define maxn 205#define MOD 1000000000000000007int n,m,x,y,in[N],out[N],color[N];ll ans = 0,ansCount[2];vector<int> p[N];bool isOne(){    ans = (ll)n * ((ll)n-1) * ((ll)n-2) / 6;    FI(n){if(in[i] > 0) return false;}    return true;}bool isTwo(){    ll mm = 0;    FI(n){        if(in[i] >= 2 ) return false;        else if(in[i] == 1)        mm++;    }    ans = mm * ((ll)n-2ll)/2ll;    return true;}bool DFS(int pos,int flag){    color[pos] = flag;    ansCount[flag]++;    FI(p[pos].size()){        int goal = p[pos][i];        if(color[goal] == -1){            if(!DFS(goal,flag ^ 1))            return false;        }        else if(color[goal] != (flag^1))            return false;    }    return true;}ll getC2(ll x){    if(x <= 1) return 0;    return x * (x - 1) /2;}ll FindTwo(int pos){    ansCount[0] = ansCount[1] = 0;    if(!DFS(pos,0)) return -1;    else {        return getC2(ansCount[0]) + getC2(ansCount[1]);    }}int main(){    while(S2(n,m)!=EOF)    {        FI(n){p[i].clear();}        memset(in,0,sizeof(in));        memset(out,0,sizeof(out));        memset(color,-1,sizeof(color));        FI(m){            S2(x,y);x--;y--;            p[x].push_back(y);p[y].push_back(x);in[x]++;out[x]++;in[y]++;out[y]++;        }        if(isOne()){cout<<"3 "<<ans<<endl;}        else if(isTwo()){cout<<"2 "<<ans<<endl;}        else {            ans = 0;            FI(n){                if(color[i] == -1){                    ll temp = FindTwo(i);                    if(temp == -1){                        ans = -1;                        break;                    }                    ans += temp;                }            }            if(ans == -1)            cout<<"0 1"<<endl;            else            cout<<"1 "<<ans<<endl;        }    }    return 0;}
0 0
原创粉丝点击