C 带有计数的BFS 数组存储图 1076 Forwards on Weibo (30分)

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

题目意思
给出N,K;
代表有n个用户,要找k层内的潜在followers;
下面n行为每个用户follow的人的个数和序号;
最后一行为要计算潜在总followers的人的个数,和他们的序号;

解题方法
重点在于计算层数;
链表存储图可以更节省空间,不过代码量更大;
这里选用数组存储图;

计层数的方法
用last存放还未出队的最小层数的最后一个值;
tail存放入队的元素;
若last已出队,说明该层以及全部出队,则让tail赋值给last;
此时tail为下一层最后一个入队的元素;

/*
给定一个关系网络
一个人发微博,推荐给多少人 
L层的间接跟随这会被计算 

N 人数; <=1000
L 层数; <=6 

user1: follow的人数n  n个人
user2: follow的人数n  n个人 
N行

最后一行读入k  要被查询的K个人 
*/

//六度空间理论
//BFS六次 count记录遍历到的数
#include<iostream> 
#include<queue>
using namespace std;
#define MAX 1001
//二维数组存放
int Followers[MAX][MAX]={0};
int visited[MAX]; 

int N,F; //N个人,F层
//F+1层直接 

void BFS(int a)
{
	for(int i=0;i<=N;i++) visited[i]=0;
	queue<int> T;
	T.push(a);
	visited[a]=1;
	int last=a;
	int tail;
	int floor=0;
	int count=0;     //计数,自己不算
	
	while(!T.empty()) 
	{
		int temp = T.front();
		T.pop();
		
		for(int i=1;i<=N;i++)
		{
			if(!visited[i]&&Followers[temp][i]==1)  //有边且未访问过 
			{
				T.push(i);
				visited[i]=1;
				tail=i;
				++count; 
			}
		}
		if(temp==last){
			last=tail;
			++floor;
		}
		if(floor==F) break;
	}
	cout<<count<<endl; 
 } 

int main()
{
	cin>>N>>F;   //读入N个人,最大K层
	
	for(int i=1;i<=N;i++)
	{
		int n;
		int temp;
		cin>>n;
		if(n==0) continue;   //没有follower就继续 
		for(int j=0;j<n;j++){
			cin>>temp;
			Followers[temp][i]=1;
		}
	 } 
	//每个边都存放在列表里
	 int k;
	 cin>>k;
	 for(int i=0;i<k;i++)
	 {	
	 	int tmp;
	 	cin>>tmp;
	 	BFS(tmp);
	 }
}
 
发布了77 篇原创文章 · 获赞 3 · 访问量 3020

猜你喜欢

转载自blog.csdn.net/BLUEsang/article/details/105408624