A1076. Forwards on Weibo

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<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 using namespace std;
 6 typedef struct NODE{
 7     int data, layer;
 8 }node;
 9 vector<node> G[1001];
10 int visit[1001];
11 int N, L, K, Mi;
12 int bfs(int vt){
13     int cnt = -1;
14     node first = {vt, 0};
15     visit[vt] = 1;
16     queue<node> Q;
17     Q.push(first);
18     while(Q.empty() == false){
19         node temp = Q.front();
20         Q.pop();
21         cnt++;
22         int len = G[temp.data].size();
23         if(temp.layer < L){
24             for(int i = 0; i < len; i++){
25                 if(visit[G[temp.data][i].data] == 0){
26                     visit[G[temp.data][i].data] = 1;
27                     G[temp.data][i].layer = temp.layer + 1;
28                     Q.push(G[temp.data][i]);
29                 }
30             }
31         }
32     }
33     return cnt;
34 }
35 int main(){
36     scanf("%d%d", &N, &L);
37     for(int i = 1; i <= N; i++){
38         scanf("%d", &Mi);
39         for(int j = 0; j < Mi; j++){
40             int temp;
41             scanf("%d", &temp);
42             node nd = {i, 0};
43             G[temp].push_back(nd);
44         }
45     }
46     scanf("%d", &K);
47     int vt, ans;
48     for(int i = 0; i < K; i++){
49         for(int j = 0; j < 1001; j++)
50             visit[j] = 0;
51         scanf("%d", &vt);
52         ans = bfs(vt);
53         printf("%d
", ans);
54     }
55     cin >> N;
56     return 0;
57 }
View Code

总结:

1、题意:求据距离点A的距离不超过L的节点的个数(A本身不计入)。

2、bfs的visit数组记录的是是否曾加入过队列,而非是否被访问。

3、本题是有向图,最好用邻接表存储。此外,题目给出的数据是A关注的人的列表,而微博消息应该是从关注列表里的人到A的方向,注意不要弄错图的边。

4、由于dfs的原则是访问过的节点就不再访问,所以求得的节点有可能不是最短的转发距离,从而有可能漏掉一些情况。如图,当使用深搜,L = 2。从A开始,A->B->C,距离达到了2开始回溯到A,而由于C已被visit标记,所以无法再次被访问,D距离A也是2但却被遗漏。

                                                                            

原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8548445.html