UVA 699 The Falling Leaves

本文介绍了解决UVA699TheFallingLeaves问题的方法,该问题是关于计算二叉树中各位置落叶堆大小的问题。通过递归前序遍历的方式,实现了对二叉树节点的处理,并记录每条垂直线上叶子的数量。

UVA 699 The Falling Leaves

Time Limited:3seconds

Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?


We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the "leaves" drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

Input:

The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value -1 is supplied. Thus the tree shown above is specified as 5 7 -1 6 -1 -1 3 -1 -1. Each actual tree node contains a positive, non-zero value. The last test case is followed by a single -1 (which would otherwise represent an empty tree).

Output:

For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of "leaves" in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the examples below.

Sample Input

5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1
Sample Output
Case 1:
7 11 3

Case 2:
9 7 21 15

#include <stdio.h>
int b[1000];
int pre( int i, int a )
{
    b[i] += a;
    int c;
    scanf("%d", &c);
    if( c != -1 )
        pre( i-1, c );
    scanf("%d",&c);
    if( c != -1 )
        pre( i+1, c );
    return 0;
}
int main()
{
    int a = 0;
    int n = 0;
    int i = 500;
    while( scanf("%d", &a) )
    {
           if( a == -1)
              break;
           n++;
           for( int t=0; t<1000; t++ )
                b[t] = 0;
           pre( i, a );
           printf("Case %d:\n",n);
           for( int t=0; t<1000; t++ )
           {
                if( b[t] != 0 )
                {
                    if( b[t+1] != 0 )
                        printf("%d ",b[t]);
                    else
                        printf("%d\n",b[t]);
                }
           }     
    }
    return 0;
}
剛看到這道題似乎需要建樹來處理,但其實仔細分析就會發現其實只是preorder的traversal罷了,一個recursion function就可以解決了



                
<think> 我们正在分析I3C总线协议中关于数据发送的时序要求。用户引用的句子是:"If the current bit to transmit is a 0, then the Device shall drive SDA Low after the falling edge of SCL and hold Low until the next falling edge of SCL" 这句话描述的是在I3C总线上发送逻辑'0'时的精确时序要求。让我们分解这个句子: 1. "current bit to transmit is a 0":当前要发送的位是逻辑0。 2. "drive SDA Low after the falling edge of SCL":在SCL(时钟线)的下降沿之后将SDA(数据线)驱动为低电平。 3. "hold Low until the next falling edge of SCL":保持低电平直到下一个SCL的下降沿。 在I3C协议中,数据的变化通常发生在时钟的边沿附近。注意这里的关键时间点是SCL的下降沿。 我们可以用时序图来理解: ```mermaid timeline title I3C发送逻辑'0'的时序 section SCL Falling Edge 1 : 0 Rising Edge : 1 Falling Edge 2 : 0 section SDA (发送0)Falling Edge 1之后: 变为低电平 在Falling Edge 2之前: 保持低电平 在Falling Edge 2之后: 可以改变(取决于下一位数据) ``` 详细步骤: 1. 当设备准备发送一个比特0时,它不会在SCL为高电平时改变SDA(因为高电平期间是数据采样期)。 2. 在SCL的下降沿(标志着前一个比特周期的结束)之后,设备将SDA线拉低。 3. 然后,设备保持SDA为低电平,直到下一个SCL下降沿(即当前比特周期的结束)。 为什么这样设计? - 在I3C中,数据在SCL的上升沿被采样。因此,在SCL高电平期间,数据必须保持稳定。而改变数据只能在SCL低电平期间进行。 - 发送0时,设备在SCL下降沿之后(即进入低电平期)立即将SDA拉低,并保持整个高电平期稳定为低,直到下一个下降沿。这样确保在SCL上升沿时,SDA是稳定的低电平,从而被正确采样为0。 数学表示: 设$t_{fe}$为SCL下降沿的时刻,则对于要发送的0比特: $$ \text{SDA}(t) = \begin{cases} \text{Low} & \text{for } t \in (t_{fe}^{(n)} + t_{hd}, t_{fe}^{(n+1)}] \\ \text{可能改变} & \text{at } t > t_{fe}^{(n+1)} \end{cases} $$ 其中: - $t_{fe}^{(n)}$是当前比特周期开始的下降沿(即前一个比特结束的下降沿) - $t_{fe}^{(n+1)}$是当前比特周期结束的下降沿(即下一个比特开始的下降沿) - $t_{hd}$是规范要求的最小保持时间(通常在下降沿后立即生效,但可能有极短的延迟要求) 注意:这里的时间间隔是开区间,因为下降沿瞬间的变化需要满足建立和保持时间。 与发送1的区别: 如果是发送1,设备在SCL下降沿之后不会驱动SDA(即释放总线,由上拉电阻拉高),但同样要保证在SCL高电平期间SDA稳定为高。 参考I2C和I3C的差异: 在I2C中,数据同样在SCL高电平期间保持稳定,变化发生在低电平期间。但是I3C的速度更快,时序要求更严格。 引用中提到I2C的典型事务(引用[1]),但注意我们现在讨论的是I3C,尽管两者有相似之处,但I3C的时序要求更精确。 此外,在引用[2]和[3]中提到了Camera的电源管理和DSI屏的初始化,虽然与总线操作有关,但并不直接关联到当前问题。 因此,这句话的核心含义是规定了在I3C总线上发送比特0时,控制SDA线的精确时间窗口:从当前比特周期开始的下降沿之后,直到下一个下降沿,都必须保持低电平。 相关问题: 1. I3C协议中,发送比特1时SDA线应该如何操作? 2. 如果发送比特0时,没有在规定的时间窗口内将SDA拉低,会导致什么问题? 3. I3C的时序参数中,建立时间(setup time)和保持时间(hold time)如何影响这个操作? 4. 在I3C多主机系统中,这个发送0的时序要求如何与总线仲裁机制配合? 5. 与I2C相比,I3C在发送数据的时序要求上有哪些改进以适应更高的速度? 注意:用户提供的引用内容中,引用[1]是关于I2C的,而I3C是改进的协议,因此我们在解释时以I3C规范为准。 </think> ### I3C总线发送逻辑'0'的时序规范深度解析 #### 1. **核心时序要求分解** 该规范定义了发送逻辑'0'时的精确操作时序: $$\begin{cases} \text{触发条件} & : \text{SCL下降沿} \\ \text{动作起点} & : \text{SCL下降沿后立即驱动SDA为低} \\ \text{保持时间} & : \text{持续到下一个SCL下降沿} \end{cases}$$ 其时序关系可表示为: ```mermaid timeline title SDA发送逻辑'0'的精确时序 section SCL时钟 Fall Edge 1 : 0 Rise Edge : 1 Fall Edge 2 : 0 section SDA操作 After Falling Edge 1 : 拉低SDA During High Period : 保持低电平 Until Falling Edge 2 : 维持不变 ``` #### 2. **物理层实现原理** - **电气特性**: $V_{\text{SDA}} < 0.3 \times V_{\text{DD}}$ 确保可靠低电平识别 - **驱动电路**: 使用开漏输出结构满足"线与"逻辑: $$ \text{SDA} = \bigcap_{i=1}^{n} \text{Driver}_i(\text{SDA}) $$ #### 3. **与逻辑'1'发送的对比** | 参数 | 发送逻辑'0' | 发送逻辑'1' | |--------------|--------------------------------|--------------------------------| | 动作时机 | SCL下降沿**后**立即拉低 | SCL下降沿**后**释放总线 | | 电平保持 | 主动驱动低电平 | 依赖上拉电阻拉高 | | 冲突检测点 | 下一个SCL上升沿验证 | 下一个SCL上升沿验证 | #### 4. **时序约束分析** 规范隐含的关键时序参数: $$ t_{\text{HD-DAT}} \geq 0 \text{ (数据保持时间)} $$ $$ t_{\text{SU-DAT}} > \frac{1}{2} t_{\text{SCL}} \text{ (数据建立时间)} $$ 式中$t_{\text{SCL}}$为时钟周期,确保在SCL上升沿采样时数据稳定[^1]。 #### 5. **驱动电路状态机** ```mermaid stateDiagram-v2 [*] --> SCL_High SCL_High --> Drive_Low: SCL下降沿 AND 需发送0 Drive_Low --> Hold_Low: 驱动成功 Hold_Low --> [*]: 下一个SCL下降沿 Hold_Low --> Release: 需发送1(下一周期) ``` #### 6. **实际应用场景** 在Camera传感器控制中(如引用[2]的CCI总线): 1. `CameraSensorI2C_PowerUp()`使能时钟 2. 发送控制命令时遵循此时序规则 3. 例如传输寄存器地址`0x34`(二进制`00110100`) - 第3位(0) :SCL下降沿后拉低SDA - 第5位(1) :SCL下降沿后释放SDA #### 7. **违规后果** - **建立时间不足**:主设备在SCL上升沿采样到亚稳态 - **保持时间不足**:从设备采样错误(尤其高速模式) - **总线冲突**:多主竞争时仲裁失败概率升高 ### 相关问题 1. I3C协议中发送逻辑'1'的完整时序要求是什么? 2. 如何计算$t_{\text{HD-DAT}}$的最小值以保证可靠通信? 3. I3C的SDA驱动电路与I2C有何本质区别? 4. 在多主设备系统中,此时序规则如何影响总线仲裁机制? 5. 当检测到时序违规时,I3C控制器通常采取哪些恢复措施?[^2][^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值