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) ...
随机推荐
- 《剑指offer》51:数组中的逆序对
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- c++作业:求N的阶乘。
N的阶乘就是n.(n-1)! 5的阶乘是什么?5*4*3*2*1 #include <iostream> using namespace std; int jiecheng(int num ...
- cocos2dx for android 接入 fmod的过程
cocos2dx自带的音效播放有SimpleAudioEngine和AudioEngine两个,SimpleAudioEngine可以播放简单的音效, 如果播放音效数量过多的话,多导致有些音效播放失败 ...
- NOIP模拟赛 czy的后宫4
czy的后宫4 [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到- ...
- SwaggerUI日常使用
最近公司项目集成springFox,记录一些swaggerUI日常使用,包括数组,文件,默认值,允许值,参数/结果类注解,响应码..等等. 一.参数注解: 单参数:@ApiImplicitParam ...
- re--findall 【转】
原文链接 python re 模块 findall 函数用法简述 代码示例: >>> import re >>> s = "adfad asdfasdf ...
- 将系统从.Net Core2.0升级到.Net Core2.1
最近将手头的一个.Net Core2.0开发的小系统升级到最新的Core2.1.升级期间遇到了一些问题,现将问题以及解决方法整理汇总一下. 一是作为笔记,二也为跟各位分享一下.如过能帮到看到这帖子的人 ...
- VS2017生成.net core项目报错:The current .NET SDK does not support targeting .NET Core 2.1. Either
今天在生成一个项目的时候,生成报错,错误如下:The current .NET SDK does not support targeting .NET Core 2.1. Either target ...
- Java学习——面对对象的思想入门
本文是看过<head first Java>之后的一点感悟,写点东西帮忙以后回忆,Java目前在我的工作中用到还不多,而我又对面对对象的编程非常的感兴趣.曾经在MFC平台上写过 ...
- KVO And KVC
http://www.cocoachina.com/industry/20140224/7866.html