CodeForces 1105E Helping Hiasat 最大独立集

Helping Hiasat  

题解:

如果我们把连续的2出现的人都相互连边的话, 题目就是问最大独立集的答案是多少。

求最大独立集可以将图变成反图, 然后求最大团。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 2e5 + ;
int e[][];
int cnt[N], group[N], sta[N], ans;
int n;
bool dfs(int u, int deep){
for(int i = u + ; i <= n; ++i){
if(cnt[i] + deep <= ans) return ;
if(e[u][i]){
int j = ;
for(; j < deep; ++j) if(!e[i][sta[j]]) break;
if(j == deep){
sta[deep] = i;
if(dfs(i, deep+)) return ;
}
}
}
if(deep > ans){
for(int i = ; i < deep; ++i) group[i] = sta[i];
ans = deep;
return ;
}
return ;
}
void maxclique(){
ans = -;
for(int i = n; i; --i){
sta[] = i;
dfs(i, );
cnt[i] = ans;
}
}
map<string,int> mp;
int main(){
int m, cnt = ;
scanf("%d%d", &m, &n);
LL x = ;
int op;
for(int i = ;i <= m; ++i){
scanf("%d", &op);
if(op == ){
for(int j = ; j <= n;++j)
for(int k = ;k <= n; ++k)
if(((x>>j)&) && ((x>>k)&))
e[j][k] = e[k][j] = ;
x = ;
}
else{
string s;
cin >> s;
if(!mp.count(s)) mp[s] = ++cnt;
x |= (1ll << mp[s]);
}
}
for(int j = ; j <= n;++j)
for(int k = ;k <= n; ++k)
if(((x>>j)&) && ((x>>k)&))
e[j][k] = e[k][j] = ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
e[i][j] ^= ;
// cout << "?" << endl;
maxclique();
printf("%d\n",ans);
return ;
}
上一篇:CF1105E Helping Hiasat 最大团


下一篇:Codeforces Round #533 (Div. 2) Solution