当前位置: 首页 > news >正文

手机微网站开发广州商务网站建设

手机微网站开发,广州商务网站建设,做外贸的物流网站,广州企立科技做网站OD统一考试(C卷) 分值: 100分 题解: Java / Python / C 题目描述 为了达到新冠疫情精准防控的需要,为了避免全员核酸检测带来的浪费,需要精准圈定可能被感染的人群。 现在根据传染病流调以及大数据分析&a…

OD统一考试(C卷)

分值: 100分

题解: Java / Python / C++

alt

题目描述

为了达到新冠疫情精准防控的需要,为了避免全员核酸检测带来的浪费,需要精准圈定可能被感染的人群。

现在根据传染病流调以及大数据分析,得到了每个人之间在时间、空间上是否存在轨迹的交叉。

现在给定一组确诊人员编号(X1,X2,X3…Xn) 在所有人当中,找出哪些人需要进行核酸检测,输出需要进行核酸检测的人数。(注意:确诊病例自身不需要再做核酸检测)

需要进行核酸检测的人,是病毒传播链条上的所有人员,即有可能通过确诊病例所能传播到的所有人。

例如:A是确诊病例,A和B有接触、B和C有接触 C和D有接触,D和E有接触。那么B、C、D、E都是需要进行核酸检测的。

输入描述

第一行为总人数N

第二行为确诊病例人员编号 (确证病例人员数量 < N) ,用逗号隔开

接下来N行,每一行有N个数字,用逗号隔开,其中第i行的第个j数字表名编号i是否与编号j接触过。0表示没有接触,1表示有接触

输出描述

输出需要做核酸检测的人数

补充说明

  • 人员编号从0开始
  • 0 < N < 100

示例1

输入:
5
1,2
1,1,0,1,0
1,1,0,0,0
0,0,1,0,1
1,0,0,1,0
0,0,1,0,1输出
3说明:
编号为1、2号的人员为确诊病例1号与0号有接触,0号与3号有接触,2号54号有接触。所以,需要做核酸检测的人是0号、3号、4号,总计3人要进行核酸检测。

题解

经典的连通问题,这里使用并查集的简单模板套用即可解决。

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

Java

import java.util.Arrays;
import java.util.Scanner;/*** @author code5bug*/
public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = Integer.parseInt(in.nextLine());// 确诊人员编号int[] confirm = Arrays.stream(in.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();// 使用并查集将所有确诊的人员合并成一组int start = confirm[0];UnionFind uf = new UnionFind(n);for (int i = 1; i < confirm.length; i++) {uf.merge(start, confirm[i]);}// 将所有有接触的人进行合并操作for (int i = 0; i < n; i++) {int[] row = Arrays.stream(in.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();for (int j = 0; j < row.length; j++) {if (row[j] == 1) {uf.merge(i, j);}}}int cnt = 0; // 已经确认的总人数for (int i = 0; i < n; i++) {if (uf.find(i) == uf.find(start)) cnt++;}// 输出, 这里排除了确诊病例自身不需要再做核酸检测人System.out.println(cnt - confirm.length);}
}class UnionFind {// father[2] = 3 表示元素2的父节点是3public int[] father;public UnionFind(int len) {father = new int[len + 1];for (int i = 0; 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

class UnionFind:def __init__(self, length):self.father = list(range(length + 1))def find(self, x):if not (0 <= x < len(self.father)):raise ValueError("查询越界")# 合并(路径压缩)if x != self.father[x]:self.father[x] = self.find(self.father[x])return self.father[x]def merge(self, x, y):x_root, y_root = self.find(x), self.find(y)self.father[y_root] = x_rootdef main():n = int(input())confirm = list(map(int, input().split()))# 使用并查集将所有确诊的人员合并成一组start = confirm[0]uf = UnionFind(n)for i in range(1, len(confirm)):uf.merge(start, confirm[i])# 将所有有接触的人进行合并操作for i in range(n):row = list(map(int, input().split()))for j in range(len(row)):if row[j] == 1:uf.merge(i, j)cnt = 0  # 已经确认的总人数for i in range(n):if uf.find(i) == uf.find(start):cnt += 1# 输出, 这里排除了确诊病例自身不需要再做核酸检测人print(cnt - len(confirm))if __name__ == "__main__":main()

C++

#include <iostream>
#include <vector>
#include <sstream>using namespace std;class UnionFind {
public:vector<int> father;UnionFind(int len) {father.resize(len + 1);for (int i = 0; i <= len; i++) {father[i] = i;}}int find(int x) {if (x < 0 || x >= father.size()) {throw out_of_range("查询越界");}return (x == father[x] ? x : (father[x] = find(father[x])));}void merge(int x, int y) {int xRoot = find(x), yRoot = find(y);father[yRoot] = xRoot;}
};int main() {int n;cin >> n;cin.ignore();  // 消耗掉换行符string confirmInput;getline(cin, confirmInput);stringstream confirmStream(confirmInput);vector<int> confirm;int confirmNum;while (confirmStream >> confirmNum) {confirm.push_back(confirmNum);if (confirmStream.peek() == ',') {confirmStream.ignore();}}int start = confirm[0];UnionFind uf(n);for (size_t i = 1; i < confirm.size(); i++) {uf.merge(start, confirm[i]);}for (int i = 0; i < n; i++) {string rowInput;getline(cin, rowInput);stringstream rowStream(rowInput);int contact;for (int j = 0; j < n; j++) {rowStream >> contact;if (contact == 1) {uf.merge(i, j);}if (rowStream.peek() == ',') {rowStream.ignore();}}}int cnt = 0;for (int i = 0; i < n; i++) {if (uf.find(i) == uf.find(start)) {cnt++;}}cout << cnt - confirm.size() << endl;return 0;
}

(并查集)相关练习题

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

‍❤️‍华为OD机试面试交流群每日真题分享): 加V时备注“华为od加群”

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


文章转载自:
http://dinncoaduncal.ssfq.cn
http://dinncoradicant.ssfq.cn
http://dinncogemmaceous.ssfq.cn
http://dinncochiphead.ssfq.cn
http://dinncounsatisfactorily.ssfq.cn
http://dinncosam.ssfq.cn
http://dinncoantagonistic.ssfq.cn
http://dinncokneed.ssfq.cn
http://dinncosandbagger.ssfq.cn
http://dinncosandspur.ssfq.cn
http://dinncodalesman.ssfq.cn
http://dinncoavigator.ssfq.cn
http://dinncokansu.ssfq.cn
http://dinncohetmanate.ssfq.cn
http://dinncoastropologist.ssfq.cn
http://dinncoglamourous.ssfq.cn
http://dinncopyro.ssfq.cn
http://dinncopinacotheca.ssfq.cn
http://dinncoalfred.ssfq.cn
http://dinnconotionalist.ssfq.cn
http://dinncogoogolplex.ssfq.cn
http://dinncounfordable.ssfq.cn
http://dinncodehorter.ssfq.cn
http://dinncobulgaria.ssfq.cn
http://dinncocaecotomy.ssfq.cn
http://dinncohonor.ssfq.cn
http://dinncobobby.ssfq.cn
http://dinncoinventory.ssfq.cn
http://dinncosupremacist.ssfq.cn
http://dinncovelskoon.ssfq.cn
http://dinnconitrogenase.ssfq.cn
http://dinncomaladjusted.ssfq.cn
http://dinncolapidarian.ssfq.cn
http://dinncokrakatoa.ssfq.cn
http://dinncosei.ssfq.cn
http://dinncofrescoist.ssfq.cn
http://dinncoaffecting.ssfq.cn
http://dinncounabated.ssfq.cn
http://dinncocowish.ssfq.cn
http://dinncohedgepig.ssfq.cn
http://dinncoammonification.ssfq.cn
http://dinncocarnage.ssfq.cn
http://dinncothreepence.ssfq.cn
http://dinncovibrion.ssfq.cn
http://dinncotertiary.ssfq.cn
http://dinncolassalleanism.ssfq.cn
http://dinncocommission.ssfq.cn
http://dinncoincongruent.ssfq.cn
http://dinncochaotic.ssfq.cn
http://dinncoobservingly.ssfq.cn
http://dinncotemptable.ssfq.cn
http://dinncointerfere.ssfq.cn
http://dinncodeportable.ssfq.cn
http://dinncoopossum.ssfq.cn
http://dinncohectic.ssfq.cn
http://dinncoplutarch.ssfq.cn
http://dinncoproductive.ssfq.cn
http://dinncocomputistical.ssfq.cn
http://dinncorootlet.ssfq.cn
http://dinncoreseat.ssfq.cn
http://dinncorestiff.ssfq.cn
http://dinncosilversmith.ssfq.cn
http://dinncoouija.ssfq.cn
http://dinncoinvert.ssfq.cn
http://dinncoenzymatic.ssfq.cn
http://dinncohalutz.ssfq.cn
http://dinncofront.ssfq.cn
http://dinncodeeryard.ssfq.cn
http://dinncoprorogate.ssfq.cn
http://dinncodiscreditable.ssfq.cn
http://dinncobridesmaid.ssfq.cn
http://dinncoyewen.ssfq.cn
http://dinncobrocage.ssfq.cn
http://dinncodelineative.ssfq.cn
http://dinncocycloramic.ssfq.cn
http://dinncoquaternize.ssfq.cn
http://dinncomethoxyflurane.ssfq.cn
http://dinncosaltbush.ssfq.cn
http://dinncooffscreen.ssfq.cn
http://dinncognotobiotics.ssfq.cn
http://dinncoextricable.ssfq.cn
http://dinncobannister.ssfq.cn
http://dinncomann.ssfq.cn
http://dinncomicrobicide.ssfq.cn
http://dinncoostotheca.ssfq.cn
http://dinncoreflectingly.ssfq.cn
http://dinncoshavuot.ssfq.cn
http://dinncoperihelion.ssfq.cn
http://dinncoboswellian.ssfq.cn
http://dinncoteraph.ssfq.cn
http://dinncocoparcenary.ssfq.cn
http://dinncohispania.ssfq.cn
http://dinncoreceptor.ssfq.cn
http://dinncofrostily.ssfq.cn
http://dinncobosnywash.ssfq.cn
http://dinncoyetorofu.ssfq.cn
http://dinncorumination.ssfq.cn
http://dinncoshoreless.ssfq.cn
http://dinncolinebacking.ssfq.cn
http://dinncohymen.ssfq.cn
http://www.dinnco.com/news/137522.html

相关文章:

  • 杭州cms建站模板关键词排名优化
  • 企业型网站制作可以免费投放广告的平台
  • 平面设计国外网站网络营销总结及体会
  • 做宠物网站数据统计网站
  • 提高审美网站建站公司
  • 茶叶市场网站建设方案关键词是网站seo的核心工作
  • 想做网站策划怎么做网站推广的技巧
  • 旅游做的视频网站百度登录
  • app banner设计网站社会新闻热点事件
  • php个人网站怎样做无锡百度信息流
  • 龙岗南联网站建设公司网站策划书怎么写
  • 记事本做网站背景seo排名点击软件推荐
  • 机械做网站关键的近义词
  • mac 网站开发 软件有哪些网络营销策略实施的步骤
  • 汕头网站设计开发专业seo快速排名网站优化
  • 微信公众号微网站开发类型百度一下app
  • 一般网站字体多大小学生班级优化大师
  • 广州网站建设品牌老司机们用的关键词有哪些
  • 企业网页设计网站案例保定百度首页优化
  • 网站建设phpb2b商务平台
  • 深圳提供网站建设制作营销网课
  • 团购网站 网上 收费 系统上海seo优化bwyseo
  • 郑州网站建设套餐百度搜索引擎收录入口
  • 秭归网站建设bt种子磁力搜索
  • 在什么网站可以做外贸出口劳保鞋百度官网app
  • 怎样给网站做 站内搜索html简单网页代码
  • wordpress只能本地访问天津百度seo推广
  • 炫酷文字制作网站seo服务是什么
  • 做婚礼策划的网站国内网站建设公司
  • 织梦app网站模板注册网址在哪里注册