题目链接

n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式。

具体看代码解释

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int de[maxn], white[maxn], black[maxn], color[maxn], head[maxn*], num, flag;
struct node
{
int to, nextt;
}e[maxn*];
void add(int u, int v) {
e[num].to = v;
e[num].nextt = head[u];
head[u] = num++;
}
void dfs(int u, int c, int s) { //每一个cnt代表一个联通块, c是颜色
color[u] = c;
if(c&)
black[s]++;
else
white[s]++;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!color[v]) {
dfs(v, -c, s);
} else if(color[v]==color[u]) { //这是本身就有奇环
flag = ;
}
}
}
int main()
{
int n, m;
cin>>n>>m;
if(!m) {
cout<<<<" "<<1ll*n*(n-)*(n-)/; //一条边也没有
return ;
}
int x, y;
mem1(head);
for(int i = ; i<=m; i++) {
scanf("%d%d", &x, &y);
de[x]++, de[y]++;
add(x, y);
add(y, x);
if(de[x]>||de[y]>) {
flag = ;
}
}
if(!flag) {
cout<<<<" "<<1ll*m*(n-)<<endl; //这种情况是有边, 但是所有的边都不相连
return ;
}
flag = ;
int cnt = ;
for(int i = ; i<=n; i++) {
if(!color[i]) {
dfs(i, , ++cnt);
}
if(flag) {
puts("0 1");
return ; //本身就有奇环
}
}
ll ans = ;
for(int i = ; i<=cnt; i++) {
ans += 1ll*(white[i]-)*white[i]+1ll*black[i]*(black[i]-); //每一个联通块里面的种数累加。
}
cout<<<<" "<<ans/<<endl;
return ;
}

codeforces 557D. Vitaly and Cycle 二分图染色的更多相关文章

  1. CodeForces - 557D Vitaly and Cycle(二分图)

    Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)

    http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...

  3. codeforces 557D Vitaly and Cycle

    题意简述 给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数 (注意不考虑自环) ---------------------------------------------------- ...

  4. Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】

    <题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...

  5. Codeforces 1093D Beautiful Graph(二分图染色+计数)

    题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...

  6. [cf557d]Vitaly and Cycle(黑白染色求奇环)

    题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立 ...

  7. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图

    D. Vitaly and Cycle       time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论

    D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

随机推荐

  1. Ubuntu小私房(3)--Uubutnu启动美化大变身

    Grub是什么? GNU GRUB 和GRUB是GRand Unified Bootloader的缩写,它是一个多重操作系统启动管理器.用来引导不同系统,如windows,linux.GRUB是多启动 ...

  2. at org.apache.jsp.index_jsp._jspInit(index_jsp.java:23)异常解决

    部署项目,启动tomcat一切正常.输入项目地址后 tomcat报例如以下错误: java.lang.NullPointerException at org.apache.jsp.index_jsp. ...

  3. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

  4. 网页平面设计 CSS

    1.在html中引入css的方法 1.行内式 行内式即在标记的style属性中设定CSS样式,这种方式本质上没有体现出CSS的优势,因此不推荐使用. 例如:<h1 style="属性名 ...

  5. 如何给Ubuntu 安装Vmware Tools

    http://jingyan.baidu.com/article/3065b3b6e8dedabecff8a435.html

  6. C++/C#结构体转化-传string给C++

    此例是把C#结构传给C++ C++: typedef struct VidyoClientInEventGroupChat_ { /*! Message (contents) to be sent t ...

  7. C# 微信公众平台开发(1)-- 服务器配置

    题记:最近公司需要开发微信服务号,由本人负责,以前虽然听过微信开发,但并没有认真的去了解,项目开发中,也边看文档边开发,记录自己的项目开发经验: 1.注册帐号--填写服务器配置 在https://mp ...

  8. CKEditor + CKFinder 实现编辑上传图片配置

    下载最新版 ckfinder 本人下载的php版本 https://cksource.com/ckfinder/download 下载最新版ckeditor http://ckeditor.com/ ...

  9. calltree看代码调用图

    calltree是在linux下面看c代码(尤其是复杂的内核代码)的神器. 推荐  calltree+vim + ctags + cscope + taglist [ vim: 搭建vim看代码的环境 ...

  10. ubuntu, Debian, CentOS

    ubuntu源自debian,内核很多文档都还是debian的字样,稳定性逐渐增强,基本满足日常开发. debian的核心稳定,性能强劲. centos的内核版本低,安全性高. 选择Debian是因为 ...