知识点

最大独立集(set) = 补图的最大团(clique )

最小顶点覆盖 + 最大独立集 = V

E. Helping Hiasat
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Hiasat registered a new account in NeckoForces and when his friends found out about that, each one of them asked to use his name as Hiasat's handle.

Luckily for Hiasat, he can change his handle in some points in time. Also he knows the exact moments friends will visit his profile page. Formally, you are given a sequence of events of two types:

  • 11 — Hiasat can change his handle.
  • 22 ss — friend ss visits Hiasat's profile.

The friend ss will be happy, if each time he visits Hiasat's profile his handle would be ss.

Hiasat asks you to help him, find the maximum possible number of happy friends he can get.

Input

The first line contains two integers nn and mm (1≤n≤105,1≤m≤401≤n≤105,1≤m≤40) — the number of events and the number of friends.

Then nn lines follow, each denoting an event of one of two types:

  • 11 — Hiasat can change his handle.
  • 22 ss — friend ss (1≤|s|≤401≤|s|≤40) visits Hiasat's profile.

It's guaranteed, that each friend's name consists only of lowercase Latin letters.

It's guaranteed, that the first event is always of the first type and each friend will visit Hiasat's profile at least once.

Output

Print a single integer — the maximum number of happy friends.

Examples
input

Copy
5 3
1
2 motarack
2 mike
1
2 light
output

Copy
2
input

Copy
4 3
1
2 alice
2 bob
2 tanyaromanova
output

Copy
1
Note

In the first example, the best way is to change the handle to the "motarack" in the first event and to the "light" in the fourth event. This way, "motarack" and "light" will be happy, but "mike" will not.

In the second example, you can choose either "alice", "bob" or "tanyaromanova" and only that friend will be happy.

题意  m个朋友 n个操作

两种操作

1 Hiasat 可以修自己的id为任意值

2 Hiasat 得一个朋友查看id

当且仅当 他的朋友每次查看id都为自己的名字才是高兴的

解析    很容易想到两个1之间的朋友是互斥的 只能使其中一名朋友高兴,我们在互斥的朋友之间连一条无向边,从而转化成求无向图的最大独立集问题。

AC代码

 #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
const int maxn = 1e5+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
//head------------------------------------------------------------------
int G[][];
int ans,cnt[],group[],n,m,vis[];//ans表示最大团,cnt[N]表示当前最大团的节点数,group[N]用以寻找一个最大团集合
map<string,int> pm;
int tot = ;
struct node
{
int op;
string s;
} Q[maxn];
vector<int> v;
bool dfs(int u,int pos)//u为当从前顶点开始深搜,pos为深搜深度(即当前深搜树所在第几层的位置)
{
int i,j;
for(i=u+; i<=tot; i++) //按递增顺序枚举顶点
{
if(cnt[i]+pos<=ans)
return ;//剪枝
if(G[u][i])
{
// 与目前团中元素比较,取 Non-N(i)
for(j=; j<pos; j++)
if(!G[i][vis[j]])
break;
if(j==pos)
{
// 若为空,则皆与 i 相邻,则此时将i加入到 最大团中
vis[pos]=i;//深搜层次也就是最大团的顶点数目,vis[pos] = i表示当前第pos小的最大团元素为i(因为是按增顺序枚举顶点 )
if(dfs(i,pos+))
return ;
}
}
}
if(pos>ans)
{
for(i=; i<pos; i++)
group[i] = vis[i]; // 更新最大团元素
ans = pos;
return ;
}
return ;
}
void maxclique()//求最大团
{
ans=-;
for(int i=tot; i>; i--)
{
vis[]=i;
dfs(i,);
cnt[i]=ans;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
{
int op;
string tmp;
scanf("%d",&op);
if(op==)
Q[i].op = op;
else
{
cin>>tmp;
Q[i].op = op;
Q[i].s = tmp;
if(!pm[tmp])
pm[tmp] = ++tot;
}
}
memset(G,inf,sizeof(G));
Q[n+].op=;
for(int i=; i<=n+; i++)
{
if(Q[i].op==)
{
for(int j=; j<v.size(); j++)
{
for(int k=j+; k<v.size(); k++)
{
G[v[k]][v[j]] = ;
G[v[j]][v[k]] = ;
}
}
v.clear();
}
else
{
v.push_back(pm[Q[i].s]);
}
}
maxclique();
if(ans<)
ans = ;
printf("%d\n",ans);
return ;
}

Codeforces Round #533 (Div. 2) E 最大独立集的更多相关文章

  1. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  2. Codeforces Round #533 (Div. 2) E. Helping Hiasat(最大独立集)

    题目链接:https://codeforces.com/contest/1105/problem/E 题意:有 n 个事件,op = 1 表示我可以修改昵称,op = 2 表示一个名为 s_i 的朋友 ...

  3. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  4. Codeforces Round #533 (Div. 2)

    C: 题意: 有n个整数ai,数列a有两个神奇的性质.1.所有的整数都在[l,r]范围内.2.这n个数的和能被3整除.现在给出l和r,和个数n,问你有多少种方法构造出数列a,方案数mod1e9+7. ...

  5. Codeforces Round #533 (Div. 2) Solution

    A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...

  6. Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】

    传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...

  7. Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】

    传送门:http://codeforces.com/contest/1105/problem/B B. Zuhair and Strings time limit per test 1 second ...

  8. Codeforces Round #533(Div. 2) C.Ayoub and Lost Array

    链接:https://codeforces.com/contest/1105/problem/C 题意: 给n,l,r. 一个n长的数组每个位置可以填区间l-r的值. 有多少种填法,使得数组每个位置相 ...

  9. Codeforces Round #533(Div. 2) D.Kilani and the Game

    链接:https://codeforces.com/contest/1105/problem/D 题意: 给n*m的地图,最多9个人,同时有每个人的扩张次数(我开始以为是直线扩张最大长度..实际是能连 ...

随机推荐

  1. 用Python爬取智联招聘信息做职业规划

    上学期在实验室发表时写了一个爬取智联招牌信息的爬虫. 操作流程大致分为:信息爬取——数据结构化——存入数据库——所需技能等分词统计——数据可视化 1.数据爬取 job = "通信工程师&qu ...

  2. TebsorFlow低阶API(五)—— 保存和恢复

    简介 tf.train.Saver 类提供了保存和恢复模型的方法.通过 tf.saved_model.simple_save 函数可以轻松地保存适合投入使用的模型.Estimator会自动保存和恢复 ...

  3. 联玛客(W 笔试)

    纸质算法题 1. 输入数据:1.3.2.4.8... 输出数据:3.1.4.2.8... 找出规律,写出一个程序求解,并附上时间复杂度和空间复杂度 我的答案: 规律一:奇偶位互换 假设输入数据长度为5 ...

  4. render_to_response()

    render_to_response('模板名称',字典) 字典:第二个参数必须是为该模板创建context时所使用的字典,如果不提供第二个参数,render_response()使用一个空字典

  5. PHP11 日期和时间

    学习要点 UNIX时间戳 将其他格式的日期转成UNIX时间戳格式 基于UNIX时间戳的日期计算 获取并格式化输出日期 修改PHP的默认时间 微秒的使用    Unix时间戳 相关概念 Unix tim ...

  6. Navicat 模型生成表

    打开模型 -> 左上角文件 -> 导出SQL 打开sql文件,将sql在数据库执行,注意主键递增.日期类型 根据当前时间戳更新是否需要(默认选中的)等等

  7. 普通平衡树(treap)

    题干:6种操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相同的数,因输出最小的排名) 4. 查询排名为x的数 5. 求x的前驱(前驱定义为小于 ...

  8. phpstorm的全局操作快捷键ctrl+shift+f被搜狗占用处理方法

    1.找到搜狗软件,右键选择属性设置 2.如图选择系统功能快捷键 3.去掉简繁切换快捷方式,确定后即可使用phpstorm的ctrl+shift+f来进行全局查找

  9. cetos7 安装telnet

    先执行命令 rpm -qa | grep telnet rpm -qa | grep telnet-server rpm -qa | grep xinetd 如果返回空白就是没有安装 yum inst ...

  10. Django 动态建表

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Random_lee # -*- coding: utf-8 -*- from django ...