题目链接

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. [转] C#.Net Socket网络通讯编程总结

    1.理解socket1).Socket接口是TCP/IP网络的应用程序接口(API).Socket接口定义了许多函数和例程,程序员可以用它们来开发TCP/IP网络应用程序.Socket可以看成是网络通 ...

  2. <转>LINQ To SQL 语法及实例大全

    一篇很全很强大的linq to sql 总结 来源:http://blog.csdn.net/pan_junbiao/article/details/7015633 目录(?)[-] LINQ to ...

  3. 菜鸟初试水--JQuery基础

    此文仅作入门级文章,望大神们高抬贵手! JQuery: 它是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作. JQuery的用途: ①访问和操作DOM元素: 使用j ...

  4. (转)Java通过axis调用WebService

    转自:http://blog.csdn.net/wanglha/article/details/49679825 转载地址:http://www.linuxidc.com/Linux/2015-06/ ...

  5. lab4 Cache Geometries 深入理解计算机系统——高速缓存

    这个实验主要是将高速缓存命中的一点东西,意在告诉我们平常多注意这方面的东西. 不懂java的,所以只管C的部分. You will do this several times, making smal ...

  6. c++ build options(important)

    The C runtime library  4 versions: Multi-threaded (/MT) Multi-threaded Debug (/MTd) Multi-threaded D ...

  7. C#控件TabControl隐藏page

    隐藏 这个需求其实就是TABCONTROL控件会有很多提前制作好的PAGE页面,每次软件启动不可能所有页面都显示出来,目前想了个比较简单的方法解决这个问题 首先定义一个List集合存储TABCONTR ...

  8. spring与hibernate整合配置基于Annotation注解方式管理实务

    1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 <!-- jdbc连接信息 --> <context:property-pla ...

  9. 类比的方法学习Performance_schema

    引用自:http://www.javacoder.cn/?p=332 MySQL在5.6版本中包含了一个强大的特性——performance-schema,合理的使用这个数据库中的表,能为我们解决一些 ...

  10. Linux 中执行命令

    第一步: 在txt文件(文件名为cmd_file)中写入代码:echo this is content! 第二步: 授权chmod 555 cmd_file 第三步: 运行 ./cmd_file -- ...