码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 今日ac题


    【模板】拓扑排序 / 家谱树 - 洛谷

    终于凭着仅存的记忆写出来了,虽然是板子题

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int N = 1010;
    6. int e[N], ne[N], h[N], idx;
    7. int n, ind[N];
    8. queue<int> q, ans;
    9. void add(int a, int b)
    10. {
    11. e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
    12. }
    13. void topo()
    14. {
    15. for(int i = 1; i <= n; i ++ )
    16. {
    17. if(!ind[i])
    18. {
    19. q.push(i);
    20. }
    21. }
    22. while(q.size())
    23. {
    24. int t = q.front();
    25. ans.push(t);
    26. q.pop();
    27. for(int i = h[t]; i != -1; i = ne[i])
    28. {
    29. int j = e[i];
    30. ind[j] --;
    31. if(!ind[j])
    32. {
    33. q.push(j);
    34. }
    35. }
    36. }
    37. }
    38. int main()
    39. {
    40. cin >> n;
    41. memset(h, -1, sizeof h);
    42. for(int i = 1; i <= n; i ++ )
    43. {
    44. int x;
    45. while(cin >> x)
    46. {
    47. if(x == 0) break;
    48. else
    49. {
    50. add(i, x);
    51. ind[x] ++;
    52. }
    53. }
    54. }
    55. topo();
    56. while(ans.size())
    57. {
    58. cout << ans.front() << ' ';
    59. ans.pop();
    60. }
    61. return 0;
    62. }

    家谱 - 洛谷

    也算是模板题吧,就是用map映射string数组

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. map p;
    6. string find(string x)
    7. {
    8. if(x != p[x]) p[x] = find(p[x]);
    9. return p[x];
    10. }
    11. int main()
    12. {
    13. char op;
    14. string a;
    15. while(cin >> op)
    16. {
    17. if(op == '$') break;
    18. else if(op == '#')
    19. {
    20. cin >> a;
    21. if(p[a] == "") p[a] = a;
    22. }
    23. else if(op == '+')
    24. {
    25. string b;
    26. cin >> b;
    27. p[b] = find(a);
    28. }
    29. else
    30. {
    31. string c;
    32. cin >> c;
    33. cout << c << ' ' << find(c) << endl;
    34. }
    35. }
    36. return 0;
    37. }

  • 相关阅读:
    linux驱动程序40:页内存分配
    队列的链表实现 题目(难度1/10)
    VMware启用共享文件夹
    Java最新面试宝典 Spring面试题
    环形单链表问题
    C++学习——多态
    【Git】git仓库的 .git 下各个目录注释
    线程基础知识
    三分钟学会数据结构顺序表
    WIN10 驱动开发环境从0搭建 (驱动开发必看)
  • 原文地址:https://blog.csdn.net/qq_74593128/article/details/132887754
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号