题目链接

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. codefirst初尝试

    Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Firs ...

  2. css重置

    清除标签的默认样式 body{margin:0;}ul,ol{margin:0 auto;padding:0;}li{ list-style:none;}dl{margin:0;}dd{margin: ...

  3. 日期选择器——java

    转载:http://zgdeng.iteye.com/blog/1405650 代码如下: import java.awt.BasicStroke; import java.awt.BorderLay ...

  4. List用法

    定义一个类: using System;using System.Collections.Generic;using System.Linq;using System.Web; namespace W ...

  5. [转]PB 基本语句 循环语句

    PB 基本语句一.赋值语句赋值语句用于给变量.对象属性赋值,这是应用程序中使用最频繁的语句,其语法格式为:variablename = expression_r其中:⑴variablename是变量名 ...

  6. ios不同版本下的UILabel自动换行问题

    @interface ThreadPreviewCell : UITableViewCell @property (nonatomic, strong) UILabel *titleLabel; @e ...

  7. Hadoop插件安装

    1.首先下载Hadoop对应版本的插件,以Hadoop 1.0版本对应的插件Hadoop-eclipse-plugin1.0.3.jar为例 2.将下载的插件放置到Ecplise安装目录的plugin ...

  8. WPF中常用控件的属性

    Source = new BitmapImage( new Uri( WangCaiConfig.GetCurrentDirectory() + imgStr, UriKind.RelativeOrA ...

  9. 拔一拔 ExtJS 3.4 里你遇到的没遇到的 BUG(1)

    本文从今天开始,我要做的就是不断的更新,不断的披露ExtJS 3.4的BUG并修复它.需要注意的是版本为3.4而不是4.0,因为4.0改动和变化比较大,所以不要对号入座. 嘿嘿,本人不怎么写东西,不过 ...

  10. Android Log工具类

    import java.text.SimpleDateFormat; import java.util.Date; import android.util.Log; public class LogU ...