1076 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 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 trigger, 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

题目描述:微博吧啦吧啦。。现在!给你一个社会网图,你需要求出最大潜在的转发数时多少,假设只能传到L层关系。


输入:第一行两个正整数N(<=1000)用户数,L(<=6)转发层。接下来有N行,每行代表第i个用户关注的人(i=1~N),每行格式为M[i] user_list[i],user_list时用户的编号。最后一行,第一个数K是带查询个数,接下来就是K个值啦。比如样例中最后一行K=2,带查询的第一个值是2。分析下样例,先把图画出来,当2发出一篇微博时,他最大能被转发的次数是多少?显然是4,他能被(1、4、5、6)转发。

输出:转发的次数


解题思路:要查询那个点,就从哪个点开始广搜遍历就行了。

注意:很简单的一道BFS题,不知道为什么会出现在30分这里。难道是因为最后一个测试点卡着了?最后一个测试点容易超时。


#include<bits/stdc++.h>
using namespace std;
int n, l;
const int maxn = 1000+5;
vector<int> G[maxn];
bool vis[maxn];
struct node{
	int index, level;
	node(){
		index = level = 0;
	}
};
void query(int start){
	fill(vis,vis+maxn,false);
	node t, tt;
	t.index = start;
	t.level = 0;
	queue<node> q;
	q.push(t);
	vis[start] = true;
	int ans = 0;
	while(!q.empty()){
		t = q.front(); q.pop();
		for(int i=0; i<G[t.index].size(); ++i){
			int u = G[t.index][i];
			if(!vis[u]&&t.level<l){
				ans++;
				vis[u] = true;
				tt.index = u;
				tt.level = t.level+1;
				q.push(tt);
			}
		}
	}
	printf("%d\n", ans);
}
int main()
{
	int k, g;
	scanf("%d %d", &n, &l);
	for(int i=1; i<=n; ++i){
		scanf("%d", &k);
		for(int j=0; j<k; ++j){
			scanf("%d", &g);
			G[g].push_back(i);
		}
	}
	scanf("%d", &k);
	for(int i=0; i<k; ++i){
		scanf("%d", &g);
		query(g);
	}
	return 0;
}