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 visited[NUM];
int forwards;
map<int,vector<int> > adjlist;

void reset()
{
memset(visited,false,sizeof(bool)*NUM);
forwards=0;
}

void dfs(int sourceNode,int indLevel,int L)
{
if(indLevel>L)
return;
for(vector<int>::iterator iter=adjlist[sourceNode].begin();iter!=adjlist[sourceNode].end();++iter)
{
if(!visited[*iter])
{
++forwards;
visited[*iter]=true;
dfs(*iter,indLevel+1,L);
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int N,L;
scanf("%d %d",&N,&L);
if(L>6)
L=6;
int i,Mi,j,followed;
for(i=1;i<=N;++i)
{
scanf("%d",&Mi);
for(j=0;j<Mi;++j)
{
scanf("%d",&followed);
adjlist[followed].push_back(i);
}
}
int K,query;
scanf("%d",&K);
for(i=0;i<K;++i)
{
scanf("%d",&query);
reset();
visited[query]=true;
dfs(query,1,L);
printf("%d\n",forwards);
}
return 0;
}

后来换了bfs后,AC了:

#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;

const int NUM=1001;

bool hasForward[NUM];
int forwards;
map<int,vector<int> > adjlist;
queue<int> recordQueue;

void reset()
{
memset(hasForward,false,sizeof(bool)*NUM);
forwards=0;
while(!recordQueue.empty())
recordQueue.pop();
}

void bfs(int query,int level)
{
int curLevel=1;
hasForward[query]=true;
recordQueue.push(query);
int endOfLevel=recordQueue.back();
int cur;
while(!recordQueue.empty()&&curLevel<=level)
{
cur=recordQueue.front();
recordQueue.pop();
for(vector<int>::iterator iter=adjlist[cur].begin();iter!=adjlist[cur].end();++iter)
{
if(!hasForward[*iter])
{
recordQueue.push(*iter);
++forwards;
hasForward[*iter]=true;
}
}
if(cur==endOfLevel)
{
++curLevel;
endOfLevel=recordQueue.back();
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int N,L;
scanf("%d %d",&N,&L);
if(L>6)
L=6;
int i,j;
int Mi,followed;
for(i=1;i<=N;++i)
{
scanf("%d",&Mi);
for(j=0;j<Mi;++j)
{
scanf("%d",&followed);
adjlist[followed].push_back(i);
}
}
int K,query;
scanf("%d",&K);
for(i=0;i<K;++i)
{
scanf("%d",&query);
reset();
bfs(query,L);
printf("%d\n",forwards);
}
return 0;
}

PAT 1076. Forwards on Weibo (30),布布扣,bubuko.com

时间: 2024-10-25 07:51:34

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

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 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) 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]; //邻接表,每个位置都是一个nod

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

PAT A1076 Forwards on Weibo (30 分)——图的bfs

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

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