传送门
题目描述
分析
最大独立集
首先相邻两个1之间的所有人最多只能选一个,所以我们可以把人名转换成数字,然后在每两个1之间的所有人之间连边
然后求补图的最大团即可
(一开始以为是个二分图跑匈牙利,后来被提醒这并不是一个二分图)
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 110;
int g[N][N];
int ans,cnt[N],n,m,vis[N];
map<string,int> M;
int ppp;
bool dfs(int u,int dep){
for(int i = u + 1;i <= n;i++){
int flag = 1;
if(cnt[i] + dep <= ans) return 0;
if(g[u][i]){
for(int j = 0;j < dep;j++)
if(!g[i][vis[j]]){
flag = 0;
break;
}
if(flag){
vis[dep] = i;
if(dfs(i,dep + 1)) return 1;
}
}
}
if(dep > ans){
ans = dep;
return 1;
}
return 0;
}
void run(){
ans = -1;
for(int i = n;i;i--){
vis[0] = i;
dfs(i,1);
cnt[i] = ans;
}
}
int main(){
scanf("%d%d",&m,&n);
vector<int> Q;
memset(g,1,sizeof g);
while(m--){
int x;
scanf("%d",&x);
if(x == 1) Q.clear();
else{
string str;
cin >> str;
if(!M[str]) M[str] = ++ppp;
for(int i = 0;i < Q.size();i++)
g[Q[i]][M[str]] = g[M[str]][Q[i]] = 0;
Q.push_back(M[str]);
}
}
run();
printf("%d\n",ans);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/