PAT 1076

JAVA沒法玩系列,C++寫了是248ms,JAVA 3000ms的時間限制都TLE,我已經看到考PAT的時候自己沮喪的樣子了。

簡單的BFS。

  1 import java.util.*;
  2 import java.io.*;
  3
  4 class FastReader{
  5     BufferedReader reader;
  6     StringTokenizer tokenizer;
  7
  8     FastReader(InputStream stream){
  9         reader = new BufferedReader(new InputStreamReader(stream), 1 << 20);
 10         tokenizer = null;
 11     }
 12
 13     String next(){
 14         while (tokenizer == null || !tokenizer.hasMoreTokens()){
 15             try {
 16                 tokenizer = new StringTokenizer(reader.readLine());
 17             } catch (Exception e){
 18                 throw new RuntimeException(e);
 19             }
 20         }
 21
 22         return tokenizer.nextToken();
 23     }
 24
 25     int next_int(){
 26         return Integer.parseInt(next());
 27     }
 28 }
 29
 30 public class Main {
 31     static ArrayList<ArrayList<Integer>> graph;
 32
 33     static int bfs(int start, int level){
 34         int count = 0;
 35         int level_cnt = 0;
 36
 37         Boolean[] visited = new Boolean[graph.size()];
 38         Arrays.fill(visited, false);
 39
 40         Queue<Integer> cur_level = new LinkedList<Integer>();
 41         cur_level.add(start);
 42         visited[start] = true;
 43
 44         while (!cur_level.isEmpty()){
 45             Queue<Integer> next_level = new LinkedList<Integer>();
 46
 47             while (!cur_level.isEmpty()){
 48                 int cur_idx = cur_level.poll();
 49
 50                 for (Integer next_idx : graph.get(cur_idx)){
 51                     if (visited[next_idx])
 52                         continue;
 53
 54                     next_level.add(next_idx);
 55                     visited[next_idx] = true;
 56                     count++;
 57                 }
 58             }
 59
 60             level_cnt++;
 61             if (level_cnt >= level)
 62                 break;
 63
 64             cur_level = next_level;
 65         }
 66
 67         return count;
 68     }
 69
 70     @SuppressWarnings("unused")
 71     public static void main(String[] args){
 72         FastReader reader = new FastReader(System.in);
 73
 74         int N, L;
 75         N = reader.next_int();
 76         L = reader.next_int();
 77
 78         graph = new ArrayList<ArrayList<Integer>>();
 79         for (int i = 0; i < N; i++)
 80             graph.add(new ArrayList<Integer>());
 81
 82         for (int i = 0; i < N; i++){
 83             int num = reader.next_int();
 84
 85             for (int j = 0; j < num; j++){
 86                 int idx = reader.next_int();
 87                 graph.get(idx - 1).add(i);
 88             }
 89         }
 90
 91         int K = reader.next_int();
 92         ArrayList<Integer> res_arr = new ArrayList<Integer>();
 93         for (int i = 0; i < K; i++){
 94             int idx = reader.next_int();
 95             int res = bfs(idx - 1, L);
 96
 97             res_arr.add(res);
 98         }
 99
100         for (Integer res : res_arr){
101             System.out.println(res);
102         }
103     }
104 }

下面刷題都用C++了,感覺clang++的編譯報錯好萌啊...

时间: 2024-12-17 07:08:14

PAT 1076的相关文章

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

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 1076 Wifi密码

下面是微博上流传的一张照片:"各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一换.谢谢合作!!~"-- 老师们为了促进学生学习也是拼了-- 本题就要求你写程序把一系列题目的答案按照卷子上给出的对应关系翻译成 wifi 的密码.这里简单假设每道选择题都有 4 个选项,有且只有 1 个正确答案. 输入格式: 输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行

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

1076 Wifi密码 下面是微博上流传的一张照片:"各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一换.谢谢合作!!~"-- 老师们为了促进学生学习也是拼了-- 本题就要求你写程序把一系列题目的答案按照卷子上给出的对应关系翻译成 wifi 的密码.这里简单假设每道选择题都有 4 个选项,有且只有 1 个正确答案. 输入格式: 输入第一行给出一个正整数 N(≤ 10

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>

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甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.