CodeForces - 557D Vitaly and Cycle(二分图)
Vitaly and Cycle
1 second
256 megabytes
standard input
standard output
After Vitaly was expelled from the university, he became interested in the graph theory.
Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.
Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w — the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.
Two ways to add edges to the graph are considered equal if they have the same sets of added edges.
Since Vitaly does not study at the university, he asked you to help him with this task.
The first line of the input contains two integers n and m ( — the number of vertices in the graph and the number of edges in the graph.
Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n) — the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.
It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.
Print in the first line of the output two space-separated integers t and w — the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.
4 4
1 2
1 3
4 2
4 3
1 2
3 3
1 2
2 3
3 1
0 1
3 0
3 1
The simple cycle is a cycle that doesn't contain any vertex twice.
题意:给你一个n个节点m条边的图 问你是不是存在一个奇数环(就是环中的节点个数为奇数个)
如果存在输出0 1
如果不存在 输出最少加多少条边使得存在一个奇数环 并输出他的方案数
当一个图是二分图的话 他是一定不存在奇数环的 反之 他就一定存在奇数环
0 1染色判断是不是二分图
如果是二分图的话 也许是多个联通块 所以我们只需要统计各个联通块中0 1中的个数 a[i] b[i] 答案就是各个联通块的 a(a-1)/2+b(b-1)/2的和
当然 有两种情况是要讨论的 m=0 不存在边 所以就是任意三个点可以组成一个奇数环 边就是加3条
还是一种已经所有联通块中节点数最多就只有两个 答案就是 (n-2)*m
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int N=+;
int head[N];
int tot;
struct node{
int to,next;
}edge[N<<];
int color[N];
int vis[N];
int a[N];
int b[N];
int num[N];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int DFS(int u,int t){
if(vis[u]==){
if(color[u]==)a[t]++;
if(color[u]==)b[t]++;
num[t]++;
}
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(color[v]==){
color[v]=color[u]^;
if(DFS(v,t)==)return ;
}
else if(color[u]==color[v]){
return ;
}
}
return ;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
if(m==){
cout<<<<" "<<(ll)n*(n-)*(n-)/<<endl;return ;
}
memset(color,,sizeof(color));
memset(vis,,sizeof(vis));
int flag=;
int t=;
for(int i=;i<=n;i++){
if(color[i]==&&vis[i]==){
if(DFS(i,++t)==){
color[i]=;
flag=;
break;
}
}
}/*
for(int i=1;i<=n;i++){
cout<<color[i]<<" "<<endl;
}
for(int i=1;i<=t;i++){
cout<<a[i]<<" "<<b[i]<<" "<<num[i]<<endl;
}*/
if(flag==){
cout<<<<" "<<<<endl;return ;
}
ll ans=;
flag=;
for(int i=;i<=t;i++){
if(num[i]<=){
flag++;continue;
}
ans=ans+(ll)a[i]*(a[i]-)/+(ll)b[i]*(b[i]-)/;
//cout<<ans<<endl;
}
if(flag!=t)cout<<<<" "<<ans<<endl;
else{
cout<<<<" "<<(ll)m*(n-)<<endl;
} }
CodeForces - 557D Vitaly and Cycle(二分图)的更多相关文章
- codeforces 557D. Vitaly and Cycle 二分图染色
题目链接 n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式. 具体看代码解释 #include<bits/stdc++.h> using names ...
- codeforces 557D Vitaly and Cycle
题意简述 给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数 (注意不考虑自环) ---------------------------------------------------- ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
- 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 ...
- 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 ...
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环
题目链接: 点这里 题目 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inpu ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces 173B Chamber of Secrets 二分图+最短路
题目链接: http://codeforces.com/problemset/problem/173/B 题意: 给你一个n*m的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...
随机推荐
- ThinkPHP---thinkphp文件加载
[一]文件加载在ThinkPHP里提供了三种方式 实际开发里,文件加载方式一般以第一种为主(通过函数库形式自动加载,此时我们仅仅需要定义文件和函数) (1)函数库形式加载 函数库分3种级别,系统函数库 ...
- 【Redis】二、Redis高级特性
(三) Redis高级特性 前面我们介绍了Redis的五种基本的数据类型,灵活运用这五种数据类型是使用Redis的基础,除此之外,Redis还有一些特性,掌握这些特性能对Redis有进一步的了解, ...
- JPA 与 JDBC 的区别和基本用法
JPA 概念 JPA(Java Persistence API)用于对象持久化的 API,是 Java EE 5.0 平台标准的 ORM 规范,使得应用程序以统一的方式访问持久层. 与 JDBC 的对 ...
- Python&机器学习总结(一)
① numpy中np.c_和np.r_ np.r_是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat(). np.c_是按行连接两个矩阵,就是把两矩阵左右相加, ...
- <MySQL>入门七 存储过程和函数
-- 存储过程和函数 /* 存储过程和函数:类似java中的方法 好处: 1.提高代码的重用性 2.简化操作 */ /* 存储过程 含义:一组预先编译好的SQL语句的集合.理解成批处理语句 1.提高代 ...
- Python数据库连接池DBUtils(基于pymysql模块连接数据库)
安装 pip3 install DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 此连接池有两种连接模式: # BDUtils数据库链接池: 模式一:基于threaing ...
- 【转】Flex 布局
网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中 ...
- npm 使用教程
链接----------------------------------npm官网npm淘宝镜像 安装包----------------------------------npm install -g ...
- Codeforces 934D/933B - A Determined Cleanup
传送门:http://codeforces.com/contest/934/problem/D 给定两个正整数p(p≥1).k(k>1).多项式f(x)的系数的取值集合为{0,1,2,...,k ...
- js eslint语法规范错误提示代码
最近在用eslint代码检测,因为之前不太注意代码规范,刚开始确实头疼,哈哈,不过用习惯了就会感觉还不错,其实也没有那样难调试 我看过之前有些人已经做过总结,自己记录下,方便自己以后查找 “Missi ...