[原创]Codeforces 557D - Vitaly and Cycle [二分图染色]【图论】
[原创]Codeforces 557D - Vitaly and Cycle [二分图染色]【图论】
2017-06-21 23:51:59 Tabris_ 阅读数:219
博客爬取于2020-06-14 22:39:57
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/73556860
题目链接:http://codeforces.com/problemset/problem/557/D
——————————————————————————————————————
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.
Input
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.
Output
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.
Examples
input
4 4
1 2
1 3
4 2
4 3
output
1 2
input
3 3
1 2
2 3
3 1
output
0 1
input
3 0
output
3 1
Note
The simple cycle is a cycle that doesn’t contain any vertex twice.
——————————————————————————————————————
题目大意:
就是现在一个N个节点,M条边的无向图,问你最少添加多少条边,能找到一个奇圈,添加的方法有多少个
解题思路:
判断奇圈偶圈,很容易想到二分图染色,那么通过二分图染色,就能确定很多信息了,
首先能确定的是最多也就添加3条边, 3条边就能将三个点变成一个奇圈了,
那么添加边就是要变成一个3个节点的奇圈
那么就分这四种情况讨论
1.添加0条边
就是原图中有奇圈了, 通过染色判断,方案数就是1了
2.添加1条边
只需添加一条边的时候就是一个点a连向点b,c. 这时候将b,c连上就好了
这种情况需要在一侧的多个节点都能和另一边的节点直接或间接连上才行,
所以我用了一个并查集维护联通性,然后统计个数
3.添加2条边
如果一个节点至多被一条边连着,
那么想要构成奇圈,就要将边的两个端点连上另外的一个一个节点构成奇圈
方案数就是
4.添加3条边
只有一条边都没有的时候才要添加三条边,
方案数就是在n个点取3个节点的方案数
分类讨论即可
附本题代码
——————————————————————————————————————
1 | # include <bits/stdc++.h> |