[cf557d]Vitaly and Cycle(黑白染色求奇环)
题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种。
解题关键:黑白染色求奇环,利用数量分析求解。
奇环:含有奇数个点的环。
二分图不存在奇环。反之亦成立。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int maxm=1e5+; int c[maxn]; //color,每个点的黑白属性,-1表示还没有标记,0/1表示黑白,比vis数组多一个作用
ll num[]; //在一次DFS中的黑白点个数
bool f=; //判断是否出现奇环 int head[maxn],tot,n,m,a,b;
struct edge{
int to;
int nxt;
}e[*maxm];
void add_edge(int u,int v){
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
void init(){
memset(head,-,sizeof head);
tot=;
memset(c,-,sizeof c);
} void dfs(int u,int x){
if(f)return;
c[u]=x;
num[x]++;
for(int i=head[u];~i;i=e[i].nxt){
int j=e[i].to;
if(c[j]==-) dfs(j,!x);
else if(c[j]==x){//存在奇环
f=;
return;
}
}
}
int main(){
init();
cin>>n>>m;
for(int i=;i<=m;i++){
cin>>a>>b;
add_edge(a,b);
add_edge(b,a);
}
ll ans=,ans2=;
if(m==){
ans=(ll)n*(n-)*(n-)/;
printf("3 %lld\n",ans);
return ;
}
for(int i=;i<=n&&(!f);i++){
if(c[i]==-){
num[]=num[]=;
dfs(i,);
ans+=(num[]*(num[]-)+num[]*(num[]-))/;
if(num[]==&&num[]==){
ans2+=n-;
}
}
}
if(f) printf("0 1\n");
else if(ans) printf("1 %lld\n",ans);
else printf("2 %lld\n",ans2);
return ;
}
[cf557d]Vitaly and Cycle(黑白染色求奇环)的更多相关文章
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- codeforces 557D. Vitaly and Cycle 二分图染色
题目链接 n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式. 具体看代码解释 #include<bits/stdc++.h> using names ...
- fzu2181(点的双连通分量+求奇环)
求出每个点双连通分量,如果在一个点双连通分量中有奇环,则这个分量每个点都在一个奇环中. 关键是要知道怎么求点双连通分量以及点双连通的性质. fzu2181 http://acm.fzu.edu.cn ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- codeforce D. Shortest Cycle(floyd求最短环)
题目链接:http://codeforces.com/contest/1206/problem/D 给n个点,如果点a[ i ] &a[ j ] 不为0,则点a[ i ] 和 a[ j ] 直 ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
- lightoj 1300 边双联通分量+交叉染色求奇圈
题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...
- hdu 1689 求奇环bfs关键是层次图
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> usin ...
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环
题目链接: 点这里 题目 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inpu ...
随机推荐
- T-SQL_面试题
创建表插入数据 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname ...
- spring mvc中,如何在 Java 代码里,获取 国际化 内容
首先,在Spring的application.xml中定义 <bean id="messageSource" class="org.springframework. ...
- ecmall中的分页问题
<ecmall>Ecmall系统自带的分页功能 在Ecmall的二次开发中,分页是必不可少的.这个系统已经自带了分页功能,下面来看看如何使用这个分页. 下面是一个自定义的类,用于查看订单的 ...
- HDU3518Boring counting(后缀自动机)
Problem Description 035 now faced a tough problem,his english teacher gives him a string,which consi ...
- oracle如何insert into 多个values
稍微熟悉Oracle的都知道,如果我们想一条SQL语句向表中插入多个值的话,如果如下语句 INSERT INTO 某表 VALUES(各个值),VALUES(各个值),.....; 这样会报错的,因为 ...
- 伪元素after,before,css/js控制样式
CSS<style> body { font: 200%/1.45 charter; } ref::before { content: '\00A7'; letter-spacing: . ...
- jenkins 参数化构建,获取git分支
def heads= ("git ls-remote -h git@gitlab.com:*.git").execute()def headlist=heads.text.read ...
- The Salt Master has rejected this minion's public key!
salt查看日志: salt --log-level=all "10.199.165.244" state.highstate 进入调试模式: salt-minion -l deb ...
- hihoCoder#1050(树中最长路)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一 ...
- QString乱谈(2)
长期以来,很多人都清楚,一旦C++源码中直接使用了中文,这样的源码想要跨平台(I18N)会非常困难. 随着: Windows下:MSVC2010成为主流 Linux下:GCC升级到4.6 C++中的中 ...