[CF1105E] Helping Hiaset

Problem Description

You signed up for a new account at a social networking site above, this account has \ (n (n \ leq 10 ^ 5) \) times recorded. Or else you are changed once ID, or is an ID for the \ (s (| s | \ leq 40) \) friends visited your space.

Do you have \ (m (m \ leq 40 ) \) friends. Each friend will visit your space at least once. If this is your first visit a friend every space, your ID and it's the same ID, then he will happy. Ask how many people you can make the most happy.

Input Format

The first line of a two positive integers n, m represents the next n lines each time recording, the following two formats:

1

2 s

Where 1 means once you change the ID, 2 represents an ID for your friend s visit once your space.

To ensure that the first record must be 1.

Output Format

Line, a positive integer that indicates how many friends can make the most happy.

Sample input

5 3
1
2 motarack
2 mike
1
2 light

Sample Output

2

Resolve

If the time period of each person appearing recorded on the set time period has been the emergence of everyone. And if two people of the intersection of the set is the empty set, then the two men can be happy at the same time. In this way, the problem is transformed to people seeking to make the most of these people appear time set intersection is empty. The intersection is not empty of even two people side, it becomes a diagram largest independent set of problems.

Specifically with regard to the intersection of judgment, we can maintain a binary number for each person with a bitset, if two binary numbers of people and is not 0, it means there is common ground.

Code

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <bitset>
#include <algorithm>
#define N 100002
#define M 42
using namespace std;
int head[M],ver[M*M],nxt[M*M],l;
int n,m,i,j,cnt1,cnt2,ans,p[M];
bool vis[M];
map<string,int> d;
bitset<N> b[M];
void insert(int x,int y)
{
    l++;
    ver[l]=y;
    nxt[l]=head[x];
    head[x]=l;
}
int main()
{
    cin>>n>>m;
    for(i=1;i<=m;i++) p[i]=i;
    for(i=1;i<=n;i++){
        int op;
        string s;
        cin>>op;
        if(op==1) cnt1++;
        else{
            cin>>s;
            if(d[s]==0) d[s]=++cnt2;
            b[d[s]][cnt1]=1;
        }
    }
    for(i=1;i<=m;i++){
        for(j=i+1;j<=m;j++){
            if((b[i]&b[j]).any()) insert(i,j),insert(j,i);
        }
    }
    int t=100;
    while(t--){
        random_shuffle(p+1,p+m+1);
        int tmp=0;
        memset(vis,0,sizeof(vis));
        for(i=1;i<=m;i++){
            if(!vis[p[i]]){
                tmp++;
                for(j=head[p[i]];j;j=nxt[j]) vis[ver[j]]=1;
            }
        }
        ans=max(ans,tmp);
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/LSlzf/p/11878210.html