并查集题目 -- ZJU1789 The Suspects

通过并查集算法解决SARS疑似病例传播模拟问题,快速找出所有潜在接触者。
The Suspects

Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 1144   Accepted Submit: 512  

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.

In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).

Once a member in a group is a suspect, all members in the group are suspects.

However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.


Input

The input contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n-1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.

A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.


Output

For each case, output the number of suspects in one line.


Sample Input

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0


Sample Output

4
1
1

解决方案:并查集。将每个组中的成员合并为一个集合,最后统计与0在同一个集合中的学生个数

Code:

#include

<stdio.h>

#include

<string.h>

#define

MAXN 30000

#define

MAXM 501

class

{

DisjointSet

public

 

{

size = s;

memset(parent,-1,s *

}

 

{

 

 

 

t = x;

 

{

k = parent[t];

parent[t] = root_x;

t = k;

}

 

}

 

{

 

 

 

 

 

 

 

{

parent[root_x] += parent[root_y];

parent[root_y] = root_x;

}

 

:void Make_Set(int s)sizeof(int));int Find_Set(int x)int root_x;int t,k;for(root_x = x; parent[root_x]>=0; root_x = parent[root_x]);while(t!=root_x)return root_x;void Union(int x, int y)if(x==y)return;int root_x = Find_Set(x);int root_y = Find_Set(y);if(root_x!=-1 && root_y!=-1 && root_x == root_y)return;if(parent[root_x] < parent[root_y])else

{

parent[root_y] += parent[root_x];

parent[root_x] = root_y;

}

}

 

{

 

}

int Size(int x)return -parent[Find_Set(x)];

protected

 

 

};

:int parent[MAXN];int size;

int

{

 

DisjointSet ds;

 

 

{

scanf(

 

ds.Make_Set(N);

 

{

scanf(

 

{

scanf(

a = ds.Find_Set(s);

 

{

scanf(

ds.Union(a, s);

}

}

}

printf(

}

}

 
main(int argc,char **argv)int N,M,i,j,k,s,a;while(true)"%d %d",&N,&M);if(N==0 && M==0)break;for(i=0; i< M; ++i)"%d",&k);if(k>0)"%d", &s);for(j = 1; j<k; ++j)"%d", &s);"%d/n",ds.Size(0));

数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第二阶段根据实际场景进行调整,通过引入1-范数和∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性和实用性。文中提供了完整的Matlab代码实现,便于读者复现和验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数和∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值