hdu_1213_How Many Tables_201403091126
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11038 Accepted Submission(s): 5447
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
#include <stdio.h>
int pre[];
int find(int x)
{
int i,r,t;
t=r=x;
while(r!=pre[r])
{
r=pre[r];
}
while(t!=r)
{
i=pre[t];
pre[t]=r;
t=i;
}
return r;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m,n,i,j,a,b,pa,pb,total;
scanf("%d %d",&n,&m);
total=n;
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<m;i++)
{
scanf("%d %d",&a,&b);
pa=find(a);
pb=find(b);
if(pa!=pb)
{
pre[pb]=pa;
total--;
}
}
printf("%d\n",total);
}
return ;
}
//并查集简单应用
hdu_1213_How Many Tables_201403091126的更多相关文章
随机推荐
- SmartDispatcher 类
UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...
- java replaceAll 忽略大小写
public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...
- 数据库登陆失败原因: 未与信任 SQL Server 连接相关联
解决方案:用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 问题简述: 用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 说明: 执行当前 ...
- Linux命令补充及基础优化。
1.用户部分 1.1 创建新用户 涉及命令 useradd [root@oldboyedu-50 ~]# useradd oldboy #添加用户 oldboy 1.2 设置密码 [root@oldb ...
- JavaScript中的substr和Java中的substring
JavaScript::substr(index, length)从下标开始截取多少位,如果length为空,则截取到最后,-1倒数第一位,-2倒数第二位.... Java:substring(sta ...
- yield from (python生成器)
#生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in 'AB': # yield i yield from 'AB' # 就相当于上面的f ...
- 怎么在windows上安装 ansible How to install ansible to my python at Windows
答案是不能再window上安装,答案如下: It's back! Take the 2018 Developer Survey today » Join Stack Overflow to learn ...
- action="post" 、 servletconfig 、 servletcontext 、getPrintWiter() 、context-param、 init-param(第一个完整的servlet)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- Servlet访问路径的两种方式、Servlet生命周期特点、计算服务启动后的访问次数、Get请求、Post请求
Servlet访问路径的两种方式: 1:注解 即在Servlet里写一个@WebServlet @WebServlet("/myServlet") 2:配置web.xml < ...
- 海量文本信息查Top-k
问题描述: 有1千万条短信,一条一行,有重复.在5分钟之内,找出重复出现的前10条. 方案一: 1.分组进行边扫描边建散列表.建立哈希表,使用头,尾和中间随便两个字节作为Hash Code, 插入到H ...