PAT1076. Forwards on Weibo(标准bfs模板)

//标准的层次遍历模板

//居然因为一个j写成了i,debug半天。。。。。解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1001;
struct node
{
int level;
vector<int>child;
};
node list[maxn];
int n,l;
bool vis[maxn];

int bfs(int v)
{
for(int j=0;j<maxn;j++){list[j].level=0;vis[j]=false;}
queue<int>q;
q.push(v);vis[v]=true;
int cnt=0;
while(!q.empty())
{
int tmp=q.front();q.pop();
if(list[tmp].level>l)break;
cnt++;
for(int i=0;i<list[tmp].child.size();i++)
{
int s=list[tmp].child[i];
if(!vis[s])
{
list[s].level=list[tmp].level+1;
vis[s]=true;
q.push(s);
}
}
}
return cnt-1;
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k,tmp;
while(scanf("%d%d",&n,&l)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&k);
for(j=0;j<k;j++)
{
scanf("%d",&tmp);
list[tmp].child.push_back(i);
}
}
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d",&tmp);
int ans=bfs(tmp);
printf("%d\n",ans);
}
}
return 0;
}

时间: 2024-12-15 06:57:47

PAT1076. Forwards on Weibo(标准bfs模板)的相关文章

PAT1076. 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

PAT1076. Forwards on Weibo (30)

使用DFS出现超时,改成bfs DFS #include <iostream> #include <vector> #include <set> using namespace std; int n; int l; int i,j; int m,k; int follower[1001][1001]; vector<int> user_list[1001]; set<int> user_s; void queryU(int a,int depth

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: 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 hi

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

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

广度优先(BFS) ------- 模板1:-----模板2:--------模板3:

//使用数组queue[ ]存放结点队列 void BFS( ) { head=0; tail=1; queue[head]=首结点的值; while (head<tail) //队列不空 { temp=tail; for (k=head; k<=tail; k++) //对当前层扩展 { if ( 到达目的状态 ) { 输出结果; return; } for (i=1; i<=m; i++) //每个结点的m种扩展可能 if (可以扩展) { 处理每种可能情况: queue[temp+

HDU5012:Dice(bfs模板)

http://acm.hdu.edu.cn/showproblem.php?pid=5012 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face,

Knight Moves(hdu1372 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731    Accepted Submission(s): 4059 Problem Description A friend of you is doing res

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