AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 1562. 微博转发(每日一题) 0.02 AC币

作者: 作者的头像   卷死你们QAQ ,  2023-03-12 20:35:54 ,  所有人可见 ,  阅读 5


0


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1010, M = 100010;
int idx,h[N],e[M], ne[M];
int n,l;
int cnt;
int x;
bool st[N];

void add(int a,int b)
{
    ne[idx] = h[a];
    e[idx] = b;
    h[a] = idx ++;
}

int bfs(int start)
{
    queue<int> q;
    memset(st,0,sizeof st);
    q.push(start);
    st[start] = true;
    int res = 0;

    for(int i=0;i<l;i++)
    {
        int sz = q.size();
        while(sz --)
        {
            int t = q.front();
            q.pop();
            for(int j=h[t];j != -1;j=ne[j])
            {
                int u = e[j];
                if(!st[u])
                {
                    q.push(u);
                    st[u] = true;
                    res ++;
                }
            }
        }
    }

    return res;
}

int main()
{
    cin>>n>>l;
    memset(h,-1,sizeof h);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&cnt);
        while(cnt --)
        {
            scanf("%d",&x);
            add(x,i);
        }
    }

    int k;
    cin>>k;
    while(k --)
    {
        scanf("%d",&x);
        cout<<bfs(x)<<endl;
    }

    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息