HDU4496_D-City(并查集删边/逆向)

探讨了在一个给定的图中,通过按顺序删除边来计算剩余连通块数量的问题,并提供了一种使用并查集实现的有效算法。

D-City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1315    Accepted Submission(s): 496


Problem Description
Luxer is a really bad guy. He destroys everything he met. 
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input. 
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
 

Input
First line of the input contains two integers N and M. 
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line. 
Constraints: 
0 < N <= 10000 
0 < M <= 100000 
0 <= u, v < N. 
 

Output
Output M lines, the ith line is the answer after deleting the first i edges in the input.
 

Sample Input
  
5 10 0 1 1 2 1 3 1 4 0 2 2 3 0 4 0 3 3 4 2 4
 

Sample Output
  
1 1 1 2 2 2 2 3 4 5
Hint
The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there's only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.
 

Source
 
解题报告
给定一个图,N个点,M条边,求按顺序删边后剩下的多少联通块。
一般我们都是用并查集来加边,删边也可以用并查集,删除第1条边等于加上m-1条边。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 100000+10
#define N 10000+10
using namespace std;
int n,m;
struct node
{
    int u,v;
} edge[M];
int B[N],ans[M];
int fine(int x)
{
    if(B[x]!=x)
    B[x]=fine(B[x]);
    return B[x];
}
int main()
{
    int i;
    while(~scanf("%d%d",&n,&m))
    {
    memset(ans,0,sizeof(ans));
    memset(edge,0,sizeof(edge));
        for(i=0;i<n;i++)
        B[i]=i;
        for(i=0; i<m; i++)
        {
            scanf("%d%d",&edge[i].u,&edge[i].v);
        }
        int sum=n;
        for(i=m-1;i>=0;i--)
        {
            ans[i]=sum;
            int xx=fine(edge[i].u);
            int yy=fine(edge[i].v);
            if(xx!=yy)
            {
                sum--;
                B[xx]=yy;
            }
        }
        for(i=0;i<m;i++)
        printf("%d\n",ans[i]);
    }
    return 0;
}


<think>我们正在解决一个关于Hi3861开发板在OpenHarmony环境中编译MQTT相关示例时出现的文件路径错误问题。用户提到了几个关键路径:hi3861_hdu_iot_application,paho_mqtt,MQTTPacket,MQTTClient-C,ohos。我们需要根据引用内容来定位问题。根据引用[3],我们得知在Hi3861的示例程序中,有一个features列表,其中包含MQTT相关的示例(如"26_paho.mqtt.embedded-c:app")。用户可能是在编译这个示例时遇到了文件路径错误。常见问题(引用[1])包括:1.Sources源文件路径不正确2.Include_dirs头文件路径不正确3.JSON语法错误(如缺少逗号)4.注释处理不当因此,我们需要检查与MQTT示例相关的BUILD.gn文件,确保源文件和头文件路径正确,并且没有语法错误。步骤:1.确认MQTT库是否已经正确放置在vendor目录下。通常,第三方库(如paho.mqtt.embedded-c)应该放在vendor的某个子目录中。2.检查示例程序的BUILD.gn文件(如demo/BUILD.gn)中是否正确定义了源文件和头文件路径。3.检查依赖的库(如MQTTPacket、MQTTClient-C)是否在项目中存在,并且路径正确。具体到用户的问题,错误可能是由于在编译26号示例时,系统找不到paho.mqtt.embedded-c库的相关文件。我们需要查看该示例的构建脚本。根据引用[3]中的示例,我们注意到在demo/BUILD.gn中,每个feature对应一个示例程序。因此,26号示例对应的构建目标在features中为"26_paho.mqtt.embedded-c:app"。这个目标应该对应一个名为app.gn的文件,位于26_paho.mqtt.embedded-c目录下。我们需要检查这个app.gn文件中的路径设置。假设项目结构如下:```vendor└──hihope└──hispark_pegasus└──demo├──26_paho.mqtt.embedded-c│├──app.gn│└──...(源文件)└──BUILD.gn```在app.gn中,应该定义了源文件路径和头文件路径。例如:```python#示例app.gn内容import("//build/lite/config/component/lite_component.gni")executable("app"){output_name="app"sources=["paho_mqtt_demo.c",#其他源文件]include_dirs=["//vendor/hihope/hispark_pegasus/demo/26_paho.mqtt.embedded-c",#MQTT库的头文件路径"//vendor/paho.mqtt.embedded-c/MQTTPacket/src","//vendor/paho.mqtt.embedded-c/MQTTClient-C/src",]deps=["//vendor/paho.mqtt.embedded-c:MQTTPacket","//vendor/paho.mqtt.embedded-c:MQTTClient-C",]cflags=["-Wno-unused-variable"]}```同时,在vendor/paho.mqtt.embedded-c目录下,应该有两个库的构建目标(BUILD.gn文件),例如:```python#vendor/paho.mqtt.embedded-c/BUILD.gnstatic_library("MQTTPacket"){sources=["MQTTPacket/src/MQTTConnectClient.c","MQTTPacket/src/MQTTConnectServer.c",...#其他源文件]include_dirs=["MQTTPacket/src"]}static_library("MQTTClient-C"){sources=["MQTTClient-C/src/MQTTClient.c"]include_dirs=["MQTTPacket/src",#依赖MQTTPacket的头文件"MQTTClient-C/src"]deps=[":MQTTPacket"]}```解决方案:1.确认paho.mqtt.embedded-c库已经正确下载并放置在vendor目录下(可能是vendor/paho.mqtt.embedded-c)。2.检查26_paho.mqtt.embedded-c示例的app.gn文件中的include_dirs和deps是否指向正确的路径。3.检查paho.mqtt.embedded-c库的BUILD.gn文件是否存在,且语法正确(如逗号、括号等)。如果用户遇到找不到文件的问题,可能是路径大小写不一致,或者路径层级不对。需要确保在app.gn中引用的路径与实际目录结构一致。另外,根据引用[1],还要检查JSON配置(如果有)和注释符号的使用。如果用户是在修改后的代码中出现问题,请确保在BUILD.gn中取消注释了MQTT示例(即去掉26号前面的#号)。如果问题仍然存在,可以尝试以下步骤:-检查整个项目的目录结构,确保MQTT库的源文件和头文件在正确的位置。-查看编译错误信息,确定是哪个文件找不到,然后根据错误信息调整路径。注意:在OpenHarmony的构建系统中,路径通常从根目录开始(//表示根目录),因此要确保路径从根目录开始正确。例如,如果paho.mqtt.embedded-c库放在vendor目录下,则路径应为:`"//vendor/paho.mqtt.embedded-c/MQTTPacket/src"`。如果用户将库放在其他地方,则需要相应调整路径。最后,如果用户使用的不是hihope的示例,而是自己的项目,那么需要在自己的BUILD.gn中正确配置这些路径。希望这些步骤能帮助用户解决问题。</think>针对Hi3861开发板在编译MQTT相关库时出现的文件路径错误问题,结合引用内容和OpenHarmony开发经验,解决方案如下: ### 问题分析 根据引用[3],MQTT示例(`26_paho.mqtt.embedded-c:app`)在`BUILD.gn`中属于可选编译项。路径错误通常由以下原因导致: 1. **头文件/源文件路径未正确配置**(引用[1]) 2. **MQTT库未正确集成到工程中** 3. **GN构建脚本语法错误**(如缺少逗号或括号) ### 解决方案 #### 步骤1:验证MQTT库路径 1. 确认`paho_mqtt`库已下载并放置在正确目录: ```bash # 标准目录结构参考 vendor/ └── hihope/ └── hispark_pegasus/ ├── demo/ │ └── 26_paho.mqtt.embedded-c/ # MQTT示例目录 └── third_party/ └── paho.mqtt.embedded-c/ # MQTT库源码 ``` 若缺失库文件,需从[官方仓库](https://github.com/eclipse/paho.mqtt.embedded-c)克隆: ```bash git clone https://github.com/eclipse/paho.mqtt.embedded-c vendor/third_party/paho.mqtt.embedded-c ``` #### 步骤2:修正GN构建脚本 在`demo/BUILD.gn`中(引用[3]): ```python lite_component("demo") { features = [ # 启用MQTT示例(移除行首#号) "26_paho.mqtt.embedded-c:app", ] } ``` 在`26_paho.mqtt.embedded-c/app.gn`中检查路径: ```python executable("app") { include_dirs = [ // 添加MQTT库头文件路径 "//vendor/third_party/paho.mqtt.embedded-c/MQTTPacket/src", "//vendor/third_party/paho.mqtt.embedded-c/MQTTClient-C/src", ] sources = [ "mqtt_demo.c", // 添加MQTT库源文件 "//vendor/third_party/paho.mqtt.embedded-c/MQTTPacket/src/*.c", "//vendor/third_party/paho.mqtt.embedded-c/MQTTClient-C/src/*.c" ] # 注意:路径分隔符使用正斜杠/,每行末尾加逗号 } ``` #### 步骤3:检查JSON配置 在`config.json`中确认MQTT组件已声明: ```json { "subsystem": "iot", "components": [ { "component": "paho_mqtt", "features": [] } ] } ``` #### 步骤4:编译验证 执行编译命令,观察输出: ```bash hb build -f ``` 若仍有路径错误,根据报错信息调整`include_dirs`和`sources`中的路径深度。 ### 关键检查点 1. **路径大小写敏感**:Linux系统严格区分大小写,确保`MQTTPacket`等名称拼写准确 2. **依赖库完整性**:检查`third_party/paho.mqtt.embedded-c`是否包含`src`目录 3. **GN语法验证**:所有数组元素以逗号结尾,字符串用双引号包裹(引用[1]) ### 注意事项 - 鸿蒙编译系统要求**绝对路径**(以`//`开头) - 若使用自定义路径,需在`build/lite/config/subsystem/iot.gni`中注册新路径 - 网络功能需提前配置WiFi连接(引用[3]中`19_wificonnect`示例) ### 相关问题 1. Hi3861开发中如何正确配置第三方库的GN构建脚本? 2. OpenHarmony编译出现`undefined reference to`错误应如何排查?[^1] 3. 如何为Hi3861移植新的物联网协议库(如CoAP)? 4. MQTT连接失败时有哪些调试手段? [^1]: 引用自站内文档《FS_Hi3861鸿蒙开发板编译问题排查》 [^2]: 引用自Hi3861芯片规格说明 [^3]: 引用自《Hi3861 OpenHarmony嵌入式应用入门》环境配置示例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值