• HDU 1068 Girls and Boys(匈牙利算法求最大独立集)


    Problem Description

    the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.

    The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

    the number of students
    the description of each student, in the following format
    student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 …
    or
    student_identifier:(0)

    The student_identifier is an integer number between 0 and n-1, for n subjects.
    For each given data set, the program should write to standard output a line containing the result.

    Sample Input

    7
    0: (3) 4 5 6
    1: (2) 4 6
    2: (0)
    3: (0)
    4: (2) 0 1
    5: (1) 0
    6: (2) 0 1
    3
    0: (2) 1 2
    1: (1) 0
    2: (1) 0

    Sample Output

    5
    2

    题意

    题目给定一些男女生之间相互的 romantic 关系,要求找出一个最大的集合,使得该集合中的所有男女生之间都不存在 romantic 关系。

    分析

    一个二分图的最大独立集点数与最大二分匹配个数有直接的关系:

    [最大独立集点数 = 顶点数 - 最大二分匹配对数 ]

    故本题直接转化为求最大二分匹配即可,需要注意的是,题中给出的条件是 1 指向 2,2 也会指向 1,所以最终算出来的匹配数其实是实际对数的两倍,最终被顶点数减去之前首先需要折半。

    基础二分匹配练手题。

    #include<bits/stdc++.h>
    #define sd(n) scanf("%d",&n)
    using namespace std;
    const int N = 1e3 + 10;
    int e[N][N], match[N];
    bool book[N];
    int n, f[N];
    
    bool dfs(int x) {
    	for (int i = 0; i < n; ++i) {
    		if (e[x][i] && !book[i]) {
    			book[i] = true;
    			if (match[i] == -1 || dfs(match[i])) {
    				match[i] = x;
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    int main() {
    	while (~scanf("%d", &n)) {
    		memset(e, 0, sizeof e);//多组输入,别忘记初始化图啊,TLE几发了。。。
    		for (int i = 0; i < n; i++)
    			match[i] = -1;
    		for (int i = 0; i < n; ++i) {
    			int t1, t2, u;
    			scanf("%d: (%d)", &t1, &t2);
    			for (int j = 0; j < t2; j++){
    				scanf("%d", &u);
    				e[t1][u] = 1;
    			}
    		}
    		int cnt = 0;
    		for (int i = 0; i < n; ++i) {
    			memset(book, false, sizeof book);
    			if (dfs(i))
    				cnt++;
    		}
    		cout << n - cnt / 2 << endl;
    	}
    }
    
  • 相关阅读:
    2017.1.16【初中部 】普及组模拟赛C组总结
    用Redis实现分布式锁 与 实现任务队列
    Mysql+Keepalived双主热备高可用操作记录
    Linux下防御DDOS攻击的操作梳理
    真正的ddos防御之道,简单干脆有效!
    ip黑白名单防火墙frdev的原理与实现
    一种简单的处理大流量访问的方法
    PHP解决网站大流量与高并发
    PHP反射机制实现自动依赖注入
    nginx 根据域名和地址跳转
  • 原文地址:https://www.cnblogs.com/RioTian/p/13484868.html
Copyright © 2020-2023  润新知