codeforces 1217D 染色问题

该博客讨论了codeforces 1217D问题,涉及有向图的染色策略。当图中无环时,只需要一种颜色,若有环,则至少需要两种颜色。通过深度优先搜索(DFS)遍历图,识别环路并进行染色,确保环内有两种颜色,而其他部分使用一种颜色。

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

题意

现给n个点,m条有向边。给这个图涂色,要求一个环内存在两种颜色,如果无环,就一种颜色即可

分析

我们知道如果整个图无环,颜色数为1,若存在环,颜色数为2
故最大颜色数为2
剩下的就是涂色问题。我们可以dfs遍历,对于一条dfs链我们标记它,当跑到祖宗节点时,我们可以知道出现环,那么给这个边涂色为2,环上其他边涂色为1,回溯过程取消标记,因为一个点可以被多个环覆盖。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T>
void out(T x) { cout << x << endl; }
ll fast_pow(ll a, ll b, ll p) {ll c = 1; while(b) { if(b & 1) c = c * a % p; a = a * a % p; b >>= 1;} return c;}
ll exgcd(ll a, ll b, ll &x, ll &y) { if(!b) {x = 1; y = 0; return a; } ll gcd = exgcd(b, a % b, y, x); y-= a / b * x; return gcd; }
const int N = 5000 + 5;
struct node
{
    int to, next, id;
}edge[N];
int head[N], col[N], num[N], tot, nu;
bool f, mark[N];
void init()
{
    memset(head, -1, sizeof(head));
    memset(num, 0, sizeof(num));
    memset(mark, false, sizeof(mark));
    nu = tot = 0;
    f = false;
}
void add(int u, int v, int id)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    edge[tot].id = id;
    head[u] = tot ++;
}
void dfs(int u)
{
    num[u] = ++ nu;
    mark[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int to = edge[i].to, id = edge[i].id;
        if(!num[to])
        {
            dfs(to);
        }
        else if(mark[to])
        {
            col[id] = 2, f = true;
        }
    }
    mark[u] = false;
}
int main()
{
    ios::sync_with_stdio(false);
    init();
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int u, v;
        cin >> u >> v;
        add(u, v, i);
        col[i] = 1;
    }
    for(int i = 1; i <= n; i ++)
    {
        if(!num[i])
            dfs(i);
    }
    if(f)
        cout << 2 << endl;
    else    
        cout << 1 << endl;
    for(int i = 1; i <= m; i ++)
        cout << col[i] << (i != m ? " " : "\n");
}

### 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、付费专栏及课程。

余额充值