1076. Forwards on Weibo (30)限定层数的广搜 or 最短路

1076. Forwards on Weibo (30)

时间限制
3000 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
    这道题怎么说呢,其实姥姥的数据结构上有一道 六度空间的题目,比这个难多了,微博的传播方式很容易让人想到广度优先搜索,其实抽象一下,这道题目的意思就是广度优先搜索k轮一共能获得多少个节点,关键是如何确定层数,在这里我没有太纠结效率,用了两个queue<int>,搞定了,其实将两个queue<int>换成指针,将最后的交换通过指针之间的交换,是可以省下一大笔时间的。
    然后,我绝对不会告诉你,这道题直接使用floyd算法然后统计所有最短路在限定层数以内的点,写起来更快。
# include <cstdio>
# include <queue>
# include <cstring>
using namespace std;


const int debug = 1;
struct edge
{
    int to;
    edge* next;
    edge(int _to):to(_to),next(NULL){}
}; 
struct vertex
{
	int cnt;
    edge *next,*last;
    vertex():cnt(-1),next(NULL),last(NULL){}
    void Attach(int _to)
	{
	    if (next==NULL)
	        next = last = new edge(_to);
        else 
            last = last->next = new edge(_to);
 	}
};
vertex person[1005];
int bfs(int root,int limit)
{
	static int vis[1005];
    if (person[root].cnt!=-1)
        return person[root].cnt;
    memset(vis,0,sizeof(vis));
    queue<int> que;int cnt = 0,level = 0;
    que.push(root);
	vis[root] = 1;
    while (level<limit)
    {
	    queue<int> temp;
    	while (!que.empty())
	    {
		  int loca = que.front();que.pop();
	      edge* next = person[loca].next;
	      while (next)
		  {
		      if (vis[next->to]==0)
	          {
			    temp.push(next->to);
			    vis[next->to] = 1;
			    temp.push(next->to);
			    cnt++;
			  }
			  next = next->next;
    	   } 
	    }
		que = temp;
		level++;
	}
	return person[root].cnt = cnt;
}
int main()
{
	int i,j,k,tmp;
    int n,l;
    scanf("%d%d",&n,&l);
    for (i=1;i<=n;i++)
    {
	     scanf("%d",&k);
		 while (k--)
		 {
		     scanf("%d",&tmp);
		     person[tmp].Attach(i);
		 }   
	}
	scanf("%d",&k);
	while (k--)
	{
		scanf("%d",&tmp);
	    printf("%d\n",bfs(tmp,l));
	}
    return 0;
}

1076. Forwards on Weibo (30)限定层数的广搜 or 最短路_第1张图片

你可能感兴趣的:(队列,图,广度优先)