codeforces 557D. Vitaly and Cycle 二分图染色
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 二分图染色的更多相关文章
- CodeForces - 557D Vitaly and Cycle(二分图)
Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
- codeforces 557D Vitaly and Cycle
题意简述 给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数 (注意不考虑自环) ---------------------------------------------------- ...
- Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】
<题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...
- Codeforces 1093D Beautiful Graph(二分图染色+计数)
题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...
- [cf557d]Vitaly and Cycle(黑白染色求奇环)
题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立 ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 ...
随机推荐
- oracle表设置主键自增长
create or replace table TBL_SYS_USER ( user_id NUMBER(19) not null, user_name ...
- 基于Jquery的Ajax分页,只有上一页和下一页
最近项目中用到ajax分页 在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接 http://www.zhangxinxu.com/wordpress/2010/ ...
- SSLSocket实现服务端和客户端双向认证的例子
首先创建服务器端私有密钥和公共密钥1, keytool -genkey -alias serverkey -keystore kserver.ks 密码: serverpass2, keytoo ...
- BZOJ 2302: [HAOI2011]Problem c( dp )
dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...
- Linux学习之Makefile文件的编写
转自:http://goodcandle.cnblogs.com/archive/2006/03/30/278702.html 目的: 基本掌握了 make 的用法,能在Linux系统上编 ...
- Do not go gentle into that good night
Do not go gentle into that good night By:Dylan Thomas Do not go gentle into that good night,Old ag ...
- Mysql 如何做双机热备和负载均衡 (方法二)
先简要介绍一下mysql双向热备:mysql从3.23.15版本以后提供数据库复制功能.利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(A<=>B)的功能. m ...
- IISExpress实现外部访问
首先修改IISExpress配置文件 \IISExpress\config\applicationhost.config 在website中添加一个binding <binding protoc ...
- python request模块学习
安装: pip install requests 使用: import requests HTTP请求:GET.POST.PUT.DELETE.HEAD.OPTIONS 1) get res = re ...
- eclipse 搭建Robotium环境--apk 环境搭建
1.配置好android sdk ,java环境 2.重新签名apk文件 在用户目录下,会有一个.android的目录,把re-sign.jar放在该目录下.执行命令 java -jar re-sig ...