【abc417】E - A Path in A Dictionary

Problem Statement

You are given a simple connected undirected graph?G?with?N?vertices and?M?edges.
The vertices of?G?are numbered vertex?1, vertex?2,?…, vertex?N, and the?i-th?(1≤i≤M)?edge connects vertices?Ui??and?Vi?.

Find the lexicographically smallest simple path from vertex?X?to vertex?Y?in?G.
That is, find the lexicographically smallest among the integer sequences?P=(P1?,P2?,…,P∣P∣?)?that satisfy the following conditions:

  • 1≤Pi?≤N

  • If?i\neqj, then?Pi?\neqPj?.

  • P1?=X?and?P_{|P|}=Y

  • For?1≤i≤∣P∣?1, there exists an edge connecting vertices?Pi??and?Pi+1?.

One can prove that such a path always exists under the constraints of this problem.

You are given?T?test cases, so find the answer for each.

Lexicographic order on integer sequencesAn integer sequence?S=(S1?,S2?,…,S∣S∣?)?is lexicographically smaller than an integer sequence?T=(T1?,T2?,…,T∣T∣?)?if either of the following 1. or 2. holds. Here,?∣S∣?and?∣T∣?represent the lengths of?S?and?T, respectively.

  1. ∣S∣<∣T∣?and?(S1?,S2?,…,S∣S∣?)=(T1?,T2?,…,T∣S∣?).

  2. There exists some?1≤i≤min(∣S∣,∣T∣)?such that?(S1?,S2?,…,Si?1?)=(T1?,T2?,…,Ti?1?)?and?Si?<Ti?.

Constraints

  • 1≤T≤500

  • 2≤N≤1000

  • N?1≤M≤min(2N(N?1)?,5×104)

  • 1≤X,Y≤N

  • X\neqY

  • 1≤Ui?<Vi?≤N

  • If?i\neqj, then?(Ui?,Vi?)\neq(Uj?,Vj?).

  • The given graph is connected.

  • The sum of?N?over all test cases in each input is at most?1000.

  • The sum of?M?over all test cases in each input is at most?5×104.

  • All input values are integers.


Input

The input is given from Standard Input in the following format:

T
case1?
case2?
?
caseT?

casei??represents the?i-th test case. Each test case is given in the following format:

N M X Y
U1? V1?
U2? V2?
?
UM? VM?

Output

Output?T?lines.
The?i-th line?(1≤i≤T)?should contain the vertex numbers on the simple path that is the answer to the?i-th test case, in order, separated by spaces.
That is, when the answer to the?i-th test case is?P=(P1?,P2?,…,P∣P∣?), output?P1?,?P2?,?…,?P∣P∣??on the?i-th line in this order, separated by spaces.


Sample Input 1

2
6 10 3 5
1 2
1 3
1 5
1 6
2 4
2 5
2 6
3 4
3 5
5 6
3 2 3 2
1 3
2 3

Sample Output 1

3 1 2 5
3 2

For the first test case, graph?G?is as follows:

The simple paths from vertex?3?to vertex?5?on?G, listed in lexicographic order, are as follows:

  • (3,1,2,5)
  • (3,1,2,6,5)
  • (3,1,5)
  • (3,1,6,2,5)
  • (3,1,6,5)
  • (3,4,2,1,5)
  • (3,4,2,1,6,5)
  • (3,4,2,5)
  • (3,4,2,6,1,5)
  • (3,4,2,6,5)
  • (3,5)

Among these, the lexicographically smallest is?(3,1,2,5), so output?3,1,2,5?separated by spaces on the first line.

For the second test case,?(3,2)?is the only simple path from vertex?3?to vertex?2.

題目大意:找到從x到y的最小字典序通路

用鄰接圖存儲,排序,dfs搜索,回溯,注意回溯的時候,不用把狀態再改回false,因為我們在dfs到該點時,已經發現它是構不成連通的,所以下次就不需要再到達這個節點了,這樣可以減少很大一部分運算,從而將TLE的代碼變成AC

AC代碼:

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll N=200010;
int n,m,x,y;
vector<int>path;
bool found;	
void dfs(int current,vector<vector<int>>&graph,vector<bool>&visit){path.push_back(current);visit[current]=true;if (current==y){found=true;for (int i:path)  cout<<i<<" ";cout<<"\n";return ;	}sort(graph[current].begin(),graph[current].end());for (auto i:graph[current]){if (!visit[i]&&!found){visit[i]=true;dfs(i,graph,visit);if (found)return ;path.pop_back();}}
}
void solve(){cin>>n>>m>>x>>y;vector<vector<int>>graph(n+1);for (int i=1;i<=m;i++){int u,v;cin>>u>>v;graph[u].push_back(v);graph[v].push_back(u);}path.clear();vector<bool>visit(n+1,false);found=false;dfs(x,graph,visit);
}
int main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int T;cin>>T;while(T--){solve();}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/91772.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/91772.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/91772.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux火焰圖

火焰圖簡介火焰圖是一種性能分析的可視化工具&#xff0c;它將CPU的調用棧&#xff08;Call Stack&#xff09;信息以矩形火焰的形式展現出來。Y軸&#xff1a;代表調用棧的深度&#xff08;函數A調用了函數B&#xff0c;B就疊在A上面&#xff09;。X軸&#xff1a;代表CPU的抽…

解剖 .NET 經典:從 Component 到 BackgroundWorker

1?? 背景與定位在 .NET Framework 2.0 時代&#xff0c;微軟引入了 BackgroundWorker 來解決 WinForm/WPF 場景下“耗時操作阻塞 UI 線程”的問題&#xff1b;而 Component 早在 1.0 就已存在&#xff0c;是所有可視化/非可視化設計器的“基類”。理解這兩者的源碼與機制&…

桌面端界面設計 |貨物 TMS 系統 - SaaS UI UX 設計:審美積累之境

在物流數字化的浪潮中&#xff0c;貨物 TMS 系統的 SaaS 化與 UI/UX 設計正構建著獨特的審美坐標系。這不僅是技術與功能的融合&#xff0c;更是一場關于效率美學的深度探索&#xff0c;為行業審美積累注入了鮮活的實踐樣本。SaaS 模式賦予貨物 TMS 系統輕盈而強大的特質&#…

多架構鏡像整合全攻略:在Docker中實現單一鏡像支持同時支持amd64和arm64架構

多架構支持的挑戰 &#xff1a;隨著異構計算&#xff08;如 ARM、x86、RISC-V 等&#xff09;的普及&#xff0c;開發者需要為不同硬件平臺提供對應的鏡像&#xff0c;傳統方式需維護多個版本&#xff08;如 image:v1-amd64 和 image:v1-arm64 &#xff09;&#xff0c;導致版本…

Linux730 tr:-d /-s;sort:-r,-n,-R,-o,-t,-k,-u;bash;cut:-d,-c;tee -a;uniq -c -i

回顧 sort sort [選項] 文件-u&#xff1a;唯一&#xff0c;去除重復 -r:按數字大小&#xff0c;倒序排序&#xff0c;大到小 -o:輸出文件 -n:按數字大小&#xff0c;順序排序&#xff0c;小到大 -t: -t后加分割符&#xff0c;按分割符為標準&#xff0c;進行篩選 -k:k后加數字…

力扣457:環形數組是否存在循環

力扣457:環形數組是否存在循環題目思路代碼題目 存在一個不含 0 的 環形 數組 nums &#xff0c;每個 nums[i] 都表示位于下標 i 的角色應該向前或向后移動的下標個數&#xff1a; 如果 nums[i] 是正數&#xff0c;向前&#xff08;下標遞增方向&#xff09;移動 |nums[i]| 步…

在 Elasticsearch 中落地 Learning to Rank(LTR)

1 為什么要引入 LTR&#xff1f; 常規檢索&#xff08;BM25、語義檢索、Hybrid、RRF …&#xff09;往往只能基于少量信號&#xff08;關鍵詞命中、向量相似度&#xff09;排序。 Learning-to-Rank 通過機器學習模型把多維度特征&#xff08;文檔屬性、查詢屬性、查詢-文檔相關…

Socket編程——TCP協議

文章目錄一、TCP傳輸二、相關接口三、多進程版本四、多線程版本一、TCP傳輸 TCP和UDP類似&#xff0c;但是在傳輸中TCP有輸入&#xff0c;輸出緩沖區&#xff0c;看下面的傳輸圖片 可以理解為TCP之間的數據傳輸都是依賴各自的socket&#xff0c;socket就充當傳輸的中介吧。 而…

GitHub使用小記——本地推送、外部拉取和分支重命名

GitHub 項目推送與拉取等操作使用隨記 本小記適用于個人項目或組織項目&#xff0c;涵蓋 GitHub 推送、拉取、分支管理、.gitignore 設置等常見需求。 1. 將已有本地工程推送至 GitHub 新倉庫 1.1 前提條件 本地項目結構完整&#xff0c;已準備好&#xff1b;本地已安裝 Git…

RabbitMQ 延時隊列插件安裝與使用詳解(基于 Delayed Message Plugin)

RabbitMQ 延時隊列插件安裝與使用詳解&#xff08;基于 Delayed Message Plugin&#xff09;&#x1f4cc; 一、什么是 RabbitMQ 延時隊列&#xff1f;&#x1f680; 二、安裝前準備? RabbitMQ 環境要求&#x1f527; 三、安裝延時隊列插件&#x1f9e9; 插件名稱&#xff1a;…

Vue項目使用ssh2-sftp-client實現打包自動上傳到服務器(完整教程)

告別手動拖拽上傳&#xff01;本教程將手把手教你如何通過ssh2-sftp-client實現Vue項目打包后自動上傳到服務器&#xff0c;提升部署效率300%。&#x1f680;一、需求場景與解決方案在Vue項目開發中&#xff0c;每次執行npm run build后都需要手動將dist目錄上傳到服務器&#…

《質光相濟:Three.js中3D視覺的底層交互邏輯》

在Three.js搭建的虛擬維度中,光照與材質的關系遠非技術參數的簡單疊加,當光線以數字形態穿越虛空,與物體表面相遇的瞬間,便開始書寫屬于這個世界的物理敘事——每一縷光斑的形狀、每一塊陰影的濃淡、每一寸肌理的反光,都是對現實光學規律的轉譯與重構。理解這種交互的深層…

無刷電機在汽車領域的應用與驅動編程技術

文章目錄引言一、核心應用場景1. 新能源汽車動力系統2. 底盤控制系統3. 車身與舒適系統4. 智能駕駛與安全系統二、無刷電機的技術優勢解析三、無刷電機驅動編程基礎1. 驅動原理2. 驅動架構四、核心控制算法與實現1. 六步換向法&#xff08;梯形波控制&#xff09;算法流程圖C語…

【游戲引擎之路】登神長階(十八):3天制作Galgame引擎《Galplayer》——無敵之道心

游戲引擎開發記錄&#xff1a;2024年 5月20日-6月4日&#xff1a;攻克2D物理引擎。 2024年 6月4日-6月13日&#xff1a;攻克《3D數學基礎》。 2024年 6月13日-6月20日&#xff1a;攻克《3D圖形教程》。 2024年 6月21日-6月22日&#xff1a;攻克《Raycasting游戲教程》。 2024年…

kotlin kmp 跨平臺環境使用sqldelight

歡迎訪問我的主頁: https://heeheeaii.github.io/ 1. 項目結構 SQLDelightKMPDemo/ ├── shared/ │ ├── src/ │ │ ├── commonMain/kotlin/ │ │ ├── androidMain/kotlin/ │ │ ├── desktopMain/kotlin/ │ │ └── commonMain/sqldel…

機器學習【五】decision_making tree

決策樹是一種通過樹形結構進行數據分類或回歸的直觀算法&#xff0c;其核心是通過層級決策路徑模擬規則推理。主要算法包括&#xff1a;ID3算法基于信息熵和信息增益選擇劃分屬性&#xff1b;C4.5算法改進ID3&#xff0c;引入增益率和剪枝技術解決多值特征偏差&#xff1b;CART…

簡單記錄一下VSCode中的一些學習記

在剛開始學習VSCode時&#xff0c;相信大家都會好奇VSCode底部區域那幾個不同的狀態欄具體有什么作用&#xff08;輸出、調試控制臺、終端、端口&#xff09;&#xff0c;貌似好像都是輸出與代碼相關的信息的&#xff1f;貌似代碼運行結果既可以出現在輸出中&#xff0c;也可以…

基于 Hadoop 生態圈的數據倉庫實踐 —— OLAP 與數據可視化(二)

目錄 二、Hive、SparkSQL、Impala 比較 1. SparkSQL 簡介 2. Hive、SparkSQL、Impala 比較 &#xff08;1&#xff09;功能 &#xff08;2&#xff09;架構 &#xff08;3&#xff09;場景 3. Hive、SparkSQL、Impala 性能對比 &#xff08;1&#xff09;cloudera 公司…

C++:std::array vs 原生數組 vs std::vector

&#x1f4cc; C&#xff1a;std::array vs 原生數組 vs std::vector 引用&#xff1a; C/C 標準庫 std::vector、std::array、原生靜態數組 的區別有哪些&#xff1f; 深度剖析&#xff1a;std::vector 內存機制與 push_back 擴容策略 今天過去了 還有許許多個明天 能和大…

Hyper-V + Centos stream 9 搭建K8s集群(二)

一、安裝自動補全主節點安裝就可以yum install -y bash-completion echo source <(kubectl completion bash) >>~/.bashrc kubectl completion bash >/etc/bash_completion.d/kubectl二、安裝Calico網絡插件&#xff08;主節點&#xff09;下載文件wget https://ca…