Codeforces 557D Vitaly and Cycle 【染色判二分图 + 组合数学】

本文介绍了一种解决特定图论问题的算法——如何在给定的无向图中找到形成简单奇数长度环所需的最少边数及方案数。讨论了算法实现细节,包括特殊情况处理、连通组件分析和二分图判定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 557D 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.

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.

题意:给定一个无向图,问你最少增加多少条边可以得到一个奇圈。问方案数。

思路:考虑奇圈的判定,如果连通图不是一个二分图,必定存在奇圈。这是一个充要条件。

首先比较坑的地方有两个:
1、无边时,需要连3条边,方案数为n * (n-1) * (n-2) / 6。
2、不存在节点数目大于2的连通分支时,需要连2条边,方案数为(n-2) * m。

其他情况,我们先判断每一个连通分支,有一个不是二分图,那么一定存在奇圈。反之,我们黑白染色每个连通分支里面的节点,对于任意的两个黑点,总可以得到一个奇圈,同理白点。那么我们就统计每个连通分支里面黑点数目cnt1[]、白点数目cnt2[],对整体贡献是cnt1[i] * (cnt1[i] - 1) / 2 + cnt2[i] * (cnt2[i] - 1) / 2。

AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
vector<int> G[MAXN];
bool vis[MAXN];
int color[MAXN], num[MAXN], pos[MAXN];
int cnt1[MAXN], cnt2[MAXN];
bool judge(int u, int id) {
    if(!vis[u]) num[id]++;
    pos[u] = id; vis[u] = true;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(color[u] == color[v]) return false;
        if(!color[v]) {
            color[v] = 3 - color[u];
            if(!judge(v, id)) return false;
        }
    }
    return true;
}
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF) {
        if(m == 0) {
            printf("3 %lld\n", 1LL * n * (n-1) * (n-2) / 6);
            continue;
        }
        for(int i = 1; i <= n; i++) {
            vis[i] = false; G[i].clear();
            num[i] = color[i] = 0;
            cnt1[i] = cnt2[i] = 0;
        }
        for(int i = 0; i < m; i++) {
            int u, v; scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        bool flag = false; int top = 1;
        for(int i = 1; i <= n; i++) {
            if(vis[i]) continue;
            color[i] = 1;
            if(!judge(i, top)) {
                flag = true; break;
            }
            top++;
        }
        if(flag) {
            printf("0 1\n");
            continue;
        }
        for(int i = 1; i <= n; i++) {
            if(color[i] == 1) {
                cnt1[pos[i]]++;
            }
            else {
                cnt2[pos[i]]++;
            }
        }
        LL ans = 0;
        for(int i = 1; i < top; i++) {
            //cout << cnt1[i] << ' ' << cnt2[i] << endl;
            if(num[i] <= 2) continue;
            ans += 1LL * cnt1[i] * (cnt1[i] - 1) / 2 + 1LL * cnt2[i] * (cnt2[i] - 1) / 2;
        }
        if(ans == 0) {
            printf("2 %lld\n", 1LL * (n-2) * m);
        }
        else {
            printf("1 %lld\n", ans);
        }
    }
    return 0;
}
### Codeforces 平台上的二分图染色算法题目 #### CF1949I. Disks 的解题思路 对于给定的一组圆盘,断是否存在一种方式使得这些圆盘能够通过改变大小来满足特定条件。如果存在某个圆既变小又变大,或者变小的圆的数量不等于变大的圆的数量,则该连通块无法实现目标状态[^5]。 为了验证这一点,可以采用二分图染色的方法: - 将每个圆视为图中的节点; - 如果两个圆之间有交集,则在这两个节点间建立一条边; - 使用两种不同的颜色尝试对整个图形进行着色,在此过程中遇到冲突则说明不存在合法方案; 这种方法不仅适用于本题描述的情况,也广泛应用于其他涉及二分性质的问题求解中。 #### 构造性算法实例 在解决一些具有构造特性的编程挑战时,比如构建IP地址这样的任务[T1 IP地址(ip)][^2],虽然这并不是典型的二分图问题,但是当涉及到如何有效地分配资源(如同一网络内的设备编号),也可以借鉴类似的思维模式——即合理规划不同部分之间的关系以达到最优配置效果。 ```cpp // C++ code snippet demonstrating bipartite graph coloring approach #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 7; int color[MAXN]; vector<int> adj[MAXN]; bool dfs(int u, int c) { color[u] = c; for (auto v : adj[u]) { if (!color[v]) { if (!dfs(v, 3 - c)) return false; // alternate colors between 1 and 2 } else if (color[v] == c) return false; // found an odd cycle or conflict } return true; } void solve() { memset(color, 0, sizeof(color)); bool isBipartite = true; for (int i = 1; i <= n && isBipartite; ++i) { if (!color[i]) isBipartite &= dfs(i, 1); } cout << (isBipartite ? "YES\n" : "NO\n"); } ``` 上述代码展示了利用深度优先搜索(DFS)来进行二分图检测的过程。这里假设输入已经准备好了一个无向图的数据结构`adj[]`表示邻接表形式存储的图以及顶点数量n。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值