• 链表小题.Play Train AtCoder - abc225_d


    链接

    由于是7个月前的代码,可能不是最优解或者可能冗余,但必定正确

    Takahashi is playing with toy trains, connecting and disconnecting them.
    There are N toy train cars, with car numbers: Car 1, Car 2, \ldots…, Car N.
    Initially, all cars are separated.

    You will be given Q queries.

    Process them in the order they are given.

    There are three kinds of queries, as follows.

    • 1 x y: Connect the front of Car y to the rear of Car x.
      It is guaranteed that:

      • x \neq yx=y
      • just before this query, no train is connected to the rear of Car x;
      • just before this query, no train is connected to the front of Car y;
      • just before this query, Car x and Car y belong to different connected components.
    • 2 x y: Disconnect the front of Car y from the rear of Car x.
      It is guaranteed that:

      • x \neq yx=y;
      • just before this query, the front of Car y is directly connected to the rear of Car x.
    • 3 x: Print the car numbers of the cars belonging to the connected component containing Car x, from front to back.

    题目大意

    有n辆车和q次操作,每次操作有三种类型:
    1,x,y 表示将y yy连到x xx的后面
    2,x,y表示将y yy从x xx后面断开
    3,x表示询问x xx相连的车的集合

    1. #include
    2. using namespace std;
    3. int n,q,i,j,k,t,x,y,z;
    4. int l[199999],r[199999];
    5. vector<int>::iterator it;
    6. int main(){
    7. cin>>n>>q;
    8. while(q--){
    9. cin>>z>>x;
    10. if(z!=3){
    11. cin>>y;
    12. }
    13. if(z==1){
    14. l[y]=x;
    15. r[x]=y;
    16. }
    17. else if(z==2){
    18. l[y]=0;
    19. r[x]=0;
    20. }
    21. else{
    22. vector<int>s;
    23. y=r[x];
    24. while(x){
    25. s.push_back(x);
    26. x=l[x];
    27. }
    28. reverse(s.begin(),s.end());
    29. while(y){
    30. s.push_back(y);
    31. y=r[y];
    32. }
    33. cout<size();
    34. for(it=s.begin();it!=s.end();++it){
    35. cout<<" "<<*it;
    36. }
    37. cout<
    38. }
    39. }
    40. return 0;
    41. }
  • 相关阅读:
    集合框架的总结1
    Jmeter工具修改为中文模式,修改字号,出现乱码情况
    使用python统计jira bug数据
    P1827 [USACO3.4] 美国血统 American Heritage(前序 + 中序 生成后序)
    Javascript中的模块化详解
    详细介绍项目开发中多种常用的分布式锁的实现以及分析比较
    spark on hive
    MySQL备份与恢复
    Navicat的使用--mysql
    太原理工大学计算机考研资料汇总
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126809726