题目链接: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 ;
}

最新文章

  1. 配置SQL server远程连接(局域网)
  2. 图情期刊要求2015(A,B,C类)
  3. MySQL执行计划解读
  4. bzoj4429: [Nwerc2015] Elementary Math小学数学
  5. win7平台下React-Native Android:Unable to upload some APKs
  6. win7/ubuntu双系统下,如何恢复成win7引导及卸载ubuntu
  7. WPF 一个数据库连接测试的实现
  8. poj: 2255
  9. leetcode 98 Validate Binary Search Tree ----- java
  10. solr的collection,shard,replica,core概念
  11. Java:进制转换
  12. 《自动共享LDAP用户并且访问其家目录》RHEL6
  13. Page 的生命周期学习小结(翻译兼笔记)
  14. Histogram Equalization
  15. IOS GCD图片数据异步下载,下载完成后合成显示
  16. MySQL &#183; 引擎特性 &#183; InnoDB 同步机制
  17. mybatis配置文件resultMap标签的使用
  18. spring之Environment
  19. ASP.NET MVC 模型绑定
  20. C++中的友元函数的总结

热门文章

  1. ASP渲染下拉框使时间依次减少
  2. ThinkPhp框架:有条件的数据库查询、tp框架的其他知识
  3. PowerDesigner建模应用(二)逆向工程,导出PDM文件前过滤元数据(表、视图、存储过程等)
  4. Repaints and Reflows 重绘和重排版
  5. TIME_WAIT 另一种解决方式 SO_LINGER
  6. 5种方法推导Normal Equation
  7. asp.net core mvc剖析:动作执行
  8. NIO(四、Selector)
  9. Java排序算法之直接选择排序
  10. aps.net验证控件的异常处理