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的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...
随机推荐
- ARX亮显问题
转载一段acedSSSetFirst的用法仅供参考:打个比方,我创建了一个命令,这个命令的功能是提示用户选择,然后只过滤文本对象作为选择集,随后在屏幕上使得这个选择集的所有成员都亮显,并且能够显示出各 ...
- Python 爬虫爬取今日头条街拍上的图片
# 今日头条--街拍 import requests from urllib.parse import urlencode import os from hashlib import md5 from ...
- linux tail-在屏幕上显示指定文件的末尾若干行
博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 tail命令用于输入文件中的尾部内容.tail命令默认在屏幕上显示指定文件的末尾10行.如果给定的文件不止一个,则在显示的每个 ...
- 腾讯云:搭建 Node.js 环境
搭建 Node.js 环境 安装 Node.js 环境 任务时间:5min ~ 10min Node.js 是运行在服务端的 JavaScript, 是基于 Chrome JavaScript V8 ...
- java nio--采用Selector实现Socket通信
server: /** * 选择器服务端 * Created by ascend on 2017/6/9 9:30. */ public class SelectorServer { // publi ...
- HDU 1274 递归拼接字符串
题目大意: 根据所给的数字,表示其相连的字符的输出个数,或是下一个括号中的所有字符的输出个数 每一个相互对应的 '(' 和 ')' 中的所有字母均作为一组数据处理 在每一次dfs过程中都处理好这样一个 ...
- noip模拟赛 天天寄快递
分析:并不是特别难的一道题,用到了贪心算法. 首先可以明确的一点是我们要尽量偷贡献最大的数据,要先满足每一个公司的贡献都大于等于K,以这个作为首要条件.那么我们可以先把每个快递公司的快递按照贡献从大到 ...
- (13)Corner Detection角点检测
import cv2 import numpy as np img=cv2.imread('opencv-corner-detection-sample.jpg') gray = cv2.cvtCol ...
- 深刻理解Python中的元类(metaclass)--代码实践
根据http://blog.jobbole.com/21351/所作的代码实践. 这篇讲得不错,但以我现在的水平,用到的机会是很少的啦... #coding=utf-8 class ObjectCre ...
- spring-boot-starter-actuator(健康监控)配置和使用
在生产环境中,需要实时或定期监控服务的可用性.Spring Boot的actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看.相关功能统计等. 集成: <depen ...