CodeForces 557 D.Vitaly and Cycle(组合数学)

Description

给出一个 n 个点m条边的无向图,现在要求加最少的边使得该图有一个长度超过 1 的奇环,问最少边数及有多少种方案加最少条边形成奇环

Input

第一行两个整数n,m表示点数和边数,之后 m 行每行两个整数u,v表示一条无向边 (3n105,0mmin(n(n1)2,105))

Output

输出所加最少边数 t 以及加t条边有多少种方案可以形成奇环

Sample Input

4 4
1 2
1 3
4 2
4 3

Sample Output

1 2

Solution

若没边则至少需要加三条边才能形成三元环,方案数 C3n

若已经存在奇环,则不需要加边,方案数为 1

若不存在奇环,且每个连通块点数不超过2,那么对于每一条边,只有从其他 n2 个点选一个点与这条边的两个端点连边可形才成三元环,需要加两条边,方案数 (n2)m

若不存在奇环,且存在点数超过 2 的连通块,对每个连通块黑白染色后,假设白点x个,黑点 y 个,由于x+y>2,那么必然有 x>1 y>1 ,故对于每个点数超过 2 的连通块,可以从一个颜色的点中选出两个连一条边即可形成奇环,需要加一条边,方案数C2x+C2y

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
int n,m,c[maxn],num[3],res,flag;
vector<int>g[maxn];
void dfs(int u,int col)
{
    if(flag)return ;
    c[u]=col;
    num[col]++;
    res++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(c[v]==-1)dfs(v,col^1);
        else if(c[v]==c[u])
        {
            flag=1;
            return ;
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(m==0)
        {
            printf("3 %I64d\n",(ll)n*(n-1)*(n-2)/6);
            continue;
        }
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v),g[v].push_back(u);
        }
        memset(c,-1,sizeof(c));
        ll ans=0;
        int mx=0;
        flag=0;
        for(int i=1;i<=n;i++)
            if(c[i]==-1)
            {
                res=num[0]=num[1]=0;
                dfs(i,0);
                if(flag)break;
                mx=max(mx,res);
                ans+=(ll)num[0]*(num[0]-1)/2+(ll)num[1]*(num[1]-1)/2;
            }
        if(flag)printf("0 1\n");
        else if(mx<=2)printf("2 %I64d\n",(ll)(n-2)*m);
        else printf("1 %I64d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值