
精准核酸检测
题目描述
为了达到新冠疫情精准防控的需要,为了避免全员核酸检测带来的浪费,需要精准圈定可能被感染的人群。
现在根据传染病流调以及大数据分析,得到了每个人之间在时间、空间上是否存在轨迹的交叉。
现在给定一组确诊人员编号(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
输入:
bash
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人要进行核酸检测。
cpp
#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;
}