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){
  if(depth==l){
    return ;
  }
  for(int i=0;i<user_list[a].size();i++){
    queryU(user_list[a][i],depth+1);
    user_s.insert(user_list[a][i]);
  }

  return ;
}
int main()
{
  cin>>n>>l;
  int a;
  for(i=1;i<=n;i++){
    cin>>m;
    for(j=0;j<m;j++){
      cin>>a;
      user_list[a].push_back(i);
    }
  }
  cin>>k;
  int sum;
  for(i=0;i<k;i++){
    cin>>a;
    user_s.clear();
    user_s.insert(a);
    queryU(a,0);
    cout<<user_s.size()-1<<endl;
  }
    return 0;
}

  bfs

最后一个测试用例一直MLE,后来用指针优化,就报超时了。

后来参考 https://www.zhihu.com/question/28264519,把队列删除语句省掉,set省掉,终于AC了!

#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n;
int l;
int i,j;
int m,k;
vector<int> user_list[1010];
vector<int> que;
int v[1005];
int main()
{
	cin>>n>>l;
	int a;
	for(i=1;i<=n;i++){
		cin>>m;
		for(j=0;j<m;j++){
			cin>>a;
			user_list[a].push_back(i);
		}
	}
	cin>>k;
	for(i=1;i<=k;i++){
		cin>>a;
		v[a]=i;
		int depth=0;
		que.clear();
		que.push_back(a);
		int levelSize=0;
		int count=1;
		for(int z=0;z<que.size();z++){
			int num=que[z];
			count--;
			vector<int> *tmp=&user_list[num];
			levelSize+=tmp->size();
			for(int y=0;y<tmp->size();y++){
				int temp_num=(*tmp)[y];
				if(v[temp_num]!=i){
					que.push_back(temp_num);
					v[temp_num]=i;
				}
			}
			if(count==0){
				count=levelSize;
				levelSize=0;
				depth++;
			}
			if(depth==l){
				break;
			}
		}
		cout<<que.size()-1<<endl;
	}
	return 0;
}

  

时间: 2024-10-03 22:37:45

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

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

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

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

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

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; ve