题目链接:

点这里

题目

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. CSS之表格操作

    表格的colspan和rowspan属性参考:http://erpoperator.blog.163.com/blog/static/17899637220123993031921/ colspan是 ...

  2. Memcached 使用

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. 安装ie10慢的解决办法

    下面是win7安装ie10的先决条件: http://support.microsoft.com/kb/2818833

  4. iOS - 网络语线程(OC)

    1. 检测网络状态 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the vi ...

  5. 【学习笔记】【C语言】函数

    一. 什么是函数 任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”.所以,你可以说C语言程序是由函数构成的. 比如你用C语言编写了一 ...

  6. 将DataTable格式化为json字符串返回

    一般用于ajax局部刷新的使用比较多,通过查询得到了DataTable数据,要想将数据放回需要将DataTable转换为json格式,以下为转换的调用函数: string json = "& ...

  7. hdu 1002大数(Java)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 嵌入式 linux 移植修改后的libjpeg 实现内存中解码

    1.修改libjpeg源码,使之实现内存解码. 修改libjpeg中读取或者输出jpeg文件的函数接口文件jdatadst.c和jdatasrc.c见下面这篇帖子. http://blog.163.c ...

  9. DFS入门之一

    深度优先搜索实现较为简单,需要控制两个因素: 1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍) 2.越界这种情况是不允许的 以杭电的1312 Red and Black 为例 ...

  10. Linux常用命令--文件的压缩和解压缩

    在Linux系统中,我们通常使用的文件压缩命令有:bunzip2 , bzip2 , cpio , gunzip , gzip ,split(切割文件) , zgrep(在压缩文件中寻找匹配的正则表达 ...