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.
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.
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.
4 4 1 2 1 3 4 2 4 3
1 2
3 3 1 2 2 3 3 1
0 1
3 0
3 1
The simple cycle is a cycle that doesn't contain any vertex twice.
题目大意:
给你一个无向图,其中有n个点,m条边,然你加最少的边使得图中存在奇数点个数的环,并且输出方案数。
思路:
1、奇数点个数的环,直接考虑二分图,如果原图是一个二分图,那么肯定要加边,如果原图不是一个二分图,那么就无需加边,因为原图中一定存在奇数点个数的环。
那么我们首先二分染色,如果可以染色,继续判断,否则输出0 1.
2、如果可以染色,我们分成几种情况讨论:
①max(degree【i】)==0.那么我们任意选三个点,加三条边,形成一个三个点的环即可。ans= 3 C(n,3)
②max(degree【i】)==1.那么我们明显需要加两条边,对应我们每一条边原图中的边,对应可以随意找除了这两个点以外的任意一个点进行环即可(依然是组成三个点的环) ans=2 m*(n-2);
③max(degree【i】)==2.那么我们明显只要加一条边就行,对应我们每一个连通块中的同颜色点之间可以相互连接,就是在同颜色边任取两个点即可。
那么过程中我们维护一个数组root【i】,表示节点i属于的连通块编号是多少。
那么对应同一颜色同一root【i】的点之间可以任意取两个点进行连接,增加的方案数就是C(这个连通块中同颜色的点的个数,2);
过程维护一下即可,对应两种颜色的点都要处理。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define ll __int64
vector<int >mp[100050];
int vis[100050];
int color[100050];
int degree[100050];
int sum[100050];
int root[100050];
int flag,rot;
void Dfs(int u)
{
vis[u]=1;
root[u]=rot;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(vis[v]==0)
{
color[v]=3-color[u];
Dfs(v);
}
else if(color[v]==color[u])flag=0;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
flag=1;
memset(color,0,sizeof(color));
memset(degree,0,sizeof(degree));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
degree[x]++,degree[y]++;
mp[x].push_back(y);
mp[y].push_back(x);
}
rot=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
rot++;color[i]=1;root[i]=rot;
Dfs(i);
}
}
if(flag==0)
{
printf("0 1\n");
}
else
{
int maxn=0;
for(int i=1;i<=n;i++)
{
maxn=max(maxn,degree[i]);
}
if(maxn==0)
{
ll output=(ll)n*(ll)(n-1)*(ll)(n-2);
output/=3;
output/=2;
printf("3 %I64d\n",output);
}
if(maxn==1)
{
ll one=0;
ll two=0;
for(int i=1;i<=n;i++)
{
if(degree[i]==1)one++;
else two++;
}
ll output=m*(one-1+two-1);
printf("2 %I64d\n",output);
}
if(maxn>=2)
{
ll output=0;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
if(color[i]==1)sum[root[i]]++;
}
for(int i=1;i<=rot;i++)output+=(ll)sum[i]*(ll)(sum[i]-1)/2;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
if(color[i]!=1)sum[root[i]]++;
}
for(int i=1;i<=rot;i++)output+=(ll)sum[i]*(ll)(sum[i]-1)/2;
printf("1 %I64d\n",output);
}
}
}
}