题目
给你N个辆车 和Q次询问。
询问有3种
1:将a车放在b车后面,保证b车后面没车且a车前没车,保证两车相邻。
2:将a车从b车断开。保证两车相邻
3:查询a车所在的列有多少车,并且从后往前输出。
最多打印1e6的车次。
题解思路
链表题写少了,一开始没感觉出来,一直想着并查集,实际上只需维护一个点的前指针和后指针即可。
直接模拟链表是On级别的。
所以能过的很快。
AC代码
#include <bits/stdc++.h>
//#include <unordered_map>
#define PII pair<int,int>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 100100 ;
vector <int> front(N,-1) ;
vector <int> back(N,-1) ;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
int n , q ;
cin >> n >> q ;
while ( q-- )
{
int t1 , t2 ,t3 ;
cin >> t1 ;
if ( t1 == 1 )
{
cin >> t2 >> t3 ;
front[t2] = t3 ;
back[t3] = t2 ;
}else if ( t1 == 2 )
{
cin >> t2 >> t3 ;
front[t2] = -1 ;
back[t3] = -1 ;
}else
{
vector <int> cs ;
cin >> t3 ;
int ps = t3 ;
while ( back[ps] != -1 )
ps = back[ps] ;
while ( ps != -1 )
{
cs.push_back(ps) ;
ps = front[ps] ;
}
cout << cs.size() << " " ;
for ( auto & i : cs )
cout << i << " ";
cout << "\n" ;
}
}
return 0 ;
}
本文介绍了一道关于车辆调度的问题,通过使用链表的数据结构来高效地解决车辆连接与查询的操作。采用前后指针的方式进行维护,使得每次操作都能快速完成。

424

被折叠的 条评论
为什么被折叠?



