码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 链表小题.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. }
  • 相关阅读:
    Linux上x86_64架构的动态链接器 ld-linux-x86-64.so.2
    Unity Protobuf+RPC+UniTask
    【JavaWeb】13-EL表达式
    RK3588 DRM显示框架
    模拟桌面自动整理, 先顶左,再顶上。
    机器学习——代价敏感错误率与代价曲线
    Spring入门这一篇就够了
    CAP 6.2 版本发布通告
    中秋实验揭示gin下ShouldBindJSON、ShouldBindWith的json到对象映射程序是怎么跑飞的[Go 避坑必备之良好的编码习惯很重要]
    Servlet
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126809726
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号