CF 557D(Vitaliy 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.

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.


很容易发现答案边数t不可能超过3条(3条必能有1奇圈),分类讨论

3条的情况:

原图所有连通支必为2分图(否则有奇圈)

故在一边连一条边必有以奇圈(连通则原来有偶路,连一边有奇圈)


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (200000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int edge[MAXM]={0},pre[MAXN]={0},next[MAXM]={0},Size=1;
void addedge(int u,int v)
{
	edge[++Size]=v;
	next[Size]=pre[u];
	pre[u]=Size;
} 
void addedge2(int u,int v){addedge(u,v),addedge(v,u);}
int n,m;

int cnt[2+10]={0},b[MAXN]={0};
bool is_odd=0;
void dfs(int x,bool c)
{
	cnt[c]++;b[x]=1+c;
	Forp(x)
	{
		int v=edge[p];
		if (!b[v]) dfs(v,!c);
		else if (b[v]==b[x]) is_odd=1; 
	}
}
int main()
{
//	freopen("D.in","r",stdin);
	
	cin>>n>>m;
	For(i,m)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		addedge2(u,v);
	}
	
	if (m==0) {
		cout<<"3 "<<1LL*n*(n-1)*(n-2)/6<<endl;
		return 0;
	}
	
	ll ans=0;
	bool flag=0;
	
	For(i,n)
		if (!b[i]) {
			MEM(cnt)
			dfs(i,0);
			if (cnt[0]+cnt[1]>2) {
				flag=1;
				Rep(k,2) ans+=1LL*cnt[k]*(cnt[k]-1)/2;
			} 
		} 
	
	if (is_odd) {
		cout<<"0 1\n";
		return 0;
	}
	
	if (flag) cout<<"1 "<<ans<<endl;
	else cout<<"2 "<<1LL*m*(n-2)<<endl;
	
	
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值