[51NOD1816]小C的二分图

本文介绍了一种求解二分图最大匹配问题的高效算法。通过动态维护一棵伸展树(splay tree),实现了对匹配边的管理和更新,确保所有匹配边不相交。适用于大规模数据集,最大支持3×10^5个节点。

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

题目大意

给定一个二分图,两边各有n个点。左边第i个点连接了右边第liri个点。
你需要找一个满足所有匹配边不相交的最大匹配。

1n3×105


题目分析

考虑依次加入左边的点,令fi表示在所有匹配数为i的合法方案中,最后一个右边的点的编号最小值。显然(如果不考虑没有方案的i的话)fi严格单调递增的。
对于所有fi[l,r)转移是用fi+1来更新fi+1,对于最大的满足fi<li,转移是fi+1=l。可以发现这个用splay来维护就是一些加点删点和区间加的操作。最后输出splay的size1就好了。


代码实现

#include <iostream>
#include <cstdio>
#include <cctype>
#include <stack>

using namespace std;

int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (!isdigit(ch)) f=ch=='0'?-1:f,ch=getchar();
    while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x*f;
}

const int N=300050;

int n;

struct SPLAY
{
    int son[N][2],size[N],fa[N],f[N],tag[N],mi[N];
    stack<int> st;
    int tot,root;

    bool side(int x){return son[fa[x]][1]==x;}

    void update(int x){size[x]=size[son[x][0]]+size[son[x][1]]+1,mi[x]=min(f[x],min(mi[son[x][0]],mi[son[x][1]]));}

    void rotate(int x)
    {
        int y=fa[x];bool s=side(x);
        if (fa[y]) son[fa[y]][side(y)]=x;
        if (son[x][s^1]) fa[son[x][s^1]]=y;
        son[y][s]=son[x][s^1],son[x][s^1]=y;
        fa[x]=fa[y],fa[y]=x;
        update(y),update(x);
    }

    void ADD(int x,int y){f[x]+=y,mi[x]+=y,tag[x]+=y;}

    void clear(int x)
    {
        if (tag[x])
        {
            if (son[x][0]) ADD(son[x][0],tag[x]);
            if (son[x][1]) ADD(son[x][1],tag[x]);
            tag[x]=0;
        }
    }

    void pushdown(int x,int y)
    {
        for (;x!=y;st.push(x),x=fa[x]);
        for (;!st.empty();clear(st.top()),st.pop());
    }

    void splay(int x,int y)
    {
        for (pushdown(x,y);fa[x]!=y;rotate(x))
            if (fa[fa[x]]!=y)
                if (side(x)==side(fa[x])) rotate(fa[x]);
                else rotate(x);
    }

    int search(int x,int y)
    {
        clear(x);
        if (mi[son[x][1]]<=y) return search(son[x][1],y);
        return f[x]<=y?x:search(son[x][0],y);
    }

    int leftward(int x){return clear(x),son[x][0]?leftward(son[x][0]):x;}

    int kth(int x,int y)
    {
        clear(x);
        if (size[son[x][0]]+1==y) return x;
        return size[son[x][0]]>=y?kth(son[x][0],y):kth(son[x][1],y-size[son[x][0]]-1);
    }

    void merge(int x,int y,int &rt)
    {
        if (!x) rt=y;
        else if (!y) rt=x;
        else splay(rt=kth(x,size[x]),0),update(fa[son[rt][1]=y]=rt);
    }

    /*
    void split(int x,int y,int &l,int &r)
    {
        if (!y) l=0,r=x;
        else splay(l=kth(x,size[x]),0),fa[r=son[l][1]]=0,son[l][1]=0,update(l);
    }
    */

    void modify(int l,int r)
    {
        int x,y,z;
        splay(y=search(root,r-1),0),root=y,splay(x=search(root,l-1),0),root=x;
        if (x!=y)
        {
            splay(y,x);
            if (z=son[y][1]) fa[z]=son[y][1]=0,update(y);
            fa[y]=son[x][1]=0,f[++tot]=l,update(son[fa[tot]=x][1]=tot),update(x);
            ADD(y,1),merge(y,z,z),merge(x,z,root);
        }
        else
        {
            if (z=son[x][1]) fa[z]=son[x][1]=0,update(x);
            f[++tot]=l,update(son[fa[tot]=x][1]=tot),update(x);
            merge(x,z,root);
        }
        y=x==y?tot:y,splay(y,0),root=y;
        if (son[y][1])
        {
            splay(x=leftward(son[y][1]),0),y=son[x][0],z=son[x][1];
            fa[y]=son[x][0]=0,fa[z]=son[x][1]=0,merge(y,z,root);
        }
    }

    void init(){mi[0]=n+1,f[root=tot=1]=0,size[tot]=1;}
}t;

int main()
{
    freopen("bipartite.in","r",stdin),freopen("bipartite.out","w",stdout);
    n=read(),t.init();
    for (int i=1,l,r;i<=n;++i) l=read(),r=read(),t.modify(l,r);
    printf("%d\n",t.size[t.root]-1);
    fclose(stdin),fclose(stdout);
    return 0;
}
题目 51nod 3478 涉及一个矩阵问题,要求通过最少的操作次数,使得矩阵中至少有 `RowCount` 行和 `ColumnCount` 列是回文的。解决这个问题的关键在于如何高效地枚举所有可能的行和列组合,并计算每种组合所需的操作次数。 ### 解法思路 1. **预处理每一行和每一列变为回文所需的最少操作次数**: - 对于每一行,计算将其变为回文所需的最少操作次数。这可以通过比较每对对称位置的值是否相同来完成。 - 对于每一列,计算将其变为回文所需的最少操作次数,方法同上。 2. **枚举所有可能的行和列组合**: - 由于 `N` 和 `M` 的最大值为 8,因此可以枚举所有可能的行组合和列组合。 - 对于每一种组合,计算其所需的最少操作次数,并取最小值。 3. **计算操作次数**: - 对于每一种组合,需要计算哪些行和列需要修改,并且注意行和列的交叉点可能会重复计算,因此需要去重。 ### 代码实现 以下是一个可能的实现方式,使用了枚举和位运算来处理组合问题: ```python def min_operations_to_palindrome(matrix, row_count, col_count): import itertools N = len(matrix) M = len(matrix[0]) # Precompute the cost to make each row a palindrome row_cost = [] for i in range(N): cost = 0 for j in range(M // 2): if matrix[i][j] != matrix[i][M - 1 - j]: cost += 1 row_cost.append(cost) # Precompute the cost to make each column a palindrome col_cost = [] for j in range(M): cost = 0 for i in range(N // 2): if matrix[i][j] != matrix[N - 1 - i][j]: cost += 1 col_cost.append(cost) min_total_cost = float('inf') # Enumerate all combinations of rows and columns rows = list(range(N)) cols = list(range(M)) from itertools import combinations for row_comb in combinations(rows, row_count): for col_comb in combinations(cols, col_count): # Calculate the cost for this combination cost = 0 # Add row costs for r in row_comb: cost += row_cost[r] # Add column costs for c in col_comb: cost += col_cost[c] # Subtract the overlapping cells for r in row_comb: for c in col_comb: # Check if this cell is part of the palindrome calculation if r < N // 2 and c < M // 2: if matrix[r][c] != matrix[r][M - 1 - c] and matrix[N - 1 - r][c] != matrix[N - 1 - r][M - 1 - c]: cost -= 1 min_total_cost = min(min_total_cost, cost) return min_total_cost # Example usage matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] row_count = 2 col_count = 2 result = min_operations_to_palindrome(matrix, row_count, col_count) print(result) ``` ### 代码说明 - **预处理成本**:首先计算每一行和每一列变为回文所需的最少操作次数。 - **枚举组合**:使用 `itertools.combinations` 枚举所有可能的行和列组合。 - **计算成本**:对于每一种组合,计算其成本,并考虑行和列交叉点的重复计算问题。 ### 复杂度分析 - **时间复杂度**:由于 `N` 和 `M` 的最大值为 8,因此枚举所有组合的时间复杂度为 $ O(N^{RowCount} \times M^{ColCount}) $,这在实际中是可接受的。 - **空间复杂度**:主要是存储预处理的成本,空间复杂度为 $ O(N + M) $。 ### 相关问题 1. 如何优化矩阵中行和列的枚举组合以减少计算时间? 2. 在计算行和列的交叉点时,如何更高效地处理重复计算的问题? 3. 如果矩阵的大小增加到更大的范围,如何调整算法以保持效率? 4. 如何处理矩阵中行和列的回文条件不同时的情况? 5. 如何扩展算法以支持更多的操作类型,例如翻转某个区域的值?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值