how many tables
2015-06-15 17:43
106 查看
//java
//http://acm.hdu.edu.cn/showproblem.php?pid=1213
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner inScanner=new Scanner (System.in);
static int peo;
static int[] rel=new int[1010];
static int count;
static int findrel(int t){
if(rel[t]==-1)
return t;
return findrel(rel[t]);
}
static void addp(int x,int y){
int po1=findrel(x);
int po2=findrel(y);
if(po1!=po2)
rel[po1]=po2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
while(inScanner.hasNext()){
int num=inScanner.nextInt();
while(num--!=0){
count=0;
peo=inScanner.nextInt();
Arrays.fill(rel, -1);
int fri=inScanner.nextInt();
while(fri--!=0){
int x=inScanner.nextInt();
int y=inScanner.nextInt();
addp(x, y);
}
for(int i=1;i<=peo;i++){
if(rel[i]==-1){
count++;
}
}
System.out.println(count);
}
}
}
}
//http://acm.hdu.edu.cn/showproblem.php?pid=1213
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner inScanner=new Scanner (System.in);
static int peo;
static int[] rel=new int[1010];
static int count;
static int findrel(int t){
if(rel[t]==-1)
return t;
return findrel(rel[t]);
}
static void addp(int x,int y){
int po1=findrel(x);
int po2=findrel(y);
if(po1!=po2)
rel[po1]=po2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
while(inScanner.hasNext()){
int num=inScanner.nextInt();
while(num--!=0){
count=0;
peo=inScanner.nextInt();
Arrays.fill(rel, -1);
int fri=inScanner.nextInt();
while(fri--!=0){
int x=inScanner.nextInt();
int y=inScanner.nextInt();
addp(x, y);
}
for(int i=1;i<=peo;i++){
if(rel[i]==-1){
count++;
}
}
System.out.println(count);
}
}
}
}
相关文章推荐
- Git 用法及流程
- java 反射
- python爬虫
- vs2008自动添加消息和MFC对话框程序不显示主画面
- 用VisualSVN Server创建版本库,以及TortoiseSVN的使用
- 10款神奇的字符图案 & 词汇云生成工具
- 安装hadoop1.2.1(参考hadoop实战第二版)
- Java for LeetCode 223 Rectangle Area
- h系列标签自动加了bold
- Lesson 4-vSphere 5.0 网络管理
- 【基础】SQLite的基本使用方法
- iOS——presentViewController
- 使用Pull解析Xml文件
- 第十二章:线程控制
- datetime和time
- 自定义滑动开关(ToggleButton)
- 配置子目录Web.config使其消除继承
- 我又回来啦
- UIImage图片压缩和截取
- Java算法题