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;
}


贴一个新的写法,不过思路是一样的
/*****************************************************************
    > File Name: tmp.cpp
    > Author: Uncle_Sugar
    > Mail: uncle_sugar@qq.com
    > Created Time: 2016年03月22日 星期二 00时16分10秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

const int debug = 1;
const int size  = 1000 + 10; 
const int INF = INT_MAX>>1;
typedef long long ll;

vector<int>	vct[size];
int vis[size];
int main()
{
	std::ios::sync_with_stdio(false);cin.tie(0);
	int i,j;
	int n,l,k;
	cin >> n >> l;
	for (i=1;i<=n;i++){
		int sum,tmp;
		cin >> sum;
		while (sum--){
			cin >> tmp;
			vct[tmp].push_back(i);
		}
	}
	queue<int> que;
	int lcnt,scnt,ans;
	cin >> k;
	while (k--){
		memset(vis,0,sizeof(vis));
		while (!que.empty())	que.pop();
		int tmp;
		cin >> tmp;
		que.push(tmp);vis[tmp] = 1;
		lcnt = 0;scnt = que.size();
		ans = 0;
		while (!que.empty()){
			int T = que.front();que.pop();
			ans++;
			for (i=0;i<vct[T].size();i++){
				if (vis[vct[T][i]]==0){
					que.push(vct[T][i]);
					vis[vct[T][i]] = 1;
				}
			}
			if (--scnt==0){
				scnt = que.size();
				lcnt++;
			}
			if (lcnt>l)	break;
		}
		cout << ans-1 << '\n';
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值