codeforces 557D Vitaly and Cycle
题意简述
给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数
(注意不考虑自环)
-----------------------------------------------------------------------------
一道二分图染色的讨论题
比赛时只会用二分图染色判断树以及偶环 忘记用这个来判奇环。。。
二分图染色这种联赛知识点的题目现在也不会写了。。。
------------------------------------------------------------------------------
我们可以按需要添加边的条数来讨论这题
首先讨论添加边条数为3——即原原图边数m为0时
$ans=n*(n-1)*(n-2)/6$
再讨论添加边条数为2——即原图中所有边都没有公共端点/所有点度数<=1 时
$ans=m*(n-2)$
再讨论添加边数为0——即原图中存在奇环时
$ans=1$
最后讨论添加边数为1——即原图中只有树以及偶环
$ans=\sum(white[i]-1)*white[i]/2+(black[i]-1)*black[i]/2$
其实思路清晰后实现起来就很容易了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define rep(i,n) for(int i=1;i<=n;++i)
#define imax(x,y) (x>y?x:y)
#define imin(x,y) (x<y?x:y)
using namespace std;
const int N=;
int firste[N],nexte[N<<],v[N<<];
int color[N],fa[N],bl[N],wh[N],degree[N];
int n,m,e=,flag=;
long long ans=;
void build_edge(int x,int y)
{
++e;
nexte[e]=firste[x];
firste[x]=e;
v[e]=y;
}
void dfs(int u,int c,int f)
{
color[u]=c;
fa[u]=f;
if(c&)++bl[f];
else ++wh[f];
for(int p=firste[u];p;p=nexte[p])
if(!color[v[p]])dfs(v[p],-c,f);
else if(color[v[p]]==color[u])
{
flag=;
return;
}
}
int main()
{
int x,y;
scanf("%d%d",&n,&m);
if(!m)
{
ans=(long long)n*(n-)*(n-)/;
printf("3 %I64d",ans);
return ;
}
rep(i,m)
{
scanf("%d%d",&x,&y);
build_edge(x,y);
build_edge(y,x);
++degree[x];
++degree[y];
if(degree[x]>||degree[y]>)flag=;
}
if(flag)
{
ans=(long long)m*(n-);
printf("2 %I64d",ans);
return ;
}
int cnt=;
rep(i,n)
if(!color[i])
{
dfs(i,,++cnt);
if(flag)
{
printf("0 1");
return ;
}
}
rep(i,cnt)
ans+=(long long)(wh[i]-)*wh[i]/+(long long)(bl[i]-)*bl[i]/;
printf("1 %I64d",ans);
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 557D. Vitaly and Cycle 二分图染色
题目链接 n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式. 具体看代码解释 #include<bits/stdc++.h> using names ...
- 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 ...
- 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 per test1 second memory limit per test256 megabytes inpu ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
- codeforces 518A. Vitaly and Strings
A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- CSS中设置字体样式
<style type="text/css"> body{ font-family: SimHei,"微软雅黑",sans-serif; } < ...
- 20190814 On Java8 第四章 运算符
第四章 运算符 优先级 程序员经常都会忘记其他优先级规则,所以应该用括号明确规定运算顺序. 赋值 基本类型的赋值都是直接的,而不像对象,赋予的只是其内存的引用.举个例子,a = b ,如果 b 是基本 ...
- 爬虫(十一)—— XPath总结
目录 XPath总结 一.何为XPath 二.XPath语法 1.语法 2.实例 三.XPath轴 1.XPath轴语法 2.XPath轴实例 四.XPath运算符 XPath总结 一.何为XPath ...
- Apache Commons 工具类介绍及简单使用(转载)
原文链接 http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动 ...
- Mysql基础篇(笔记)
mysql数据库是由DB跟DBMS跟sql组成 DB 数据库仓库 DBMS 数据库管理系统 SQL 一门通用的数据库语言 数据库启动命令 : 关闭->net stop MySQL || 开启 ...
- python学习第六天运算符总结大全
python学习第六天运算符总结大全,玖乐网络(www.96net.com.cn)列出下面详细 1,算术运算符 + 加 - 减 *乘 / 除 % 求模 ** 乘方 // 取整除 - 返回商的整数部分 ...
- F Find the AFei Numbers
链接:https://ac.nowcoder.com/acm/contest/338/F来源:牛客网 题目描述 AFei loves numbers. He defines the natural n ...
- tornado后台小框架
import tornado.ioloop import tornado.web """使用get方法提交过来数据就是用get方法,使用post执行post方法这个框架的 ...
- uoj396 [NOI2018]屠龙勇士
[NOI2018]屠龙勇士 描述 小 D 最近在网上发现了一款小游戏.游戏的规则如下: 游戏的目标是按照编号 1∼n 顺序杀掉 n 条巨龙,每条巨龙拥有一个初始的生命值 ai .同时每条巨龙拥有恢复能 ...
- JS中的匿名函数、回调函数、匿名回调函数
工欲善其事必先利其器 在学习JavaScript设计模式一书时,遇到了“匿名回调函数”这个概念,有点疑惑,查找了些资料重新看了下函数的相关知识点之后,对这个概念有了认识.九层之台,起于垒土.在熟悉这一 ...