Find them, Catch them

本文介绍了一种通过不完整信息判断两个犯罪分子是否属于同一帮派的算法。该算法使用并查集数据结构来处理帮派成员之间的关系,并能够根据输入消息实时更新帮派成员的归属情况。

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

  1. D [a] [b]
    where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

  2. A [a] [b]
    where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
    Input
    The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.
    Output
    For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”
    Sample Input
    1
    5 5
    A 1 2
    D 1 2
    A 1 2
    D 2 4
    A 1 4
    Sample Output
    Not sure yet.
    In different gangs.
    In the same gang.
    有两个黑帮,然后a操作问你x,y是否是一个黑帮里的,b操作代表x,y不在一个黑帮中。
    食物链的精简版,和祖先的关系只有2种状态了。随便怎么搞都可以。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1005;
int n,k,p[50005],r[50005],m;
void init()
{
    for(int i = 1;i <= n;i++)
    {
        p[i] = i;r[i] = 0;
    }
}
int find(int x)
{
    if(x != p[x])
    {
        int pp = find(p[x]);
        if(r[x] == r[p[x]])r[x] = 0;
        else r[x] = 1;
        p[x] = pp;
    }
    return p[x];
}
void union_set(int x,int y,int type)
{
    int fx,fy;
    fx = find(x);fy = find(y);
    if(type)
    {
    p[fy] = fx;
    if(r[fx] == r[fy])r[fx] = 0;
    else r[fx] = 1;
    }
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);getchar();
        init();
        while(m--)
        {
            char c;int a,b;
            scanf("%c%d%d",&c,&a,&b);getchar();
            if(c == 'D')
            {
                union_set(a,b,1);
            }
            else if(c == 'A')
            {
                union_set(a,b,0);
                int fa = find(a);int fb = find(b);
                if(fa != fb)
                    printf("Not sure yet.\n");
                else
                {
                    if(r[a] == r[b])
                        printf("In the same gang.\n");
                    else
                        printf("In different gangs.\n");
                }
            }
        }
    }
    return 0;
}
在处理C++中从网络接收的响应数据时,解析缓冲区并根据特定的命令结束标识(KEY_CMDEND)进行分块处理是一个常见的需求,尤其是在与硬件设备或协议交互时。以下是关于如何实现此功能的详细说明。 ### 数据解析与处理 在网络通信中,后端程序通常需要从缓冲区读取数据,并根据特定的分隔符或标识符进行分割和处理。例如,当接收到的数据流包含多个命令,每个命令以特定的字符串(如KEY_CMDEND)结束时,可以通过查找这些标识符来确定每个命令的边界,并逐一处理。 以下是一个基本的实现示例: ```cpp #include <iostream> #include <string> #include <vector> // 假设 KEY_CMDEND 是 "\r\n" const std::string KEY_CMDEND = "\r\n"; std::vector<std::string> parseResponseData(const std::string& buffer) { std::vector<std::string> commands; size_t start = 0; size_t end; // 循环查找每个命令的结束位置 while ((end = buffer.find(KEY_CMDEND, start)) != std::string::npos) { // 提取命令 std::string command = buffer.substr(start, end - start); if (!command.empty()) { commands.push_back(command); } // 更新起始位置 start = end + KEY_CMDEND.size(); } // 处理剩余的数据(如果有的话) if (start < buffer.size()) { commands.push_back(buffer.substr(start)); } return commands; } ``` ### 异常处理 在解析和处理数据时,可能会遇到各种异常情况,如网络中断、数据格式错误等。为了确保程序的健壮性,需要对这些情况进行处理。例如,可以使用try-catch块来捕获和处理异常: ```cpp #include <stdexcept> void handleEthernetEFEMMessage(const std::string& message) { try { // 假设 message 是从缓冲区提取的命令 if (message.find("COMMREADY") != std::string::npos) { // 处理 COMMREADY 消息 std::cout << "Received COMMREADY message: " << message << std::endl; } else { // 处理其他消息 std::cout << "Received unknown message: " << message << std::endl; } } catch (const std::exception& e) { // 处理异常 std::cerr << "Exception occurred: " << e.what() << std::endl; } } ``` ### 综合应用 将上述解析和异常处理逻辑结合起来,可以构建一个完整的数据处理流程: ```cpp int main() { // 模拟网络缓冲区 std::string buffer = "CMD1\r\nCMD2\r\nCOMMREADY\r\nCMD3\r\n"; // 解析缓冲区中的命令 std::vector<std::string> commands = parseResponseData(buffer); // 处理每个命令 for (const auto& command : commands) { handleEthernetEFEMMessage(command); } return 0; } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值