Gym - 101915D Largest Group 最大团

给你一个二分图 问你最大团为多大

解一:状压DP

解二:二分图最大匹配

二分图的最大团=补图的最大独立集

二分图最大独立集=二分图定点个数-最大匹配

//Hungary
#include<bits/stdc++.h>
using namespace std;
#define N 50
int useif[N];   //记录y中节点是否使用 0表示没有访问过,1为访问过
int link[N];   //记录当前与y节点相连的x的节点
int mat[N][N]; //记录连接x和y的边,如果i和j之间有边则为1,否则为0
int gn, gm;   //二分图中x和y中点的数目
int can(int t) {
        int i;
        for (i = 1; i <= gm; i++) {
                if (useif[i] == 0 && mat[t][i]) {
                        useif[i] = 1;
                        if (link[i] == -1 || can(link[i])) {
                                link[i] = t;
                                return 1;
                        }
                }
        }
        return 0;
}
int MaxMatch() {
        int i, num;
        num = 0;
        memset(link, 0xff, sizeof(link));
        for (i = 1; i <= gn; i++) {
                memset(useif, 0, sizeof(useif));
                if (can(i)) {
                        num++;
                }
        }
        return num;
}
int main() {
        int TCASE;
        scanf("%d", &TCASE);
        while (TCASE--) {
                int n, k;
                int u, v;
                scanf("%d %d", &n, &k);
                gn = gm = n;
                memset(mat, 0, sizeof(mat));
                for (int i = 1; i <= k; i++) {
                        scanf("%d %d", &u, &v);
                        mat[u][v] = 1;
                }
                for (int i = 1; i <= n; i++) {
                        for (int j = 1; j <= n; j++) {
                                mat[i][j] = mat[i][j] ^ 1;
                        }
                }
                int ans = n * 2;
                ans -= MaxMatch();
                printf("%d\n", ans);
        }
}

原文地址:https://www.cnblogs.com/Aragaki/p/9823328.html

时间: 2024-11-09 02:59:58

Gym - 101915D Largest Group 最大团的相关文章

Gym - 101915D Largest Group 最大独立集 Or 状态压缩DP

题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一边女生一边的,然后一种解法就是 求图的最大独立集,(看起来很巧,实则也是一种套路) (最大独立集是一个点集,其中任意两点在图中无对应边,对于一般图来说,最大独立集是一个NP完全问题,对于二分图来说最大独立集=|V|-二分图的最大匹配数) 我们本来连边是,所有的朋友关系连边(男女就行了,同性都可以忽略

POJ2985 The k-th Largest Group[树状数组求第k大值 并查集]

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted: 2875 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g

POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Accepted: 2847 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants

poj 2985 The k-th Largest Group 求第K大数 Treap, Binary Index Tree, Segment Tree

题目链接:点击打开链接 题意:有两种操作,合并集合,查询第K大集合的元素个数.(总操作次数为2*10^5) 解法: 1.Treap 2.树状数组 |-二分找第K大数 |-二进制思想,逼近第K大数 3.线段树 4.... Treap模板(静态数组) #include <math.h> #include <time.h> #include <stdio.h> #include <limits.h> #include <stdlib.h> const

hdu 2985 The k-th Largest Group 树状数组求第K大

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted: 2712 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g

【POJ2985】【Treap + 并查集】The k-th Largest Group

Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to group some of the cats. To do that, he first offers a number to each of the cat (1, 2, 3, …, n). Then he occ

Gym 101775A - Chat Group - [简单数学题][2017EC-Final]

题目链接:http://codeforces.com/gym/101775/problem/A It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: since every 3 or more persons could make a chat group, there can be 42 different chat groups. Given N

[poj2985]The k-th Largest Group[Treap]

1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #include <ctime> 8 9 using namespace std; 10 11 class Treap 12 { 13 private: 14 struc

POJ2985 The k-th Largest Group

题解: 并查集+树状数组+二分 注意将第k大数,转换为第tot+1-k小数, 就可以用用树状数组维护了 注意tot是动态的,在每次合并时都需要更新 代码: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<map> #include<set> using namespace std; using namespace st