LA 6450 social advertising(dfs剪枝)
6450 Social Advertising
You have decided to start up a new social networking company. Other existing popular social networks
already have billions of users, so the only way to compete with them is to include novel features no
other networks have.
Your company has decided to market to advertisers a cheaper way to charge for advertisements (ads).
The advertiser chooses which users’ “wall” the ads would appear on, and only those ads are charged.
When an ad is posted on a user’s wall, all of his/her friends (and of course the user himself/herself)
will see the ad. In this way, an advertiser only has to pay for a small number of ads to reach many
more users.
You would like to post ads to a particular group of users with the minimum cost. You already have
the “friends list” of each of these users, and you want to determine the smallest number of ads you have
to post in order to reach every user in this group. In this social network, if A is a friend of B, then B
is also a friend of A for any two users A and B.
Input
The input consists of multiple test cases. The first line of input is a single integer, not more than
10, indicating the number of test cases to follow. Each case starts with a line containing an integer n
(1 ≤ n ≤ 20) indicating the number of users in the group. For the next n lines, the ith line contains the
friend list of user i (users are labelled 1, . . . , n). Each line starts with an integer d (0 ≤ d < n) followed
by d labels of the friends. No user is a friend of himself/herself.
Output
For each case, display on a line the minimum number of ads needed to be placed in order for them to
reach the entire group of users.
Sample Input
2
5
4 2 3 4 5
4 1 3 4 5
4 1 2 4 5
4 1 2 3 5
4 1 2 3 4
5
2 4 5
2 3 5
1 2
2 1 5
3 1 2 4
Sample Output
1
2
题目大意:打广告搞宣传,有许多朋友关系,一个人做一下广告可以让他的n个朋友还有他自己看到。求找最少的做广告的人。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int maxn=;
int n,flag;
vector<int> f[maxn];
int vis[maxn];
int cnt[maxn]; bool is_ok(int s)
{
memset(vis,,sizeof(vis));
int i,j,sum=;
for(i=;i<s;i++)
{
if(!vis[cnt[i]])
{
vis[cnt[i]]=;sum++;
}
for(j=;j<f[cnt[i]].size();j++)
{
if(!vis[f[cnt[i]][j]])
{
vis[f[cnt[i]][j]]=;sum++;
}
}
}
if(sum==n) return ;
return ;
} void dfs(int now,int s,int dep)
{
if(now>n+) return ;
if(s==dep)
{
if(is_ok(s)) flag=;
return ;
}
cnt[s]=now;
dfs(now+,s+,dep);
dfs(now+,s,dep);
}
int main()
{
int t,i,k,p;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++) f[i].clear();
for(i=;i<=n;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&p);
f[i].push_back(p);f[p].push_back(i);
}
}
flag=;
for(i=;i<=n;i++)
{
dfs(,,i);
if(flag) break;
}
printf("%d\n",i);
}
return ;
}
LA 6450 social advertising(dfs剪枝)的更多相关文章
- LA 6450 Social Advertising
[题目] 给一个无向图,每当对某个点操作,该点以及与该点相连的点都获得标记,问标记所有点至少需要操作多少次 输入 第一行为T,表示测试数据组数 每组测试数据第一行为n(1<=n<=20)表 ...
- UVALive 6450 Social Advertising DFS解法
题意:一些人有朋友关系,在某个人的社交网站上投放广告可以被所有该人的直接朋友看到,问最小投放多少个广告使给出的人都看到广告.(n<=20) 解法:看到n的范围可以想到用二进制数表示每个人被覆盖与 ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- CPP-网络/通信:gsoap 的教程和使用
1.1.1 gSOAP 1.1.1 .1 简介 gSOAP 编译工具提供了一个 SOAP/XML 关于 C/C++ 语言的实现,从而让 C/C++ 语言研发 web 服务或客户端程式 ...
- PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%
大家都知道PayPal是另一家迁移到Node.js平台的大型公司,Jeff Harrell的这篇博文 Node.js at PayPal 解释了为什么从Java迁移出来的原因: 开发效率提高一倍(2 ...
- shell脚本,awk里面的BEGIN讲解。
解释: BEGIN{}这个特殊的pattern最常用的就是 变量赋值. BEGIN这个pattern就是文件没开始读的时候 执行 awk 'BEGIN{FS=":";OFS=&qu ...
- ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表
Spring整合Hibernate Spring的Web项目中,web.xml文件会自动加载,以出现欢迎首页.也可以在这个文件中对Spring的配置文件进行监听,自启动配置文件, 以及之前Struts ...
- k8s的资源限制及资源请求
容器的资源需求及限制: 需求:requests ##定义容器运行时至少需要资源 限制:limits ##定义容器运行时最多能分配的资源 requests:pod.spec.con ...
- Docker自学纪实(一)Docker介绍
先简单了解一下,做个记录,以便不时之需. Docker简介:Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依 ...
- 【Linux命令】nohup和&差异,查看进程和终止进程!
最近在开发dueros的技能,官方提供的PHPSDK中有多个实力,而运行实例的命令如下是 nohup php -S 0.0.0.0:8029 myindex.php & 从命令来看,肯定是在8 ...
- pandas按索引插入对应值的处理方法 - join
在工作中遇到一个问题即,实时的车辆数据中,需要将车辆的vid(一个Series)对应上其通用名称,以及车辆用途等信息进行统计. 正常的小规模操作是利用一个循环,查找vid 在另一张vid对应车辆名称用 ...
- SQL_1_简介
了解一门语言,还是应该从名字开始.SQL中的S即Structured(结构),L即Language(语言),Q即Query(查询),但不仅仅只是查询,还可以建立数据库,添加和删除数据,对数据作联合,当 ...
- 安装go 1.5 & 部署
https://storage.googleapis.com/golang/go1.5.linux-amd64.tar.gz tar -C /usr/local -xzf go1.5.linux-am ...