1. Title description
原题链接
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:
Output Specification:
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
Two, problem-solving ideas
Given a starting point, the layer sequence of the graph traverses the problem. Note that the data given here is a user who follows other users, not people who follow this user, so pay attention to it when creating adjacency lists. For each input query, we perform a layer sequence traversal (implemented by a queue), and store the number of layers corresponding to each user in the level array. After it is greater than the L required by the question, break.
Three, AC code
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1010;
vector<vector<int> > Adj;
int main()
{
int N, L, num, tmp, query;
scanf("%d%d", &N, &L);
Adj.resize(N+1);
for(int i=1; i<=N; i++)
{
scanf("%d", &num);
for(int j=0; j<num; j++)
{
scanf("%d", &tmp);
Adj[tmp].push_back(i);
}
}
scanf("%d", &num);
for(int i=0; i<num; i++)
{
int cnt = 0, level[N+1];
queue<int> q;
bool inq[maxn] = {
false};
scanf("%d", &query);
inq[query] = true;
q.push(query);
level[query] = 0;
while(!q.empty())
{
tmp = q.front();
q.pop();
if(level[tmp] >= L) break;
for(int i=0; i<Adj[tmp].size(); i++)
{
if(!inq[Adj[tmp][i]])
{
q.push(Adj[tmp][i]);
inq[Adj[tmp][i]] = true;
level[Adj[tmp][i]] = level[tmp] + 1;
cnt++;
}
}
}
printf("%d\n", cnt);
}
return 0;
}