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的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...
随机推荐
- 手机中快速看图,浏览编辑DWG 梦想极光CAD
梦想极光CAD6.0(2016.3.1) 手机版最新更新 1.增加手机上,图纸浏览时预览功能 2.增加直接从手机,QQ接收目录下加载文件功能 3.手机交互界面优化 4.增加新建图纸功能 5.增加缓存功 ...
- jQuey中的return false作用是什么?
jQuey中的return false作用是什么?在众多的语句中都有return false的使用,当然对于熟悉它的开发者来说,当然是知根知底,知道此语句的作用,当然也就知道在什么时候使用此语句,不过 ...
- Java核心技术 卷一 复习笔记(乙
1.字符串从概念上讲,Java字符串就是Unicode字符序列.Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义类,叫String. 每个用双引号括起来的字符串都是 String ...
- 【tips】自动化测试工具 - selenium和phantomJS
### 目录清单 selenium和phantomjs概述 selenium常用API 案例操作:模拟登陆csdn 1. selenium和phantomJS是什么东西 selenium是一套web网 ...
- linux动态库加载路径修改
1.在 /etc/ld.so.conf 文件中添加搜索路径,重启或者 ldconfig 生效: 2.在 /etc/ld.so.conf.d 目录下添加 *.conf 文件,其中可以添加搜索路径,重启获 ...
- 51nod1128 正整数分组V2
[题解] 二分一个最大值,check一下分出来的组数是否小于等于k即可. #include<cstdio> #include<algorithm> #define LL lon ...
- c#读取.config文件内容
今天在做项目的时候,由于程序同时启动多种情况的数据,测试分为多个人,就需要把数据分离开来,于是用了一个临时的配置文件,让测试在配置文件修改相应数据从而让各个测试互相不影响! 步骤: 第一步:添加一个A ...
- BZOJ 2095 [POI2010]Bridges (最大流、欧拉回路)
洛谷上有这题,但是输出方案缺SPJ..(而且我也懒得输出方案了) 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2095 题解: 首先判 ...
- TensorFlow Ops
TensorFlow Ops 1. Fun with TensorBoard In TensorFlow, you collectively call constants, variables, op ...
- 魔法猪学院(codevs 1835)
题目描述 Description iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世 ...