首页 技术 正文
技术 2022年11月14日
0 收藏 686 点赞 4,050 浏览 1484 个字

题目链接:http://codeforces.com/problemset/problem/557/D

大意 给出一个未必联通的无向图(点数至少为3),问最少加多少边可以形成一个奇圈,以及这样做的方案有多少种。

首先如果是一张没有边的图,那么直接就是需要加三条边,有C(n,3)种方式。

接着,判断这张图每一个联通块是否是一个二分图,因为二分图是一定没有奇圈的。如果有联通块不是二分图,那么也就是意味着存在奇圈,这样的话需要加0条边,方式为1种。

接下去还需要分两种情况讨论

1.如果所有的联通块都只有1个或者2个点,则至少需要加2条边,方式为所有点数为2的联通块随便选择一个其他的点加2条边,故统计所有点数为2的联通块数量。

2.存在至少包含3个点的联通块,注意,此时已经排除了联通块不是二分图的情况,所以也即联通块一定是二分图。对于二分图,连接x部和y部的边是不会形成奇圈的,这种情况下只需要连接一条相同部之间的边即可。所以方案数为对于所有至少包含3个点的联通块,计算x部和y部点数,对答案累加上 C(cntx,2)+C(cnty,2)

 #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set> using namespace std; const int N=1e5+; vector<int> edge[N];
bool vis[N];
int col[N];
bool found=false;
int cnt[];
void dfs(int u,int c) {
vis[u]=true;
col[u]=c;
cnt[c]++;
for (int i=;i<edge[u].size();i++) {
int v=edge[u][i];
if (vis[v]&&col[v]==c) {
found=true;
}
if (vis[v]) continue;
dfs(v,c^);
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for (int i=;i<=m;i++) {
int u,v;
scanf("%d %d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
if (m==) {
long long ret=1LL*n*(n-)*(n-)/6LL;
cout<<<<" "<<ret<<endl;
}
else {
found=false;
bool three=false;
long long retThree=;
int cnt2=;
for (int i=;i<=n&&!found;i++) {
if (vis[i]) continue;
cnt[]=cnt[]=;
dfs(i,);
if (cnt[]+cnt[]>=){
three=true;
retThree+=1LL*cnt[]*(cnt[]-)/2LL+1LL*cnt[]*(cnt[]-)/2LL;
}
else if (cnt[]+cnt[]==)
cnt2++;
}
if (found) {
cout<<<<" "<<<<endl;
}
else if (three){
cout<<<<" "<<retThree<<endl;
}
else {
long long ret=1LL*cnt2*(n-);
cout<<<<" "<<ret<<endl;
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290