首页 技术 正文
技术 2022年11月16日
0 收藏 393 点赞 2,304 浏览 2732 个字

题目链接:

点这里

题目

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902