codeforces 557 D. 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.
题意:
给出一个图,无重边和自环
设t为要加的最小的边数,使得图有奇数个节点的环,环中的每一个节点只经过一次
设w为加满足条件的t条边的方案数
输出t w
思路:
考虑这个图的每一个节点的度deg
1.最大的度 = 0,说明图没有边,图的最长路为0,那么
t = 3,w = C(n,3)
2.最大的度 = 1,说明图的最长路为1,那么
t = 2,w = m * (n - 2)
3.最大的度 > 1,说明图的最长路>1,那么
(1).如果原图有奇数个节点的环,那么
t = 0,w = 1
(2).如果原图没有奇数个节点的环,那么
t = 1,w = ?
如何判断原图有没有奇数个节点的环呢?
我们发现,
如果原图没有奇数个节点的环,那么原图就是一个2分图
如果原图有奇数个节点的环,那么原图就不是一个2分图
所以用染色法就可以知道原图是不是一个2分图啦
那上面讨论中的?的值是多少呢?
当原图是一个2分图的时候,
对于图的每一个联通分量,我们把这个联通分量的节点分成了2部分,
分别有x,y个节点,那么ans += C(x,2) + C(y,2)
注意,
是对每一个联通分量,分别得到x,y,然后更新ans
而不是对整个图求x,y
因为对于我们选择加入的这一条边的2个端点,一定是要在同一个联通分量中的
代码:
//File Name: cf557D.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年07月08日 星期五 20时38分29秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue> #define LL long long using namespace std; const int MAXN = + ; int deg[MAXN];
int is[MAXN];
bool flag;
queue<int> que; struct Edge{
int to,next;
}edge[MAXN << ];
int head[MAXN],tot; void init(){
memset(head,-,sizeof head);
tot = ;
memset(deg,,sizeof deg);
} void addedge(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} void dfs(int p,int u,int x){
is[u] = x;
que.push(u);
for(int i=head[u];~i;i=edge[i].next){
int v = edge[i].to;
if(v == p)
continue;
if(is[v] == x){
flag = false;
return ;
}
else if(is[v] == -)
dfs(u,v,x ^ );
}
} void solve(int n,int m){
LL ans;
int ma = -;
for(int i=;i<=n;i++)
ma = max(deg[i],ma);
if(ma == ){
ans = (LL)n * (n - ) * (n - ) / ;
cout << "3 " << ans << endl;
}
else if(ma == ){
ans = (LL)m * (n - );
cout << "2 " << ans << endl;
}
else{
ans = ;
memset(is,-,sizeof is);
flag = true;
while(!que.empty())
que.pop();
for(int i=;i<=n;i++){
if(is[i] == -){
dfs(-,i,);
int x = ,y = , u;
while(!que.empty()){
u = que.front();
que.pop();
if(is[u] == ) x++;
else y++;
}
ans += (LL)x * (x - ) / + (LL)y * (y - ) / ;
}
}
if(!flag)
cout << "0 1" << endl;
else
cout << "1 " << ans << endl;
}
return ;
} int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
init();
for(int i=,u,v;i<m;i++){
scanf("%d %d",&u,&v);
addedge(u,v);
deg[u]++;
deg[v]++;
}
solve(n,m);
}
return ;
}
codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图的更多相关文章
- 【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 图论
D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- CodeForces - 557D Vitaly and Cycle(二分图)
Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 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 ...
- HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Java实现 LeetCode 785 判断二分图(分析题)
785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...
- codeforces 557D Vitaly and Cycle
题意简述 给定一个图 求至少添加多少条边使得它存在奇环 并求出添加的方案数 (注意不考虑自环) ---------------------------------------------------- ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
随机推荐
- shell脚本处理大数据系列之(一)方法小结
转自:http://longriver.me/?p=57 方法1: 单进程处理大规模的文件速度如(上million量级)比较慢,可以采用awk取模的方法,将文件分而治之,这样可以利用充分的利用多核CP ...
- Integer相加产生的类型转换问题
做项目时犯二没有搞清楚优先级的问题从而暴露出一个Integer相加而产生的类型转换的问题 Integer a; Integer b; Integer c; c= a+b==null?a:b; jav ...
- poj3660 最短路/拓扑序
题意:有n头牛,为了给牛排顺序,给出了牛之间的胜负关系,具有传递性,问给出的胜负关系是否可以给这些牛排出唯一的顺序. 其实是个拓扑排序问题,牛的胜负关系就是有向边,然后判断是否有唯一的拓扑序就行.当然 ...
- 什么是域名?什么网站名?什么是URL?
域名,相信大家都不默认,也使用过无数次!比如: google.com.baidu.com.163.com等. 这时候,你可能要奇怪了,为什么小编没有在前面加上www? 因为正常情况下,不应该是www. ...
- 【BZOJ1004】【HNOI20008】cards
看黄学长的代码才写出来的,sro_hzwer_orz 原题: 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给 ...
- asm单机dg dbca报错ORA-01031 CRS-2676,rman restore主库控制文件报错ORA-15081
dg-> ll $ORACLE_HOME/bin/oracle -r-xr-s--x 1 oracle asmadmin 210824714 Nov 20 16:41 /u01/app/orac ...
- distinct和group by
distinct:同时查询两个字段,另外一个字段(不是distinct的那个字段,下例是tagid)需要多个值,如下 group by:同时查询两个字段,另外一个字段( ...
- WordPress中"无法将上传的文件移动至"错误的解决方法
#chown -R www:www /wwwroot 今天在网页上传图片到博客,结果提示:“无法将上传的文件移动至 /home/wwwroot/wp-content/uploads/2013/”,郁闷 ...
- deb、rpm、tar.gz三种Linux软件包的区别
初接解LINUX的,同样都是for linux,但rpm.tar.gz.deb包还是有很大区别的, 这种区别可使安装过程进行不下去.那我们应该下载什么格式的包呢? rpm包-在红帽LINUX.SUSE ...
- 实现手电筒Flash Light 关键代码
实现手电筒Flash Light 关键代码 实现Flash的逻辑 view.setOnClickListener(new OnClickListener() { @Override public vo ...