题目链接:

点这里

题目

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 奇环的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)

    http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...

  4. Codeforces Round #311 (Div. 2) A,B,C,D,E

    A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdli ...

  5. Codeforces Round #311 (Div. 2)题解

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. tar命令: 对某目录文件打tar包时,排除指定的目录或文件

     如某当前目录存在以下文件或目录: 1.txt2.txt3.txtdir1dir2my2015.tarmy2016.tar 若要对当前目录除1.txt 和dir1.tar外,打包tar 步骤一.建立e ...

  2. linux 内核和应用程序区别

    应用程序存在于虚拟内存中, 有一个非常大的堆栈区. 堆栈, 当然, 是用来保存函数调用历史以及所有的由当前活跃的函数创建的自动变量. 内核, 相反, 有一个非常小的堆栈; 它可能小到一个, 4096 ...

  3. 初探内联方式的 onload="doSomething()"为何要加"()"?而js代码的 onload="doSomething" 和 addEventListener 为何不加"()"?

    问题引入:在看<Jquery基础教程>第四版的时,P34页有这样一段话 引用函数与调用函数 这里在将函数指定为处理程序时,省略了后面的圆括号,只使用了函数名.如果带着圆括号,函数会被立即调 ...

  4. hdu 1284 钱币兑换问题 完全背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1284 递推公式:dp[i] = sum(dp[i], dp[i-C]) /* 钱币兑换问题 Time ...

  5. java虚拟机理解探索1

    以下内容源于个人对<深入java虚拟机>的理解总结 基本概念: java虚拟机可以指一种抽象规范,也可以指一种具体实现,亦可以指一个java虚拟机实例. 虚拟机生命周期: 一个java虚拟 ...

  6. 阿里巴巴2013年实习生笔试题A

    一.单项选择题 1.下列说法不正确的是:(B) A.SATA硬盘的速度速度大约为500Mbps/s B.读取18XDVD光盘数据的速度为1Gbps C.前兆以太网的数据读取速度为1Gpbs D.读取D ...

  7. 分布式MySQL 数据库

    http://zhangxugg-163-com.iteye.com/blog/1666673 而本文所描述的 federated属于 MySQL的一种特殊引擎,利用它可将本地数据表映射至远程 MyS ...

  8. windows phone 8 开发系列(一)环境搭建

    一:前奏说明 本人一名普通的neter,对新玩意有点小兴趣,之前wp7出来的时候,折腾学习过点wp7开发,后来也没怎么用到(主要对微软抛弃wp7的行为比较不爽),现在wp8已经出来一段时间了,市场上也 ...

  9. JVM学习---JAVA内存

    一.JAVA运行时数据区域:JAVA中的运行时内存区域有的随着虚拟机进程的启动而存在,有的区域则是依赖用户线程的启动和结束而建立和销毁的.包括以下的几个区域. 图. JAVA虚拟机运行时数据区 1.程 ...

  10. CentOS安全设置

    删除多余的用户和用户组,修改口令文件属性,禁止[Ctrl+Alt+Delete]重启命令,防止别人ping的方法.整理自互联网. 1.删除多余的用户和用户组 //删除多余用户 # vi /etc/pa ...