Description Bobo 有 n 个列表 L1, L2, …, Ln. 初始时,Li 仅包含元素 i, 即 Li = [i]. 他依次执行了 m 次操作。第 i 次操作由两个整数 ai, bi 指定, 每次操作分为两步: Lai ← reverse(Lai + Lbi), 其中 ← 表示赋值,+ 表示列表的连接,reverse 表示列表的反转。例如,reverse([1, 2] + [3, 4, 5]) = [5, 4, 3, 2, 1]. Lbi ← []. 其中 [] 表示空的列表。 输出 m 次操作后, L1 的元素。 Input 输入文件包含多组数据,请处理到文件结束。 每组数据的第一行包含两个整数 n 和 m. 接下来 m 行,其中第 i 行包含 2 个整数 ai, bi. 1 ≤ n, m ≤ 105 1 ≤ ai, bi ≤ n, ai ≠ bi n 的总和,m 的总和都不超过 5 × 105. Output 对于每组数据,先输出 L1 的长度 |L1|,再输出 |L1| 个整数,表示 L1 的元素。 Sample Input 2 1 1 2 2 1 2 1 3 3 3 2 3 2 1 3 Sample Output 2 2 1 0 3 2 3 1
时间: 2023-05-24 13:04:06 浏览: 68
解题思路:
首先,分析题目可以得出,每个操作都只涉及到两个列表,因为只有两个参数,且每次清空 Lbi。因此,我们可以使用邻接表来存储这些列表之间的关系。
其中,每个邻接表中存储了与该节点相邻的节点(即执行过操作的节点)。邻接表可以用 unordered_map 来实现,键为节点编号,值为 vector<int> 类型的邻接表。
那么,如何处理这些操作呢?对于一次操作,我们只需将 La 和 Lb 反转,然后将反转后的 La 指向 Lb,将 Lb 置为空即可。反转可以用 reverse() 函数。
最后,我们只需从 L1 开始,依次遍历相邻节点,即可得出 L1 的元素。
代码实现:
相关问题
headers = [i[0] for i in cursor.description]
这行代码是用来获取数据库查询结果的列名(字段名)的,其中 cursor 是数据库查询的游标对象,description 是游标对象的一个属性,包括查询结果的描述信息,包括列名、数据类型、字符集等。通过遍历 description 中的每个元素,获取每个元素的第一个值,也就是列名,组成一个列表返回。这个列表就是查询结果的列名。
帮我优化这段代码 handleWorkOrderMessage() { // 待办理、待审核二级弹窗 console.log(this.pWorkOrder.orderState); if (this.pWorkOrder.orderState === '20') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,下单!'; } // 接单 if (this.pWorkOrder.orderState === '30') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,接单!'; } // 反馈 if (this.pWorkOrder.orderState === '40') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.popupSitch.image.label = '反馈图片'; this.popupSitch.image.show = true; this.description = '按要求完成,反馈!'; } // 审核关闭 if (this.pWorkOrder.orderState === '50') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,审核关闭!'; } this.$refs.orderPopup.open(); },
可以尝试以下优化:
1. 将重复的代码提取出来,避免重复写相同的语句。例如,多个条件下都需要设置 `popupSitch.description.show` 为 `true`,可以在代码开头先设置为 `false`,然后根据条件进行修改。
2. 使用 switch 语句替代多个 if 语句,可以让代码更加清晰易懂。
3. 如果有多处使用的字符串,可以将其提取为常量或者变量,方便修改和管理。
下面是优化后的代码示例:
```
handleWorkOrderMessage() {
const DES_LABEL = '经办意见';
const DESC_COMPLETE = '按要求完成,';
const DESC_ORDER = '下单!';
const DESC_ACCEPT = '接单!';
const DESC_FEEDBACK = '反馈!';
const DESC_CLOSE = '审核关闭!';
this.popupSitch = {
description: {
show: false,
label: ''
},
image: {
show: false,
label: ''
}
};
switch (this.pWorkOrder.orderState) {
case '20':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.description = DESC_COMPLETE + DESC_ORDER;
break;
case '30':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.description = DESC_COMPLETE + DESC_ACCEPT;
break;
case '40':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.popupSitch.image.show = true;
this.popupSitch.image.label = '反馈图片';
this.description = DESC_COMPLETE + DESC_FEEDBACK;
break;
case '50':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.description = DESC_COMPLETE + DESC_CLOSE;
break;
default:
break;
}
this.$refs.orderPopup.open();
},
```