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个朋友还有他自己看到。求找最少的做广告的人。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6
 7 const int maxn=25;
 8 int n,flag;
 9 vector<int> f[maxn];
10 int vis[maxn];
11 int cnt[maxn];
12
13 bool is_ok(int s)
14 {
15     memset(vis,0,sizeof(vis));
16     int i,j,sum=0;
17     for(i=0;i<s;i++)
18     {
19         if(!vis[cnt[i]])
20         {
21             vis[cnt[i]]=1;sum++;
22         }
23         for(j=0;j<f[cnt[i]].size();j++)
24         {
25             if(!vis[f[cnt[i]][j]])
26             {
27                 vis[f[cnt[i]][j]]=1;sum++;
28             }
29         }
30     }
31     if(sum==n) return 1;
32     return 0;
33 }
34
35 void dfs(int now,int s,int dep)
36 {
37     if(now>n+1) return ;
38     if(s==dep)
39     {
40         if(is_ok(s)) flag=1;
41         return ;
42     }
43     cnt[s]=now;
44     dfs(now+1,s+1,dep);
45     dfs(now+1,s,dep);
46 }
47 int main()
48 {
49     int t,i,k,p;
50     scanf("%d",&t);
51     while(t--)
52     {
53         scanf("%d",&n);
54         for(i=1;i<=n;i++) f[i].clear();
55         for(i=1;i<=n;i++)
56         {
57             scanf("%d",&k);
58             while(k--)
59             {
60                 scanf("%d",&p);
61                 f[i].push_back(p);f[p].push_back(i);
62             }
63         }
64         flag=0;
65         for(i=1;i<=n;i++)
66         {
67             dfs(1,0,i);
68             if(flag) break;
69         }
70         printf("%d\n",i);
71     }
72     return 0;
73 }

LA 6450 social advertising(dfs剪枝)

时间: 2024-10-09 09:26:23

LA 6450 social advertising(dfs剪枝)的相关文章

Uva LA6450 Social Advertising DFS

You have decided to start up a new social networking company. Other existing popular social networksalready have billions of users, so the only way to compete with them is to include novel features noother networks have.Your company has decided to ma

UVALIVE 6450 Social Advertising

You have decided to start up a new social networking company. Other existing popular social networksalready have billions of users, so the only way to compete with them is to include novel features noother networks have.Your company has decided to ma

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

Cubes(DFS+剪枝)

题意:给一个数N,求N最少由多少个数的立方构成,并输出这些数. 做法:DFS + 剪枝,剪枝的边界很很很重要! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> int cub[400]; int ans[300]; int tp[300]; int n; int sum = 0x3f3f3f3f; //个数 void dfs(int le

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

EOJ1981 || POJ1011 经典dfs+剪枝+奇怪的数据

题目:EOJ1981 || POJ1011   经典dfs+剪枝+奇怪的数据 Description George took sticks of the same length and cut them randomly until all partsbecame at most 50 units long. Now he wants to return sticks to the originalstate, but he forgot how many sticks he had origi

HDOJ 5113 Black And White DFS+剪枝

DFS+剪枝... 在每次DFS前,当前棋盘的格子数量的一半小于一种颜色的数量时就剪掉 Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 50 Special Judge Problem Description In mathematics,