DSU stands for ‘decorate, sort, undecorate’ and refers to a pattern that is often useful for sorting lists according to some attribute of elements.

For example, if you have a dictionary that maps from mothers to lists of their children, you might want to sort the mothers by their number of children. Here is a function that does that:

The first loop assigns each mother to mother and each list of children to children. It builds a list of tuples. Where each tuple is the number of children and a mother. Sort compares the first element, number of children, first, and only considers the second element to break ties. The result of tuples sorted in increasing order by number of children.

The second loop traverses the list of tuples and builds a list of mothers, sorted by parity (which in this context means number of children).

This pattern is called ‘decorate, sort, undecorate’ because the first loop ‘decorates’ the list of mothers by pairing each mother with her parity, and the last loop ‘undecorate’ the sorted list by removing the parity information.

from Thinking in Python

DSU的更多相关文章

  1. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  2. Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)

    Socks Problem Description: Arseniy is already grown-up and independent. His mother decided to leave ...

  3. dsu + lca

    贴一下使用dsu和lca的代码,dsu的代码很简单,可以马上写出来,但是lca的代码就不熟练了.这里lca的计算还是用了dfs的访问时间标记,我想起来割边, 割点的判断, dfu[u], low[u] ...

  4. CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]

    D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...

  5. CF 570D. Tree Requests [dsu on tree]

    传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...

  6. [dsu on tree]【学习笔记】

    十几天前看到zyf2000发过关于这个的题目的Blog, 今天终于去学习了一下 Codeforces原文链接 dsu on tree 简介 我也不清楚dsu是什么的英文缩写... 就像是树上的启发式合 ...

  7. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  8. dsu on tree 树上启发式合并 学习笔记

    近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...

  9. UOJ#266. 【清华集训2016】Alice和Bob又在玩游戏 博弈,DSU on Tree,Trie

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ266.html 题解 首先我们可以直接暴力 $O(n^2)$ 用 sg 函数来算答案. 对于一个树就是枚举 ...

  10. dsu on tree入门

    先瞎扯几句 说起来我跟这个算法好像还有很深的渊源呢qwq.当时在学业水平考试的考场上,题目都做完了不会做,于是开始xjb出题.突然我想到这么一个题 看起来好像很可做的样子,然而直到考试完我都只想出来一 ...

随机推荐

  1. Servlet体验之旅(二)——Session、Cookie

    我们知道Session和Cookie都是用于会话跟踪的,仅仅是实现的方式不大一样,那么他们到底有什么不同呢?以下跟着我脚步来了解一下: Session.Cookie的含义: Session 一种ser ...

  2. Android DatePickerDialog样式不一致的问题

    三星和华为的平板上,DatePickerDialog的显示样式不一致.三星的仅仅显示月日年选择框,而华为的平板上另外还显示了日历表.代码同样. 可能是系统控件做了部分改动,后来你发现是能够设置的: D ...

  3. hdoj 4548 美素数 【打表】

    另类打表:将从1到n的满足美素数条件的数目赋值给prime[n],这样最后仅仅须要用prime[L]减去prime[R-1]就可以: 美素数 Time Limit: 3000/1000 MS (Jav ...

  4. 单片机: EEPROM和串口通信

    名称:IIC协议 EEPROM24c02 通过串口通信存数读取数据 内容:此程序用于检測EEPROM性能,測试方法例如以下:写入24c02一个数据,然后在内存中改变这些数据. 掉电后主内存将失去这些信 ...

  5. centos6高速部署java应用

    眼下提供IDC服务的厂商真的是五花八门,可是更正服务到位的却为数不多,搞得比較好的应该是阿里云.天成.51idc,出于时间考虑还是建议选用windows,至少安装开发环境会方便得多,不会耗费太长时间. ...

  6. ES shard unassigned的解决方法汇总

    说下shard出现的几个状态说明: relocating_shards shows the number of shards that are currently moving from one no ...

  7. POJ 3694 Network(Tarjan求割边+LCA)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10969   Accepted: 4096 Descript ...

  8. jqGrid系列知识

    1.获取选中到行的ID var rowKey = jQuery(grid_selector).getGridParam("selrow"); 2.获取选中行除ID之外的数据 var ...

  9. 给DOM操作生成的元素添加事件

    问题:通过js给页面添加新元素,并给该元素添加绑定事件,但新添加的元素上却没有绑定任何事件. 常见例子:在处理表格的时候,每行行末有个删除按钮,如下图.点击删除按钮的时候删除这一行. //html部分 ...

  10. 路飞学城Python-Day2

    13.变量的定义规范 变量的含义:变量就是定义之后还能发生改变,可以重新赋值的量;变量的定义规范:名字不能随便起,声明一个变量,name = "123"[变量 = 值]变量定义规则 ...