CF #311 D. Vitaly and Cycle 加最少边形成奇圈
题目链接:http://codeforces.com/problemset/problem/557/D
大意 给出一个未必联通的无向图(点数至少为3),问最少加多少边可以形成一个奇圈,以及这样做的方案有多少种。
首先如果是一张没有边的图,那么直接就是需要加三条边,有C(n,3)种方式。
接着,判断这张图每一个联通块是否是一个二分图,因为二分图是一定没有奇圈的。如果有联通块不是二分图,那么也就是意味着存在奇圈,这样的话需要加0条边,方式为1种。
接下去还需要分两种情况讨论
1.如果所有的联通块都只有1个或者2个点,则至少需要加2条边,方式为所有点数为2的联通块随便选择一个其他的点加2条边,故统计所有点数为2的联通块数量。
2.存在至少包含3个点的联通块,注意,此时已经排除了联通块不是二分图的情况,所以也即联通块一定是二分图。对于二分图,连接x部和y部的边是不会形成奇圈的,这种情况下只需要连接一条相同部之间的边即可。所以方案数为对于所有至少包含3个点的联通块,计算x部和y部点数,对答案累加上 C(cntx,2)+C(cnty,2)
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set> using namespace std; const int N=1e5+; vector<int> edge[N];
bool vis[N];
int col[N];
bool found=false;
int cnt[];
void dfs(int u,int c) {
vis[u]=true;
col[u]=c;
cnt[c]++;
for (int i=;i<edge[u].size();i++) {
int v=edge[u][i];
if (vis[v]&&col[v]==c) {
found=true;
}
if (vis[v]) continue;
dfs(v,c^);
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for (int i=;i<=m;i++) {
int u,v;
scanf("%d %d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
if (m==) {
long long ret=1LL*n*(n-)*(n-)/6LL;
cout<<<<" "<<ret<<endl;
}
else {
found=false;
bool three=false;
long long retThree=;
int cnt2=;
for (int i=;i<=n&&!found;i++) {
if (vis[i]) continue;
cnt[]=cnt[]=;
dfs(i,);
if (cnt[]+cnt[]>=){
three=true;
retThree+=1LL*cnt[]*(cnt[]-)/2LL+1LL*cnt[]*(cnt[]-)/2LL;
}
else if (cnt[]+cnt[]==)
cnt2++;
}
if (found) {
cout<<<<" "<<<<endl;
}
else if (three){
cout<<<<" "<<retThree<<endl;
}
else {
long long ret=1LL*cnt2*(n-);
cout<<<<" "<<ret<<endl;
}
}
return ;
}
CF #311 D. Vitaly and Cycle 加最少边形成奇圈的更多相关文章
- 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 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 ...
- CodeForces - 557D Vitaly and Cycle(二分图)
Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input sta ...
- [cf557d]Vitaly and Cycle(黑白染色求奇环)
题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立 ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
- codeforces 557D. Vitaly and Cycle 二分图染色
题目链接 n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式. 具体看代码解释 #include<bits/stdc++.h> using names ...
- 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767
poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图 任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...
随机推荐
- iframe----摘抄出处未知
1.frame不能脱离frameSet单独使用,iframe可以: 2.frame不能放在body中:如下可以正常显示: <!--<body>--> <frameset ...
- SpringBoot之旅 -- SpringBoot 项目健康检查与监控
前言 You build it,You run it, 当我们编写的项目上线后,为了能第一时间知晓该项目是否出现问题,常常对项目进行健康检查及一些指标进行监控. Spring Boot-Actuato ...
- Java Web(十四) 编写MyBookStore项目的总结
这几天一直没有发博文,原因是在写一个书城的小项目,作为web学习的最后沉淀,接下来就要到框架的学习了. --WH 一.项目介绍 从网上找的一个培训机构的小项目,名称叫做 书城购物网站 吧,其中就是分前 ...
- ACM 整数划分(四)
整数划分(四) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...
- wemall doraemon中Android app商城系统工具集合类,包含各种程序中用到的静态方法
wemall doraemon中Android app商城系统工具集合类,包含各种程序中用到的静态方法,可用于其他商城或者系统的编程参考 package cn.zzu.edu.wemall.utils ...
- 基于JAVA语言的selenium测试基础总结
目录一.基本语句1.循环控制(break,continue)3.字符的替换(replace,repalceFirst,replaceAll,regex)4.字符串的连接("+",a ...
- HBase架构
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6573817.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...
- 卸载php+apache+mysql
一.卸载删除 mysql 1 sudo apt-get autoremove --purge mysql-server-5.0 2 sudo apt-get remove mysql-server ...
- Static语句块和初始语句块的使用---2015年10月恒生电子笔试试卷
package lhm.test; /** * @author lenovo * */public class Person { private int id; private static int ...
- Linux块设备驱动(二) _MTD驱动及其用户空间编程
MTD(Memory Technology Device)即常说的Flash等使用存储芯片的存储设备,MTD子系统对应的是块设备驱动框架中的设备驱动层,可以说,MTD就是针对Flash设备设计的标准化 ...