1076 Forwards on Weibo (30 分) (BFS)

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const int maxn = 2010;
struct node
{
    int v;
    int layer;
};
vector<node> A[maxn];
bool vist[maxn]={false};
int n,l;

int BFS(int i)
{
    queue<node> q;
    int forw = 0;   //转发数
//    vist[i] = true;
    node t;
    t.v = i;
    t.layer = 0;
    q.push(t);
    vist[t.v] = true;

    while(!q.empty())
    {
        node top = q.front();
        q.pop();
        int u = top.v;
        for(int j = 0;j<A[u].size();j++)
        {
            node next = A[u][j]; //下一个节点
            next.layer = top.layer + 1;
            if(vist[next.v] == false && next.layer <= l) //如果下一个节点还未访问,且层数低于给定值
            {
                q.push(next);
                vist[next.v] = true;
                forw++;
            }
        }
    }
    return forw;

}

int main()
{
//    freopen("1.txt","r",stdin);
    node x;
    int k,temp;
    scanf("%d %d",&n,&l);
    for(int i = 1;i<=n;i++)
    {
        x.v = i;
        scanf("%d",&k);
        for(int j = 0;j<k;j++)
        {
            scanf("%d",&temp);
            A[temp].push_back(x); //关注的人 -> 当前用户
        }
    }
    int m,id;
    scanf("%d",&m);
    for(int i = 0;i<m;i++)
    {
        fill(vist+1,vist+n+1,0);
        scanf("%d",&id);
        int tt= BFS(id);
        printf("%d\n",tt);
    }


    return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
const int maxv = 2011,inf = 1<<30;

struct node
{
    int data;
    int lay;
};

int G[maxv][maxv] = {0},n,l;
bool vis[maxv] = {false};
int Bfs(int uid)
{
    node topN;
    topN.data = uid;
    topN.lay = 0;
    int ans = 0;
    queue<node>q;
    q.push(topN);
    vis[uid] = true;
    while(!q.empty())
    {
        node s = q.front();
        q.pop();
//        vis[s] = true;
//        if(layer > l)
//            break;
        for(int i = 1;i<=n;i++)
        {
            if(G[s.data][i] == 1 && vis[i] == false && s.lay < l)
            {
                vis[i] = true;
                ans++;
                node t;
                t.data = i;
                t.lay = s.lay + 1;
                q.push(t);
            }
        }

    }
    return ans;
}

int main()
{
    freopen("1.txt","r",stdin);
    scanf("%d%d",&n,&l);
    for(int i = 1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        for(int j=0;j<t;j++)
        {
            int fid;
            scanf("%d",&fid);
//            G[i][fid] = 1;
            G[fid][i] = 1;
        }
    }
    int k;
    scanf("%d",&k);
    for(int i=0;i<k;i++)
    {
        fill(vis,vis+maxv,0);
        int uid;
        scanf("%d",&uid);
        printf("%d\n",Bfs(uid));
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值