PAT:1076. Forwards on Weibo (30) AC

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int MAX=1010;

bool tag[MAX];    //标记BFS是是否被访问过

struct node
{
  int ID;      //编号
  int layer;    //层号
};

vector<node> Adj[MAX];  //邻接表,每个位置都是一个node
queue<node> q;      //BFS待查询队列

int BFS(int s,int l)    //起始点s,极限传播层数l
{
  int transNum=0;      //转发次数
  node start;
  start.ID=s;
  start.layer=0;
  q.push(start);
  tag[s]=1;
  while(!q.empty())
  {
    node tmp=q.front();
    q.pop();
    int u=tmp.ID;
    for(int i=0 ; i<Adj[u].size() ; ++i)    //处理此编号的邻接节点
    {
      node next=Adj[u][i];          //【skill】可以直接用二维数组
      next.layer=tmp.layer+1;
      if(tag[next.ID]==0 && next.layer<=l)  //未被访问而且在传播范围之内
      {
        ++transNum;
        tag[next.ID]=1;            //符合的才进的来,在这里改标记
        q.push(next);
      }
    }
  }
  return transNum;
}

int main()
{
  int n,lev;
  node user;
  scanf("%d%d",&n,&lev);
  for(int i=1 ; i<=n ; ++i)        //建立邻接表
  {
    user.ID=i;
    int followNum;
    scanf("%d",&followNum);
    for(int j=0 ; j<followNum ; ++j)
    {
      int f;
      scanf("%d",&f);
      Adj[f].push_back(user);      //【caution】看清题意:f号可以转发给编号为i的用户user
    }
  }
  int k;
  scanf("%d",&k);
  for(int t=0 ; t<k ; ++t)
  {
    int query;
    scanf("%d",&query);
    memset(tag,0,sizeof(tag));
    printf("%d\n",BFS(query,lev));
  }
  return 0;
}
时间: 2024-10-13 02:31:03

PAT:1076. Forwards on Weibo (30) AC的相关文章

PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

题目 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 followe

PAT 1076. Forwards on Weibo (30)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1076 此题考查图的遍历操作: 本来我个人觉得可以用dfs的,但是不知何故,有两个case没有过,贴出代码,望指出错误之处: #include <cstdio> #include <map> #include <vector> #include <memory.h> using namespace std; const int NUM=1001; bool

1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B 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 follo

PAT (Advanced Level) 1076. Forwards on Weibo (30)

最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm>

1076. Forwards on Weibo (30)

时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT 1076 Forwards on Weibo

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

A1076. Forwards on Weibo (30)

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

PAT:1060. Are They Equal (25) AC

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int n; string deal(string s,int &e) //[思维]1:吸收多余0:.2:找到“.”判断与1的大小.3:去除“.”的同时统计10的指数(正负与step2有关) { //4:判断是否删完了,删完了表明数字是0.5:存入前n个数字,不足用

PAT:1018. 锤子剪刀布 (20) AC

#include<stdio.h> #include<stdlib.h> int main() { int n,maxA,maxB; maxA=maxB=-1; int A[3]={0},B[3]={0}; //0,1,2位置分别存胜利,平局,失败..填入次数 int HARSH1[3]={0},HARSH2[3]={0}; //0:布,1锤子,2剪刀: scanf("%d",&n); for(int t=0 ; t<n ; ++t) { getc