BZOJ 2788 Poi2012 Festival 差分约束+Tarjan+Floyd

本文介绍了一种解决差分约束系统的方法,包括构建图模型、使用Floyd算法检查可行性、通过Tarjan算法进行强连通分量压缩,并计算每个强连通分量内变量的不同取值数量。

题目大意:给定n个正整数变量和m1+m2个限制条件,每个形如xi+1=yixi<=yi,求这些变量最多能有多少个不同的取值

首先我们可以根据差分约束建图,Floyd跑最短路,判断是否无解
然后Tarjan缩点,显然不同强连通分量之间互不影响
一个强连通分量内的最多取值个数等于强连通分量两两之间最短路的最大值+1

证明:
由于边权只有{0,1,1}三种,因此取值数=最大值-最小值+1
不妨设最短路的最大值为ans,那么对于这个差分约束系统的任意一组解,我选择最小的数x和最大的数y,由于这个图强连通,因此xy必然存在至少一条路径
不妨设x>y的最短路径长度为z,那么取值数1=yxzans
显然我们可以构造出一组解使得取值数1=ans,故ans就是最大的取值数

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 660
using namespace std;
int n,m1,m2,ans;
struct abcd{
    int to,next;
}table[200200];
int head[M],tot;
int f[M][M];
int dpt[M],low[M],T;
void Add(int x,int y)
{
    table[++tot].to=y;
    table[tot].next=head[x];
    head[x]=tot;
}
void Calculate(vector<int> &stack)
{
    int i,j,re=0;
    for(i=0;i<(signed)stack.size();i++)
        for(j=0;j<(signed)stack.size();j++)
            re=max(re,f[stack[i]][stack[j]]);
    ans+=re+1;
}
void Tarjan(int x)
{
    static int stack[M],top;
    static bool v[M];
    int i;
    dpt[x]=low[x]=++T;
    stack[++top]=x;
    for(i=head[x];i;i=table[i].next)
    {
        if(v[table[i].to])
            continue;
        if(dpt[table[i].to])
            low[x]=min(low[x],dpt[table[i].to]);
        else
            Tarjan(table[i].to),low[x]=min(low[x],low[table[i].to]);
    }
    if(dpt[x]==low[x])
    {
        int t;
        vector<int> *s=new vector<int>;
        do{
            t=stack[top--];
            v[t]=true;
            s->push_back(t);
        }while(t!=x);
        Calculate(*s);
    }
}
int main()
{
    int i,j,k,x,y;
    cin>>n>>m1>>m2;
    memset(f,0x3f,sizeof f);
    for(i=1;i<=m1;i++)
    {
        scanf("%d%d",&x,&y);
        Add(x,y);Add(y,x);
        f[x][y]=min(f[x][y],1);
        f[y][x]=min(f[y][x],-1);
    }
    for(i=1;i<=m2;i++)
    {
        scanf("%d%d",&x,&y);
        Add(y,x);
        f[y][x]=min(f[y][x],0);
    }
    for(i=1;i<=n;i++)
        f[i][i]=0;
    for(k=1;k<=n;k++)
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
    for(i=1;i<=n;i++)
        if(f[i][i]<0)
            return puts("NIE"),0;
    for(i=1;i<=n;i++)
        if(!dpt[i])
            Tarjan(i);
    cout<<ans<<endl;
    return 0;
}
### BZOJ1728 Two-Headed Cows (双头牛) 的解题思路 #### 题目概述 BZOJ1728 是一道经典的图论问题,题目描述了一群双头牛之间的关系网络。每只双头牛可以看作是一个节点,而它们的关系则构成了边。目标是从这些关系中找出满足特定条件的最大子集。 此问题的核心在于利用 **二分查找** 和 **染色法** 来验证是否存在符合条件的子图结构[^1]。 --- #### 解题核心概念 ##### 1. 图模型构建 该问题可以通过无向图建模,其中每个顶点代表一只双头牛,边表示两只双头牛之间存在某种关联。最终的目标是在这个图中找到最大的独立集合(Independent Set),即任意两个顶点都不相连的一组顶点[^2]。 ##### 2. 二分查找的应用 为了高效求解最大独立集大小 \( k \),采用二分策略来逐步逼近最优解。具体来说,在区间 [0, n] 中通过不断调整上下界寻找可能的最大值 \( k \)[^3]。 ##### 3. 染色法验证可行性 对于当前假设的最大独立集大小 \( mid \),尝试从原图中选取恰好 \( mid \) 个顶点构成候选集合,并检查其是否形成合法的独立集。这一过程通常借助 BFS 或 DFS 实现,同时配合颜色标记技术区分已访问状态以及检测冲突情况[^4]。 以下是基于 Python 的伪代码实现: ```python from collections import deque def bfs_coloring(graph, start_node): queue = deque() color_map = {} # 初始化起点的颜色为 0 color_map[start_node] = 0 queue.append(start_node) while queue: current = queue.popleft() for neighbor in graph[current]: if neighbor not in color_map: # 给邻居分配相反的颜色 color_map[neighbor] = 1 - color_map[current] queue.append(neighbor) elif color_map[neighbor] == color_map[current]: return False # 如果发现相邻节点有相同颜色,则无法完成有效染色 return True def is_possible_to_select_k(graph, nodes_count, target_size): from itertools import combinations all_nodes = list(range(nodes_count)) possible_combinations = combinations(all_nodes, target_size) for subset in possible_combinations: subgraph = {node: [] for node in subset} valid_subset = True for u in subset: for v in graph[u]: if v in subset and v != u: subgraph[u].append(v) # 对子图进行染色测试 colors_used = set() coloring_success = True for node in subset: if node not in colors_used: success = bfs_coloring(subgraph, node) if not success: coloring_success = False break if coloring_success: return True # 找到一个有效的组合即可返回成功标志 return False def binary_search_max_independent_set(graph, total_nodes): low, high = 0, total_nodes best_result = 0 while low <= high: mid = (low + high) // 2 if is_possible_to_select_k(graph, total_nodes, mid): best_result = mid low = mid + 1 else: high = mid - 1 return best_result ``` --- #### 复杂度分析 上述算法的时间复杂度主要取决于以下几个方面: - 枚举所有可能的子集规模:\( O(\binom{n}{k}) \), 其中 \( k \) 表示当前试探的独立集大小。 - 子图构造与染色检验操作:每次调用 `bfs_coloring` 函数需遍历整个子图,最坏情况下时间开销接近线性级别 \( O(k^2) \). 综合来看整体效率较高但仍有优化空间[^5]. --- #### 总结 通过对 BZOJ1728 进行深入剖析可知,合理运用二分加染色的方法能够显著提升解决问题的能力。这种方法不仅适用于本题场景下寻找最大独立集的任务需求,同时也可推广至其他相似类型的 NP 完全难题处理之中[^6]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值