package com.liang.poj;
import java.util.Scanner;
public class Main {
static int n = 0;
static int m = 0;
static int[] node = null;
static int[] childNum = null;
static int count = 0;
static int sum = 0;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
n = scan.nextInt();
m = scan.nextInt();
if(n == 0 || m == 0) {
break;
}
node = new int[n + 1];
childNum = new int[n + 1];
makeSet();
for (int i = 1; i <= m; i++) {
int a = scan.nextInt();
int b = scan.nextInt();
merge(a, b);
}
for (int i = 1; i <= n; i++) {
if (node[i] == i) {
sum++;
}
}
System.out.println("Case "+ ++count + ": " + sum);
}
}
public static void makeSet() {
sum = 0;
for (int i = 1; i <= n; i++) {
node[i] = i;
childNum[i] = 0;
}
}
public static int findFather(int x) {
if (x != node[x]) {
x = findFather(node[x]);
}
return x;
}
public static void merge(int a, int b) {
int fa = findFather(a);
int fb = findFather(b);
if (fa != fb) {
if (childNum[fa] >= childNum[fb]) {
node[fb] = fa;
childNum[fa]++;
} else {
node[fa] = fb;
childNum[fb]++;
}
}
}
}
相关推荐
手把手教你如何修改YOLO11的模型结构(以CBAM和可变性卷积为例)
YOLO11修改模型结构
http://poj.org/problem?id=2524并查集简单应用
用总人数减去合并的次数得到的就是所要求的: #include<stdio.h> #include<string.h> int num[50001]; int find(int x) { return x==num[x]?x:num[x]=find(num[x]); } int main() { int n,m,t=1; while(~scanf("%d%d",...
阿里云天池大数据长期赛:金融风控-贷款违约预测(含代码)
本次比赛让自己在大数据的处理流程上有了更明确的认知,从数据清洗、特征工程、数据建模,每一个过程都能学到很多经验和知识,值得去历练。
POJ2524->并查集
题意:给出一些在一个集合的元素信息,求一共有多少个无交集的集合 题解:并查集,初始集合个数为人数,在合并集合的过程中,每次在一个集合里合并了一个人,集合个数就减一 #include #include using namespace std ; #define MAX 50005 int parent[MAX] ; int rank[MAX] ; int num[MAX] ; int cnt
延迟相乘叠加求和 DMAS (Field II 仿真代码)
(Delay Multiply and Sum, DMAS)算法,最初是为雷达微波系统设计的,研究者们对 DMAS 波束合成方法进行了修改,并应用超声成像。结果表明,DMAS 波束合成在模拟和实验中的表现都优于传统的延时叠加(Delay and Sum, DAS),这种新方法带来的主要改进是对比度分辨率明显提高(即主瓣和侧瓣更窄),从而增加了动态范围,提高了 B 型超声图像质量。与传统 DAS 相似,DMAS 算法通过对来自超声换能器阵元的信号应用几何计算延迟来聚焦接收到的波束。
华为昇腾服务器npu中安装paddle,pytorch等环境,训练代码迁移适配等问题记录
参考文档:https://github.com/PaddlePaddle/PaddleCustomDevice/blob/develop/backends/npu/README_cn.md
poj2524
说来惭愧啊。。现在才会并查集。我竟然给我妈妈讲明白并查集怎么回事了- - #define _CRT_SECURE_NO_WARNINGS #include using namespace std; #define maxx 50010 int set[maxx]; int find(int x) { return x == set[x] ? x : (set[x] = fin
ArcGIS+PLUS+InVEST三件套:从零搞定生态系统服务模拟预测(附完整数据包)
本文详细介绍了如何整合ArcGIS、PLUS模型和InVEST模型,构建一套完整的生态系统服务模拟预测工作流。内容涵盖从统一数据预处理、PLUS模型多情景模拟,到InVEST模型量化评估的全过程,并提供了关键脚本和避坑指南,旨在帮助研究者和从业者高效解决土地利用变化与生态系统服务评估结合的实际难题。
Poj2524 并查集
[code="java"]package middle; import java.io.BufferedInputStream; import java.util.Scanner; /** * poj2524 * 并查集的最简应用 * 只需要合并,并统计当前合集数就行了 * 好长时间啊!!! * 4622MS * @author NC */ publ...
RK3588网络调试笔记:当我把默认RTL8211F PHY换成另一款时,踩了哪些坑?
本文详细记录了在RK3588平台上将默认RTL8211F PHY芯片替换为其他型号的全过程,包括硬件设计验证、内核驱动适配、复位时序配置等关键步骤。通过实际案例分享,提供了从驱动调试到硬件优化的系统性解决方案,帮助开发者高效完成PHY芯片替换,确保网络接口稳定性。
BACnet协议栈基本对象BI、BO、AI、AO、AV以及device和File对象介绍
BACnet协议栈基本对象BI、BO、AI、AO、AV以及device和File对象介绍
POJ2524 并查集应用
import java.io.FileNotFoundException; import java.util.Scanner; public class Main2 { static Node[] nodes; static int count = 0;// 信仰个数 public static void main(String s[]) throws FileNotFoundExce
【AI 赋能:Python 人工智能应用实战】16. BERT微调实战:中文新闻分类全流程(附模型蒸馏+ONNX部署完整代码)
中文新闻分类是NLP领域的经典实用任务,BERT模型凭借双向上下文建模能力,能有效解决中文歧义与长文本依赖问题,但存在推理速度慢、资源占用高的痛点。本文以THUCNews中文新闻数据集为核心,提供从数据预处理、BERT微调、模型蒸馏到生产部署的全流程实操指南。首先详解THUCNews的多编码读取与清洗(解决gbk/utf-8兼容问题);然后实现BERT微调,加入早停机制与混淆矩阵可视化,验证准确率达93.8%;接着用DistilBERT完成模型蒸馏,在仅损失0.6%准确率的前提下,将推理速度提升2.7倍。
POJ_2524(并查集模板)
POJ 2524 并查集模板题 #include <iostream> using namespace std; const int maxn = 5e4+10; int pre[maxn]; int n,m; void init(int k) { for (int i=1; i<=k; i++) pre[i] = i; } int find(int x) { if (x ...
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
SIMATIC S7-1200, S7-1500 PID 控制[手册]
SIMATIC S7-1200, S7-1500 PID 控制[手册]
【并查集】POJ 2524 Ubiquitous Religions
POJ 2524 Ubiquitous Religions #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <cstring> #include <string>...
数学建模模型总结
这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!
POJ2524-宗教问题-并查集-ACM
太难的搞不过,只能来
win10 跳过U8环境检测的IE WebControls.rar
win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。
poj-2524(裸并查集)
题目链接: http://poj.org/problem?id=2524 题意: n个人,m对人宗教相同,输出一共有多少个不同的宗教。 代码如下: #include #include using namespace std; #define M 500100 int par[M]; int h[M]; int n, m; void init(int a) { for(i
Poj 2524 Ubiquitous Religions[简单并查集]
/* Poj 2524 Ubiquitous Religions 简单的并查集. */ #include int father[50002],N,M; void Init() { for(int i=1;i<=N;i++) father[i]=i; } int Find_Father(int x) { if(x!=father[x]) father[x]=F
POJ 2524 宗教信仰 并查集 基础模板
世界上有许多不同的宗教,现在有一个你感兴趣的问题:找出多少不同的宗教,在你的大学中的大学生信仰了多少种不同的宗教。你知道在你的大学有n个学生(0<n<= 50000)。若直接问每一个学生的宗教信仰不大适合。此外,许多学生还不太愿意说出自己的信仰。有一种方法来避免这个问题,询问m(0<=m<=n(n- 1)/ 2)对学生,询问他们是否信仰同一个宗教(比如,可以询问他们是否都参...
poj 2524 Ubiquitous Religions(并查集)
题目链接:http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 33619 Accepted: 16258 Description
POJ 2524 Ubiquitous Religions(并查集)
POJ 2524 Ubiquitous Religions(并查集) 题目大意:给你n个节点和m条边,然后问你有多少个连通分量. 分析:每读入一条边,如果这条边的两个点不在一个连通分量,那么总的连通分量数目就减少1.初始连通分量有n个. AC代码:344ms #include #include using namespace std; const int MAXN=50000+100; i
poj 2524 Ubiquitous Religions(并查集)
题目链接:poj 2524 Ubiquitous Religions 题目大意:
POJ 2524 Ubiquitous Religions 并查集
题意:现在在弄一个大学调查,调查的内容就是,大学生群体的信仰。 如果测试样例输入 1 2 就代表 1 2 拥有同样的信仰。 最后问你最多有多少个不同的 信仰。显然这是一个并查集的题目。而且是模版题目。传送门:POJ-2524-Ubiquitous Religions#include <stdio.h> int pre[50005]; int Find(int x) { int r=x;
poj 2524(并查集 链表实现)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16643 Accepted: 8050 Description There are so many different religions in the world
POJ 2524 Ubiquitous Religions - (并查集)
题目链接:http://poj.org/problem?id=2524 #include <stdio.h> #include <vector> #include <algorithm> using namespace std; //POJ 2524 Ubiquitous Religions inline int getFather(int *p...
POJ.2524 Ubiquitous Religions (并查集)
POJ.2524 Ubiquitous Religions (并查集)题意分析学校共有n个人,先给出m组信息,求问学校最多有多少种宗教信仰信奉者。 每组信息包括a,b两个人,表示a,b两人信奉同一种宗教。 初始化集合时,把每一个人都当做一个宗教。 读入信息时,如果发现2个人信奉的宗教不同(根节点祖先不同),合并这两个人,并使得宗教总数目-1, 最后输答案即可。代码总览#include <iost
1万+



被折叠的 条评论
为什么被折叠?



