PTA甲級 1076 Forwards on Weibo (30分)
強烈推薦,刷P他的朋友都認識一下柳神–PTA解法大佬
本文由參考於柳神部落格寫成
還有就是非常非常有用的 演算法筆記 全名是
演算法筆記 上級訓練實戰指南 //這本都是P他的題解
演算法筆記
PS 今天也要加油鴨
題目原文
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 can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:
M[i] user_list[i]
where M[i]
(≤100) is the total number of people that user[i]
follows; and user_list[i]
is a list of the M[i]
users that followed by user[i]
. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.
Then finally a positive K is given, followed by K UserID
's for query.
Output Specification:
For each UserID
, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.
Sample Input:
7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6
Sample Output:
4
5
生詞如下:
PS:關鍵詞沒有看懂就GG,大意就是給你一個圖,和連線關係.
然後再給出查詢的結點.問你?(我沒有看懂的地方)
關鍵句子:
maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.
假設僅計算L級間接關注者,則任何特定使用者的最大潛在轉發量
最大的潛在轉發量只計算L級的關注者.
indirect 間接
trigger 觸發
題目大意:
就是問你一個有向圖中,已某個點當中心,它下面L層一共有多少結點.
思路如下:
BFS或者DFS都可以簡單的解決問題
DFS比較麻煩一點.
要注意這種情況
5 3
0
1 1
1 2
2 1 3
1 4
1 1
正確答案要輸出
4
因為我們在遍歷了 1->2->3->4之後,5 就會被彈出.
但是在1-4-5的情況下是可以的.
我們需要對DFS的程式碼做修改
修改後的DFS程式碼如下:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int Max = 1024;
int n, l, fan, id, query = 0, t,SumRoot=0, level[Max];
bool vis[Max];
vector<int> G[Max];
void dfsTrave(int v, int depth) {
vis[v] = true;
level[v] = depth;
if (depth < l) { //剪枝的一個方法level[G[v][i]] > depth + 1 就是當出現
for (int i = 0; i < G[v].size(); ++i) {
if (!vis[G[v][i]] || level[G[v][i]] > depth + 1) dfsTrave(G[v][i], depth + 1);
}
}
return;
}
int main(void) {
scanf("%d%d", &n, &l);
for (int i = 1; i <= n; ++i) {
scanf("%d", &fan);
for (int j = 0; j < fan; ++j) {
scanf("%d", &id);
G[id].push_back(i);
}
}
scanf("%d", &query);
for (int i = 0; i < query; ++i) {
scanf("%d", &t);
SumRoot = 0;
memset(vis, false, sizeof(vis));
memset(level, -1, sizeof(level));
dfsTrave(t, 0);
for(int i=1;i<=n;++i) SumRoot+= level[i] > 0 ? 1 : 0;
printf("%d\n", SumRoot);
}
return 0;
}
BFS程式碼如下:
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int Max = 1024;
int n, l, fan, id, query = 0, t, SumRoot = 0, idlevel[Max];
bool vis[Max];
vector<int> G[Max];
void bfs(int v) {
queue<int> q;
vis[v] = true;
q.push(v);
idlevel[v] = 0;
while (q.size()) {
int u = q.front();
q.pop();
int next = idlevel[u] + 1;
if (next > l)return;
for (int i = 0; i < G[u].size(); ++i) {
if (!vis[G[u][i]]){
SumRoot++;
vis[G[u][i]] = true;
idlevel[G[u][i]] = next;
q.push(G[u][i]);
}
}
}
}
int main(void) {
scanf("%d%d", &n, &l);
for (int i = 1; i <= n; ++i) {
scanf("%d", &fan);
for (int j = 0; j < fan; ++j) {
scanf("%d", &id);
G[id].push_back(i);
}
}
scanf("%d", &query);
for (int i = 0; i < query; ++i) {
scanf("%d", &t);
SumRoot = 0;
memset(vis, false, sizeof(vis));
bfs(t);
printf("%d\n", SumRoot);
}
return 0;
}
如果這篇文章對你有張幫助的話,可以用你高貴的小手給我點一個免費的贊嗎
相信我,你也能變成光.
如果你有任何建議,或者是發現了我的錯誤,歡迎評論留言指出.
相關文章
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- 1076 Wifi密碼 (15分)WiFi密碼
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- PAT甲級考試題庫題目分類
- PAT甲級1126~1130|C++實現C++
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PTA乙級_1024 科學計數法 (20分)_pythonPython
- PTA 檢查密碼 (15分)密碼
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- Struts中global-forwards問題Forward
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- C++學習之路 | PTA乙級—— 1015 德才論 (25分)(精簡)C++
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- pta
- PAT甲級1023 Have Fun with Number
- 【PTA】 學生成績錄入及查詢 (20分)
- 6-1 二分查詢 (20分) PTA 資料結構資料結構
- PAT-B 1076 Wifi密碼WiFi密碼
- L1-057 PTA使我精神煥發 分數 5
- PAT甲級-1005. Spell It Right (20)各位之和
- PTA1090 危險品裝箱 (25分)詳解
- 暴力解法破解PTA L1-006 連續因子 (20分)
- PTA 7-60 衝鋒衣選貨及折扣 (15分) (C語言)C語言
- PAT1076 WiFi密碼(java實現)WiFi密碼Java
- PAT甲級-1010. Radix (25)進位制
- PAT甲級-1012. The Best Rank (25)並列排序排序
- 2024 秋季PAT認證甲級(題解A1-A4)
- 基礎程式設計題(PTA) 7-35 有理數均值 (20分)程式設計
- 7-34 PTA C語言--求分數序列前N項和C語言
- docker筆記30-k8s dashboard認證及分級授權Docker筆記K8S
- 30分鐘SQL指南SQL
- 基礎程式設計題(PTA) 7-26 單詞長度 (15分)程式設計
- Java解決 PTA L2-003 月餅 (25分) 超時問題Java
- PTA題目總結