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
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
Hint
The simple cycle is a cycle that doesn't contain any vertex twice.
题意:让你在一个图上添加最少的边,使图中有长度为奇数的环,输出添加的边数和方案数
思路:一个联通分量如果是二分图,那么它没有奇环,判断一个图是否是二分图可以染色判断,然后分成图最大度数0,1,>=2三种分别算就好了
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define maxn 305000
ll d[maxn],head[maxn],vis[maxn],cnt,root[maxn],rot,sum[maxn],p[maxn],flag=0;
struct node
{
ll to,next;
}edge[maxn];ll n;ll id=0;
void addedge(ll u,ll v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void init()
{
memset(p,0,sizeof(p));
memset(root,0,sizeof(root));
memset(sum,0,sizeof(sum));
memset(d,0,sizeof(d));
memset(head,-1,sizeof(head));
cnt=0;rot=0;flag=0;
}
void dfs(ll u)
{
root[u]=rot;
for(ll i=head[u];i!=-1;i=edge[i].next)
{
ll v=edge[i].to;
if(p[v]==p[u])
{
//printf("p[%lld]=%lld p[%lld]=%lld\n",u,p[u],v,p[v]);
flag=1;
}
if(root[v]==0)
{
p[v]=3-p[u];
dfs(v);
}
}
}
int main()
{
init();
ll n,m;
scanf("%lld %lld",&n,&m);
for(ll i=1;i<=m;i++)
{
ll u,v;
scanf("%lld %lld",&u,&v);
addedge(u,v);
addedge(v,u);
d[u]++;d[v]++;
}
ll ans=0,max1=0;
for(ll i=1;i<=n;i++)
{
if(root[i]==0)
{
++rot;
p[i]=1;
dfs(i);
}
}
if(flag==1)
{
printf("0 1\n");
return 0;
}
for(int i=1;i<=n;i++)
{
max1=max(max1,d[i]);
}
if(max1==0)
{
ans=n*(n-1)*(n-2)/2/3;
printf("3 %lld\n",ans);
}
else if(max1==1)
{
ans=m*(n-2);
printf("2 %lld\n",ans);
}
else if(max1>=2)
{
for(int i=1;i<=n;i++)
{
if(p[i]==1) sum[root[i]]++;
}
for(int i=1;i<=rot;i++)
{
ans+=sum[i]*(sum[i]-1)/2;
}
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
if(p[i]==2) sum[root[i]]++;
}
for(int i=1;i<=rot;i++)
{
ans+=sum[i]*(sum[i]-1)/2;
}
printf("1 %lld\n",ans);
}
}