【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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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…