知识点

最大独立集(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. java.lang.NoSuchMethodError: org.hibernate.cfg.Environment.verifyProperties

    我在使用jpa2+spring4+hibernate4 的时候,报错java.lang.NoSuchMethodError: org.hibernate.cfg.Environment.verifyP ...

  2. Tcl介绍和基础语法

    Tcl的背景 Tcl(读作tickle)诞生于80年代的加州大学伯克利分校,作为一种简单高效可移植性好的脚本语言,目前已经广泛应用在几乎所有的EDA工具中.Tcl 的最大特点就是其语法格式极其简单,采 ...

  3. iOS 如何使用TabbarController

    xcode中给我内置很多app模版,不过很多时候我们需要更加灵活的初始化项目.下面我就简单介绍一下,如何从0开始制作一个tabbar app. 创建个项目,由于我们从头开始写程序,因此理论上对模版没有 ...

  4. DFS、BFS和Backtracking模板

    区别与联系 区别 DFS多用于连通性问题因为其运行思想与人脑的思维很相似,故解决连通性问题更自然,采用递归,编写简便(但我个人不这样觉得...) DFS的常数时间开销会较少.所以对于一些能用DFS就能 ...

  5. 实训day02 python

    一.数据类型 列表: 定义:在[]内,可以存放多个任意类型的值,并以逗号隔开: 一般用于存放学生的爱好,课堂的周期等. 定义一个学生列表,可存放多个学生 students = ['A','B','C' ...

  6. C-基础:memcpy、memset、memmove、memcmp、memchr

    一,原型 void * memcpy ( void * destination, const void * source, size_t num ); 功能:将以source作为起始地址的数据复制nu ...

  7. 1-jdk的安装与配置

    1- Jvm.jdk.jre之间的关系 JVM:Java虚拟机,保证java程序跨平台.(Java Virtual Machine) JRE: Java运行环境,包含JVM和核心类库.如果只是想运行j ...

  8. linux 搜索文本

    find -type f -name '*.php'|xargs grep '127.0.0.1'  搜索所有.php 内容 127.0.0.1 转自:http://www.cnblogs.com/w ...

  9. day09 10 11 12 三天函数内容

    小括号.中括号名字()函数调用符[] 索引调用符 函数的注释:官方推荐: 查看注释 :funcming.__doc__      funcming.__name__ def func(name, ag ...

  10. select onchange事件的使用

    <select name="expireDay" id="expireDay" class="form-control" onchan ...