Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环
题目链接:
题目
D. Vitaly and Cycle
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意
给你一个无向图,问你最少添加几条边可以形成奇环,并且输出不同的方式数。
题解
最多只要添加三条边,所以我们可以分类讨论:
- 添加三条边
一条边都没有的时候
ans=C[n][3] - 添加两条边
每个顶点的度最大为1的时候
ans=m*(n-2) - 添加一条边
对每个连通块黑白染色,假设一个连通块黑的有b个,白的有w个,则:
ans+=b(b-1)/2+w(w-1)/2
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = maxn * 2;
typedef __int64 LL;
int n, m;
vector<int> G[maxn];
int deg[maxn];
int color[maxn];
void dfs(int u, int fa,LL &cnt,LL &r,LL &b) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
if (!color[v]) {
color[v] = 3 - color[u];
if (color[v] == 1) r++;
else b++;
dfs(v, u, cnt, r, b);
}
else {
if (color[v] == color[u]) {
//printf("u:%d,v:%d\n", u, v);
cnt++;
}
}
}
}
int main() {
memset(deg, 0, sizeof(deg));
scanf("%d%d", &n, &m);
int maxv = -1;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u,&v),u--,v--;
G[u].push_back(v);
G[v].push_back(u);
deg[u]++, deg[v]++;
}
for (int i = 0; i < n; i++) maxv = max(maxv, deg[i]);
if (m == 0) {
LL ans = (LL)n*(n - 1)*(n - 2) / 6;
printf("3 %I64d\n", ans);
return 0;
}
if (maxv < 2) {
LL ans = (LL)m*(n - 2);
printf("2 %I64d\n", ans);
return 0;
}
memset(color,0,sizeof(color));
LL cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
LL r = 1, b = 0;
if (!color[i]) {
color[i] = 1;
dfs(i, -1,cnt1,r,b);
cnt2 += r*(r - 1) / 2 + b*(b - 1) / 2;
}
}
if (cnt1 == 0) {
printf("1 %I64d\n", cnt2);
}
else {
printf("0 %I64d\n", cnt1/2);
}
return 0;
}
Codeforces Round #311 (Div. 2) 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 test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)
http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...
- Codeforces Round #311 (Div. 2) A,B,C,D,E
A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdli ...
- Codeforces Round #311 (Div. 2)题解
A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #330 (Div. 2) A. Vitaly and Night 暴力
A. Vitaly and Night Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/p ...
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset
C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...
随机推荐
- Sql 求比率 类型 影响 结果
- DateDiff函数 asp运算时间
DateDiff DateDiff函数 返回 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目. 语法 DateDiff(interval, date1, date2[, f ...
- UI1_HTTP下载
// DataModel.h // UI1_HTTP下载 // // Created by zhangxueming on 15/7/17. // Copyright (c) 2015年 zhangx ...
- Eclipse+GitHub
之前一直想研究github的使用,但一直没时间,今天抽空学习了一下,发现真的是非常好用!!! 准备材料 1.你要有最新版的Eclipse(不要问我为什么要最新版的,反正我用的是最新版本) 2.一个gi ...
- 《搭建DNS负载均衡服务》RHEL6
搭建DNS负载均衡环境: 1.至少三台的linux虚拟机,一台主的DNS服务器,1台副的(可以N台),1台测试机. 负载均衡有很多种,apache那样的是为了缓解人们访问网站时给服务器造成太大的压力, ...
- Berkeley DB
最近用BDB写点东西,写了挺多个测试工程.列下表,也理清楚最近的思路 1.测试BDB程序,包括打开增加记录,查询记录,获取所有记录.将数据转存mysql 程序的不足,增加记录仅仅只有key和value ...
- 无法安装程序包“MIcrosoft.Owin.Security 2.0.2”。您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.0”作为目标的项目中。
在VS2010 MVC4项目中,安装NuGet程序包Microsoft.AspNet.SignalR时出现以下错误: 原因是安装的版本是Microsoft.AspNet.SignalR 2.0.2,要 ...
- js 鼠标事件的抓取代码
js 鼠标事件的抓取代码,分享给大家. 1.通过ele.setCapture();设置鼠标事件的抓取. 2,应用可以通过单.双击文字来获取时间. <html> <head> & ...
- Configuration python CGI in XAMPP in win-7
1.After install XAMPP,we need add the path of the Mysql just find the path and add it to your sys-pa ...
- hadoop自动安装的脚本与步骤
最近要在10几台机器上安装hadoop.对于这种繁复而重复的工作,一步步的打命令行,对于程序员来说是一件不能忍的事情.所以我就琢磨着怎么写一个脚本来自动安装hadoop. 任务: 在10几台机器上中的 ...