1076 Forwards on Weibo

25 篇文章 0 订阅
4 篇文章 0 订阅

这道题能看懂英文题意,就好办,联想到BFS
题目大意:给n个用户,最大层数L,然后依次给出n个用户关注(粉Follow)的用户编号, Follow是粉的意思,Followers是粉丝数量.这里有vector Follow来表示图结点
然后以待查寻编号为起始编号BFS进行L层的遍历count结点总数
代码如下

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <cstring>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <ctype.h>
using namespace std;
const int maxn = 1010;
struct node {
	int id, layer;
};
vector<node>  Follower[maxn];
int visit[maxn] = { 0 };
int BFS(int s, int L) {
	int numFollow = 0;
	node start;
	start.id = s;
	start.layer = 0;
	queue<node> q;
	q.push(start);
	visit[s] = 1;
	while (!q.empty()) {
		node top = q.front();
		q.pop();
		int u = top.id;
		for (int i = 0; i < Follower[u].size(); i++) {
			node next = Follower[u][i];
			next.layer = top.layer + 1;
			if (visit[next.id] == 0 && next.layer <= L) {
				q.push(next);
				visit[next.id] = 1;
				numFollow++;
			}
		}
	}
	return numFollow;
}
int main() {
	int n, L;
	scanf("%d%d", &n, &L);
	for (int i = 1; i <= n; i++) {
		node user;
		user.id = i;
		int k, tFollow;
		scanf("%d", &k);
		for (int j = 0; j < k; j++) {
			scanf("%d", &tFollow);
			Follower[tFollow].push_back(user);
		}
	}
	int querry, s;
	scanf("%d", &querry);
	for (int i = 0; i < querry; i++) {
		scanf("%d", &s);
		memset(visit, 0, sizeof(visit));
		int numFollow = BFS(s, L);
		printf("%d\n", numFollow);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值