冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 927|回覆: 0
打印 上一主題 下一主題

[心得] [演算法心得] Codeforces. Valid BFS?

[複製鏈接]

7

主題

0

好友

11

積分

新手上路

Rank: 1

UID
320966
帖子
146
主題
7
精華
0
積分
11
楓幣
756
威望
11
存款
0
贊助金額
0
推廣
0
GP
31
閱讀權限
10
性別
保密
在線時間
9 小時
註冊時間
2021-9-30
最後登入
2022-7-6
跳轉到指定樓層
1
發表於 2022-5-10 14:32:34 |只看該作者 |倒序瀏覽
本帖最後由 jimmyhealer 於 2022-5-10 14:36 編輯

嗨大家好~ 我是新加入論壇的萌新,之後可能會每日更新一下演算法心得,那目前會在 c/c++ 版,如果大家很熱絡的話再申請 演算法/競賽程式 版吧!

(昨天太忙了~~ 斷更QQ)

--------------------------------------------------- 正文開始 ---------------------------------------------------

題目名稱: Valid BFS?
題目網站: Codeforces
題目網址: https://codeforces.com/problemset/problem/1037/D
題目內容:

The BFS algorithm is defined as follows.

Consider an undirected graph with vertices numbered from 1 to n. Initialize q as a new queue containing only vertex 1, mark the vertex 1 as used.
Extract a vertex v from the head of the queue q.
Print the index of vertex v.
Iterate in arbitrary order through all such vertices u that u is a neighbor of v and is not marked yet as used. Mark the vertex u as used and insert it into the tail of the queue q.
If the queue is not empty, continue from step 2.
Otherwise finish.
Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.

In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex 1. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

題目大意:

給你一張無向圖,並且給你一個BFS過程中遇到點的順序,問是否為合法順序。

範例輸入 #1:
4
1 2
1 3
2 4
1 2 3 4


範例輸出 #1:
Yes

範例輸入 #2:
4
1 2
1 3
2 4
1 3 2 4


範例輸出 #2:
Yes

--------------------------------------------------- 題解心得 ---------------------------------------------------

題目要求的是BFS順序,把紀錄過的順序讓他對於每一個子樹做排序。
另外一個重點是,為方便實作,可以用鄰接表而不是鄰接矩陣,因為如下。
假設 節點1 有三個子節點 分別為 節點2 節點3 節點 4,而BFS的順序為 1342。
代表 節點3 是第一個迭代的節點,節點4 為第二個迭代的節點, 等等...
那 節點1 的鄰接表排序後就變成 342。
最後只要正常的做BFS操作,每次紀錄加入佇列的點。
判斷是否個題目所給的順序相同即可。

AC code (811ms, 14992 KB):
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define INF 0x3f3f3f3f
  4. #define MOD 1000000007
  5. #define io iOS::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. using namespace std;
  7. const int MXN = 200005;

  8. void sol(){
  9.   ll ans = 0, n = 0, m = 0, k = 1;
  10.   cin >> n;
  11.   vector<vector<int> > path(n + 1);
  12.   vector<int> ordered(n), index(n + 1);
  13.   for(int i = 0; i < n - 1; ++i){
  14.     int s, t;
  15.     cin >> s >> t;
  16.     path[s].emplace_back(t);
  17.     path[t].emplace_back(s);
  18.   }
  19.   for(int i = 0; i < n; ++i){
  20.     cin >> ordered[i];
  21.     index[ordered[i]] = i;
  22.   }
  23.   for(int i = 1; i <= n; ++i){
  24.     sort(path[i].begin(), path[i].end(), [&](int a, int b){
  25.           return index[a] < index[b];
  26.         });
  27.   }
  28.   queue<int> q;
  29.   vector<int> vis(n + 1), res;
  30.   q.push(1);
  31.   while(!q.empty()){
  32.     int p = q.front(); q.pop();
  33.     vis[p] = 1;
  34.     res.emplace_back(p);
  35.     for(int i : path[p]){
  36.       if(!vis[i]) q.push(i);
  37.     }
  38.   }
  39.   for(int i = 0; i < n; ++i){
  40.     if(ordered[i] != res[i]){
  41.       cout << "No\n";
  42.       return;
  43.     }
  44.   }
  45.   cout << "Yes\n";
  46. }

  47. int main(){
  48.   //io
  49.   int t = 1;
  50.   while(t--){
  51.     sol();
  52.   }
  53. }
複製代碼
[發帖際遇]: jimmyhealer 雖然沒有「哈味」,但全身卻散發出「魯」味,獲得路人同情施捨 1 楓幣 幸運榜 / 衰神榜
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-23 23:38

回頂部