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 wowould 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 users
More users.
You wowould like to post ads to a participant 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 RST line of input is a single integer, not more
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
Friend list of user I (users are labeled 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
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
A company advertises advertisements. If a person advertises advertisements, all the friends connected to him can know the advertisements, asking all the people to know the advertisements and the minimum number of advertisements.
Therefore, we need to find the least vertex to overwrite all connected vertices.
Idea: Enumerate any combination. If there is only one person, can all be overwritten? If not, it will enumerate whether all two people can be overwritten, and so on;
When DFS arrives at the leaf node, it calculates whether to overwrite all data. This problem can easily time out. Later, let's look at how others wrote it. He used a binary enumeration to make the time more complex,
However, he does not use an adjacent table, but uses binary to create an edge, which is a bit 6.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn=25;int n,flag;vector<int> f[maxn];int vis[maxn];int cnt[maxn];bool is_ok(int s){ memset(vis,0,sizeof(vis)); int i,j,sum=0; for(i=0;i<s;i++) { if(!vis[cnt[i]]) { vis[cnt[i]]=1;sum++; } for(j=0;j<f[cnt[i]].size();j++) { if(!vis[f[cnt[i]][j]]) { vis[f[cnt[i]][j]]=1;sum++; } } } if(sum==n) return 1; return 0;}void dfs(int now,int s,int dep){ if(now>n+1) return ; if(s==dep) { if(is_ok(s)) flag=1; return ; } cnt[s]=now; dfs(now+1,s+1,dep); dfs(now+1,s,dep);}int main(){ int t,i,k,p; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) f[i].clear(); for(i=1;i<=n;i++) { scanf("%d",&k); while(k--) { scanf("%d",&p); f[i].push_back(p);f[p].push_back(i); } } flag=0; for(i=1;i<=n;i++) { dfs(1,0,i); if(flag) break; } printf("%d\n",i); } return 0;}
His Edge building code is improved, with less time of 100 + MS
Improved code:
# Include <iostream> # include <cstdio> # include <cstring> # include <vector> using namespace STD; const int maxn = 25; int N, flag; int vis [maxn]; int CNT [maxn]; int eg [maxn]; bool is_ OK (INT s) {int ans = 0; For (INT I = 0; I <s; I ++) ans | = eg [CNT [I]; If (ANS = (1 <n)-1) return true; // return false is accessed for all vertices;} void DFS (INT now, int S, int dep) {If (now> n + 1) return; if (S = dep) {If (is_ OK (s) Flag = 1; return;} CNT [s] = now; DFS (now + 1, S + 1, dep); DFS (now + 1, S, DEP);} int main () {int T, I, K, P; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); memset (eg, 0, sizeof (EG); for (I = 1; I <= N; I ++) {scanf ("% d", & K); eg [I] | = 1 <(I-1); While (k --) {scanf ("% d", & P); eg [I] | = 1 <(p-1 ); // indicates that the I and P sides have connected edges} flag = 0; for (I = 1; I <= N; I ++) {DFS (1, 0, i); If (FLAG) break;} printf ("% d \ n", I);} return 0 ;} /* 254 2 3 4 54 1 4 54 1 2 4 54 1 3 54 1 2 3 452 4 52 3 51 22 1 53 1 2 4 */