PAT (Advanced Level) Practice - 1076 Forwards on Weibo(30 分)

简介: PAT (Advanced Level) Practice - 1076 Forwards on Weibo(30 分)

题目链接:点击打开链接

题目大意:给出每个用户关注的人的 id 和转发最多的层数,求一个 id 发了条微博最多会有多少个人转发?

解题思路:带层数的广度优先,因为一个用户只能转发一次,所以用 vis 判断当前结点是否入队过了,如果入队过了就不能重复入队(重复转发消息),控制不超过 L 层。

AC 代码

1.#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
const int maxn=1010;
struct node
{
    int id,lvl;
    node(){}
    node(int id,int lvl):id(id),lvl(lvl){}
};
int n,l;
vector<int> v[maxn];
int bfs(int id)
{
    int cnt=0, vis[maxn]={0};
    vis[id]=1;
    queue<node> q;
    q.push(node(id,0));
    node nd;
    while(!q.empty())
    {
        nd=q.front(); q.pop();
        int tid=nd.id, lvl=nd.lvl;
        for(int i=0;i<v[tid].size();i++)
        {
            int newid=v[tid][i];
            if(!vis[newid] && lvl<l)
            {
                q.push(node(newid,lvl+1));
                vis[newid]=1;
                cnt++;
            }
        }
    }
    return cnt;
}
int main()
{
    int m,tid;
    scanf("%d%d",&n,&l);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&tid);
            v[tid].push_back(i);
        }
    }
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&tid);
        printf("%d\n",bfs(tid));
    }
    return 0;
}
目录
相关文章
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
63 0
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
66 0
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
99 0
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
82 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
100 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
113 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
125 0
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
100 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
118 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
110 0

热门文章

最新文章