图论 -> 二分图习题

重点

  1. 染色法判断二分图,匈牙利算法求二分图最大匹配。
  2. 二分图仅仅对于无向图来说,不知道那个点在左边,那个点在右边要双向加边。
  3. 匈牙利算法仅仅从左边的点出发向右边。

判断一道题是不是二分图

  1. 分为左右两个集合,集合内的两个点不能放在一起(不能相关)

习题

1. 关押罪犯

意识到这是一个二分问题,让最大冲突为k,所有大于k的边都要在二分图的两侧,判断这样的图是不是二分图,二分枚举k找到最小的k。

/*
 * Author:  Chen_zhuozhuo
 * Created Time:  2020/3/31 9:00:20
 * File Name: b.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxint = -1u>>1;
const int inf = 0x3f3f3f3f;
const int maxn = 20010;
const int maxm = 100010;
int n,m,color[maxn],from,to,wei,head[maxn],cnt = 1;

struct Edge{
    int next, to, wei;
}edge[maxm<<1];

void addedge(int from, int to, int wei){
    edge[cnt].wei = wei;
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt ++;
}

void init(){
    for(int i=0;i<maxn;i++){
        head[i] = -1;
    }
}

bool dye(int now, int c, int limit){
    color[now] = c;
    for(int i = head[now]; ~i; i = edge[i].next){
        if(edge[i].wei <= limit) continue;///小于等于
        int to = edge[i].to;
        
        if(color[to] == c) return false;
        if(!color[to]){
            if(!dye(to, 3 - c, limit)) return false;
        }
    }
    return true;
}

bool check(int limit){
    memset(color, 0, sizeof(color));
    for(int i=1;i<=n;i++){
        if(!color[i]){
            if(!dye(i, 1, limit))
                return false;
        }
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n>>m;
    init();
    for(int i=1;i<=m;i++){
        cin>>from>>to>>wei;
        addedge(from, to, wei);
        addedge(to, from, wei);
    }
    int l = 0, r = 1e9;
    while(l < r){
        int mid = l + r >> 1;
        if(check(mid)){
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    cout<<l<<endl;
    return 0;
}

2. 机器任务

注意:

  1. 谜一样的数组大小。
/*
 * Author:  Chen_zhuozhuo
 * Created Time:  2020/3/31 17:07:27
 * File Name: c.cpp
 */
//376机器任务
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxint = -1u>>1;
const int inf = 0x3f3f3f3f;
const int maxn = 505;
const int maxm = 1005;

///md,谜一样的数组大小

int cnt = 1, head[maxn];
int a[maxn], b[maxn];
int st[maxn], match[maxn], n, m, k, temp;

struct EDGE{
    int next, to;
}edge[maxm<<2];

void init(){
    memset(head, -1, sizeof(head));
    memset(match, -1, sizeof(match));
}

void addedge(int from, int to){
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt ++;
}

bool find(int x){
    for(int i=head[x]; ~i; i=edge[i].next){
        int to = edge[i].to;
        if(st[to]) continue;
        st[to] = 1;
        if(match[to]==-1 || find(match[to])){
            match[to] = x;
            return true;
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    while(cin>>n, n){
        init();
        cin>>m>>k;
        for(int i=0;i<k;i++){
            cin>>temp>>a[i]>>b[i];
            if(a[i] == 0 || b[i] == 0) continue;
            addedge(a[i], b[i]);
        }
        int ans = 0;
        for(int i=1;i<=n;i++){
            memset(st, 0, sizeof(st));
            if(find(i)) ans ++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
输入样例:
5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0
输出样例:
3
*/

3. I题 Error Correction (最大独立集)

题意

给定n和n个字符串,求一个最大的swap-free集合。swap-free:集合内任意两个字符串,不能通过任意两个字符交换位置(不一定相邻),相互得到。

思路

队友想到:把能转移到的字符串相互连边,最多能找到多少不相邻的点。
最后确定的思路:能转移的字符串连边(双向),求最大独立集。因为是双向边,最大匹配算了两次,所以这里 最大独立集 = n -(最大匹配数/2),正常是最大独立集=n-最大匹配。

tip

Q:为啥对能转移到的连边?
A:二分图里,因为能转移到,所以不能放进同一个集合,这是二分图的模型。

/*
 * Author:  Chen_zhuozhuo
 * Created Time:  2020/10/22 22:33:29
 * File Name: a.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define debug(x) cout << #x << ": " << x << endl
#define debug_(x, y) cout << #x << ": " << x <<"   "<<#y<<" : "<<y<< endl
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxint = -1u>>1;
const int inf = 0x3f3f3f3f;
const int maxn = 505;
const int maxm = maxn * maxn;
int cnt = 1, head[maxn];
char str[maxn][30];
int n;
int match[maxn];
bool st[maxn];

struct EDGE{
    int next, to;
}edge[maxm<<1];

void init(){
    for(int i=0;i<maxn;i++)
        head[i] = -1;
}

void addedge(int from, int to){
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt ++;
}

bool check(char *a, char *b){
    int len = strlen(a);
    int cnt_dif = 0;
    for(int i=0;i<len;i++){
        if(a[i] != b[i]){
            cnt_dif ++;
        }
        if(cnt_dif > 2)
            return false;
    }
    if(cnt_dif == 2)
        return true;
    else
        return false;
}

bool find(int x){
    for(int i=head[x]; ~i; i=edge[i].next){
        int to = edge[i].to;
        if(!st[to]){
            st[to] = true;
            if(match[to] == 0 || find(match[to])){
                match[to] = x;
                return true;
            }
        }
    }
    return false;
}

int main() {
    //char a[] = "abcd", b[] = "abdc", c[] = "adbc";
    //cout<<check(a, b)<<"  "<<check(a,c)<<endl;
    scanf("%d", &n);
    init();
    for(int i=1;i<=n;i++){
        scanf("%s", str[i]);
    }
    
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(check(str[i], str[j])){
                addedge(i, j);
                addedge(j, i);
                //cout<<str[i]<<" -> "<<str[j]<<endl;
                //cout<<str[j]<<" -> "<<str[i]<<endl;
            }
        }
    }
    
    int res = 0;
    for(int i=1;i<=n;i++){
        memset(st, false, sizeof st);
        if(find(i)) res ++;
    }
    
    int ans = n - res / 2;
    
    printf("%d\n", ans);
    
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值