A1076. Forwards on Weibo (30)

 

Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=1000), the number of users; and L (<=6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:

M[i] user_list[i]

where M[i] (<=100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that are followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

Then finally a positive K is given, followed by K UserID's for query.

Output Specification:

For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can triger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.

Sample Input:

7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6

Sample Output:

4
5

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <string>
 8 #include <stack> 
 9 #include <queue>
10 #include <vector>
11 using namespace std;
12 const int maxn=1010;
13 int  n,maxlay; 
14 struct node{
15     int id;
16     int layer; 
17 };
18 bool inq[maxn]={false};
19 vector<node> adj[maxn];
20 
21 int BFS(int root,int lay)//宽度搜索 
22 {
23     int zhuanfa=0;
24     queue<node> q;
25     node start;
26     inq[root]=true;
27     start.id=root;
28     start.layer=0;
29     q.push(start);
30     while(!q.empty())
31     {
32         node topnode=q.front();
33         q.pop();
34         for(int i=0;i<adj[topnode.id].size();i++)
35         {
36             node next=adj[topnode.id][i];
37             next.layer=topnode.layer+1;
38             if(inq[next.id]==false&&next.layer<=maxlay)
39             {
40                 q.push(next);
41                 inq[next.id]=true;
42                 zhuanfa++;
43             }
44         }
45     }
46     return zhuanfa;
47 }
48 int main(){
49    node user; 
50    scanf("%d%d",&n,&maxlay);
51    for(int i=1;i<=n;i++)
52    {
53        int numf;
54        user.id=i;
55        scanf("%d",&numf);
56        for(int j=0;j<numf;j++)
57        {
58            int idf;
59            scanf("%d",&idf);//第i个节点关注的idf
60         adj[idf].push_back(user);           
61     }
62    }
63     int numq,s;
64     scanf("%d",&numq);
65     for(int i=0;i<numq;i++)
66     {
67         scanf("%d",&s);
68         memset(inq,false,sizeof(inq));
69         int numForward=BFS(s,maxlay);
70         printf("%d\n",numForward);
71     }
72    
73    
74     return 0;
75 }

 

posted @ 2015-03-10 01:23  Joilee  阅读(162)  评论(0编辑  收藏  举报