题目链接

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. 《4》CentOS7.0+OpenStack+kvm云平台部署—配置Nova

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  2. 从Excel转Access的一个方法说开去(DataRow的state状态)

    因为客户对access不太熟悉,更喜欢玩EXCEL.但是系统要求导入ACCESS.所以我们得做个把EXCEL转换成Access的小工具.(别问我为啥不让系统直接导入excel....我不知道!),然后 ...

  3. spm3 基本

    spm3 命令 spm init //初始化一个spm模块,会生成基本配置以及测试文件等(下图). //注 初始化以后一般需要 鲜执行一下 spm install 安装默认依赖模块 index.js就 ...

  4. IIS发布问题-用户 'IIS APPPOOL\DefaultAppPool' 登录失败

    今天新建了一个ASP.NET(Language=C#)网站,配置好数据库后编写了几行代码测试数据库的是否能正常使用. 当运行程序时,第一个页面都没有打开就出现了错误(因为我首页就访问数据库,填充一些D ...

  5. SGU 187.Twist and whirl - want to cheat( splay )

    维护一个支持翻转次数M的长度N的序列..最后输出序列.1<=N<=130000, 1<=M<=2000 splay裸题... ------------------------- ...

  6. [C#参考]事件机制

    还是那个项目,为了降低程序的耦合性,我决定小小的重构一下自己原来的代码,把Socket通信和帧的分析这两部分分别封装成一个类,当然线程没有变,只是封装了一下,为的就是模块测试完容易拼接.这也是我打算降 ...

  7. Java基础之"=="和 和 equals 方法的区别

    一."=="操作符 ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同,要比较两个基本类型的数据或两个引用变量是否相等,只能用==操作 ...

  8. R包——ggplot2(一)

    关于ggplot2包(一) 关于ggplot2包(一) ggplot2基本要素 数据(Data)和映射(Mapping) 几何对象(Geometric) 标尺(Scale) 统计变换(Statisti ...

  9. js中&& 和 ||

    原文链接:http://wenrunchang123.iteye.com/blog/1749802 a() && b()     1). 如果执行a() 返回true:那么执行b()并 ...

  10. Use API to retrieve data from internet

    Reference: Working with APIs Many big companies and organizations provide API for us to retrieve dat ...