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甲級——Be Unique
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM
- PAT甲級1032 Sharing
- 1021 Deepest Root(甲級)
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- PTA乙級 1004 成績排名
- PAT甲級1023 Have Fun with Number
- 1076 Wifi密碼 (15分)WiFi密碼
- PAT甲級-1015. Reversible Primes (20)
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT-B 1076 Wifi密碼WiFi密碼
- PAT甲級1126~1130|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PAT1076 WiFi密碼(java實現)WiFi密碼Java
- pta
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PTA乙級_1024 科學計數法 (20分)_pythonPython
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- 2024 秋季PAT認證甲級(題解A1-A4)
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- PTA出租
- Homestead 建立應用顯示 Could not delete /home/vagrant/code/weibodelete
- 夕甲甲——孔乙己之C++版C++
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 2021.9.12週六PAT甲級考試覆盤與總結
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- pta檢索
- [PTA]day 2
- [PTA]day 3
- [PTA]day 5
- [PTA]day 9
- [PTA]day 7