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: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图 任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...
随机推荐
- 关于IE低版本兼容问题
1,元素浮动之后,能设置宽度的话就给元素加宽度.如果需要宽度是内容撑开,就给它里边的块元素加上浮动: 解决方案:给需要宽度由内容撑开的元素加上浮动 css样式: <style> .box{ ...
- java深拷贝和浅拷贝
1.概念 java里的clone分为: A:浅复制(浅克隆): 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象. b:深复制(深克隆):深复制把要复制的对象所引用的对象都复制了一遍. Java中对 ...
- java基础之基础语法详录(一)
[前言] java的语法先从基础语法学,Java语言是由类和对象组成的,其对象和类又是由方法和变量组成,而方法,又包含了语句和表达式. 对象:(几乎)一切都是对象,比如:一只熊猫,他的外观,颜色,他在 ...
- ZJOI2017 Day2
私のZJOI Day2 2017-3-22 08:00:07 AtCoder试题选讲 SYC(Sun Yican) from Shaoxing No.1 High School 2017-3-22 0 ...
- Android开发之自定义视图
继承View 1.重写onMeasure(int wMeasureSpec,int hMeasureSpec)处理程序,这样可以标明视图尺寸 2.重写onDraw,以便绘制我们自己的自定义视图内 3. ...
- angular element()
使用angular.element()获取一个dom的方法. 1.可以使用jquery的选择器 2.可以使用javascript的原生的的查找元素的方法 下面是angular.element()提供的 ...
- yii2邮件配置教程,报Expected response code 250 but got code "553"原因
main.php(或main-local.php)中的邮件配置如下: 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPat ...
- JAVA源码剖析(容器篇)HashMap解析(JDK7)
Map集合: HashMap底层结构示意图: HashMap是一个“链表散列”,其底层是一个数组,数组里面的每一项都是一条单链表. 数组和链表中每一项存的都是一“Entry对象”,该对象内部拥有key ...
- Python快速入门(1)
FROM:实验楼 http://python.usyiyi.cn/python_278/tutorial/index.html http://woodpecker.org.cn/abyteofpyth ...
- PPAPI插件开发指南
转载请注明出处:http://www.cnblogs.com/fangkm/p/4401075.html 前言 插件一直是浏览器的重要组成部分,丰富浏览器的运行能力,实现一些HTML+JS实现不了本地 ...