We are a team - 华为OD统一考试

news/2024/7/20 18:04:47 标签: 华为od, 算法, python, java, c++, 面试

OD统一考试

题解: Java / Python / C++

alt

题目描述

总共有 n 个人在机房,每个人有一个标号 (1<=标号<=n) ,他们分成了多个团队,需要你根据收到的 m 条消息判定指定的两个人是否在一个团队中,具体的:

  1. 消息构成为 a b c,整数 a、b 分别代表两个人的标号,整数 c 代表指令。
  2. c== 0 代表a和b在一个团队内。
  3. c == 1 代表需要判定 a 和b 的关系,如果 a和b是一个团队,输出一行"we are a team",如果不是,输出一行"we are not a team"。
  4. c 为其他值,或当前行a或b 超出 1~n 的范围,输出 “da pian zi”。

输入描述

  1. 第一行包含两个整数 n,m(1<=n.m<=100000).分别表示有n个人和 m 条消息。
  2. 随后的 m 行,每行一条消息,消息格式为: a b c (1<=a,b<=n, 0<=c<=1)

输出描述

  1. c ==1.根据 a 和 b 是否在一个团队中输出一行字符串,在一个团队中输出 “we are a team”, 不在一个团队中输出 “we are not a team”。
  2. c 为其他值,或当前行 a 或 b 的标号小于 1 或者大于 n 时,输出字符串 “da pian zi”。
  3. 如果第一行 n 和 m的值超出约定的范围时,输出字符串"NULL"。

示例1

输入
5 6
1 2 0
1 2 1
1 5 0
2 3 1
2 5 1
1 3 2

输出
we are a team
we are not a team
we are a team
da pian zi

题解

并查集的简单模板套用

如果对并查集不会,可以通过 https://zhuanlan.zhihu.com/p/93647900 来学习。

Java

java">import java.util.Scanner;
public class Main {

    private static boolean checkRange(int a, int b, int c) {
        return 1 <= a && a <= 100000 && 1 <= b && b <= 100000 && 0 <= c && c <= 1;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(), m = scanner.nextInt();
        if (!checkRange(n, m, 0)) {
            System.out.println("NULL");
            return;
        }

        UnionFind uf = new UnionFind(n);
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
            if (checkRange(a, b, c)) {
                if (c == 0) {
                    uf.merge(a, b);
                } else if (uf.find(a) == uf.find(b)) {
                    System.out.println("we are a team");
                } else {
                    System.out.println("we are not a team");
                }
            } else {
                System.out.println("da pian zi");
            }
        }
    }

}

/**
 * 并查集
 *
 * @Description: 学习参考: https://zhuanlan.zhihu.com/p/93647900
 * @Author code5bug
 * @Date 20-10-22
 * @Version 1.0
 **/
class UnionFind {
    // father[2] = 3 表示元素2的父节点是3
    public int[] father;

    public UnionFind(int len) {
        father = new int[len + 1];
        for (int i = 1; i <= len; i++) {
            father[i] = i;
        }
    }

    // 查询 x 的根节点
    public int find(int x) {
        if (x < 0 || x >= father.length) {
            throw new RuntimeException("查询越界");
        }

        // 合并(路径压缩)
        return (x == father[x] ? x : (father[x] = find(father[x])));
    }

    // 合并节点, y 的根节点指向 x 的根节点
    public void merge(int x, int y) {
        int xRoot = find(x), yRoot = find(y);
        father[yRoot] = xRoot;
    }
}

Python

python">n, m = map(int, input().split())


def check_range(a: int, b: int, c=0) -> bool:
    return 1 <= a <= 100000 and 1 <= b <= 100000 and 0 <= c <= 1


if check_range(n, m):
    fa = [i for i in range(n + 1)]

    def find(x: int) -> int:
        if fa[x] != x:
            fa[x] = find(fa[x])
        return fa[x]

    def merge(x: int, y: int):
        x_root, y_root = find(x), find(y)
        fa[x_root] = y_root

    for _ in range(m):
        a, b, c = map(int, input().split())
        if check_range(a, b, c):
            if c == 0:
                merge(a, b)
            elif find(a) == find(b):
                print("we are a team")
            else:
                print("we are not a team")
        else:
            print("da pian zi")
else:
    print("NULL")

C++

#include <iostream>
#include <vector>

using namespace std;

bool check_range(int a, int b, int c = 0) {
    if(a < 1 || b < 1 || c < 0) return false;
    if(a > 100000 || b > 100000 || c > 1) return false;
    return true;
}


int find(vector<int>& fa, int x) {
    if(fa[x] != x) {
        fa[x] = find(fa, fa[x]);
    }
    return fa[x];
}

int merge(vector<int>& fa, int x, int y) {
    int xRoot = find(fa, x), yRoot = find(fa, y);
    fa[xRoot] = yRoot;
}

int main() {
    int n, m;
    cin >> n >> m;

    if(!check_range(n, m)) {
        cout << "NULL" << endl;
        return -1;
    }

    vector<int> fa(n+1);
    for(int i=0; i<=n; i++) fa[i] = i;

    for(int i=0, a, b, c; i<m; i++) {
        cin >> a >> b >> c;
        if(check_range(a, b, c)) {
            if(c == 0) {
                merge(fa, a, b);
            } else if(find(fa, a) == find(fa, b)) {
                cout << "we are a team" << endl;
            } else {
                cout << "we are not a team" << endl;
            }
        } else {
            cout << "da pian zi" << endl;
        }
    }

    return 0;
}

(并查集)相关练习题

题号题目难易
LeetCode 12021202. 交换字符串中的元素中等
LeetCode 17221722. 执行交换操作后的最小汉明距离中等
LeetCode 947947. 移除最多的同行或同列石头中等
LeetCode 924924. 尽量减少恶意软件的传播困难

🙏整理题解不易, 如果有帮助到您,请给点个赞 ‍❤️‍ 和收藏 ⭐,让更多的人看到。🙏🙏🙏


http://www.niftyadmin.cn/n/5292366.html

相关文章

推三返一模式:打破常规,裂变矩阵!

推三返一的商业逻辑 推三返一模式是一种常见的营销策略&#xff0c;通过客户推荐的方式来实现商业拓展。该模式的核心理念是&#xff0c;当客户向自己的社交圈推荐某个产品或服务时&#xff0c;商家会给予客户一定的奖励或回馈&#xff0c;以此激励客户更积极地推广产品。 在推…

test perf-01-性能测试之 JMeter

JMeter Apache JMeter 可以用于测试静态和动态资源(Web动态应用程序)的性能。 它可以用于模拟服务器、服务器组、网络或对象上的负载&#xff0c;以测试其强度或分析不同负载类型下的总体性能。 JMeter Tutorial 作用 Apache JMeter可以用于测试静态和动态资源(Web动态应用程…

第三方软件测试公司有哪些服务形式?如何收费?

由于软件企业的增多&#xff0c;企业更加注重软件开发&#xff0c;因此会将软件测试工作交由第三方软件测试公司进行。第三方软件测试公司也就是专门做软件测评的外包公司&#xff0c;主要是发现软件漏洞和缺陷以便公正、客观评估软件质量&#xff0c;再出具一份软件测试报告。…

深信服AF防火墙配置SSL VPN

防火墙版本&#xff1a;8.0.85 需提前确认防火墙是是否有SSL VPN的授权&#xff0c;确认授权用户数量 1、确认内外网接口划分 2、网络→SSL VPN&#xff0c;选择内外网接口地址 3、SSL VPN→用户管理→新增一个SSL VPN的用户 4、新增L3VPN资源&#xff0c;类型选择Other&…

Mybatis枚举类型处理和类型处理器

专栏精选 引入Mybatis Mybatis的快速入门 Mybatis的增删改查扩展功能说明 mapper映射的参数和结果 Mybatis复杂类型的结果映射 Mybatis基于注解的结果映射 文章目录 专栏精选摘要引言正文枚举类型映射简单枚举映射枚举顺序映射复杂枚举映射 类型处理器 总结 摘要 在这篇…

算法系统学习(持续更新)

引言 首先说说为什么要写这个博客呢&#xff0c;我也是想在学习算法中能与大家一起分享&#xff0c;一起进步&#xff0c;同时把学到的东西能写出来&#xff0c;写清楚&#xff0c;也是对知识一种巩固。 算法目录 1.双指针&#xff08;8道习题&#xff09; 2.滑动窗口&…

C语言——指针题目“指针探测器“

如果你觉得你指针学的自我感觉良好&#xff0c;甚至已经到达了炉火纯青的地步&#xff0c;不妨来试试这道题目&#xff1f; #include<stdio.h> int main() {char* c[] { "ENTER","NEW","POINT","FIRST" };char** cp[] { c 3…

大数据前馈神经网络解密:深入理解人工智能的基石

文章目录 大数据前馈神经网络解密&#xff1a;深入理解人工智能的基石一、前馈神经网络概述什么是前馈神经网络前馈神经网络的工作原理应用场景及优缺点 二、前馈神经网络的基本结构输入层、隐藏层和输出层激活函数的选择与作用网络权重和偏置 三、前馈神经网络的训练方法损失函…