HDU 1213 How Many Tables(模板——并查集)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1213
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.
input starts with an integer T(1<=T<=25) which indicate the
number of test cases. Then T test cases follow. Each test case starts
with two integers N and M(1<=N,M<=1000). N indicates the number of
friends, the friends are marked from 1 to N. Then M lines follow. Each
line consists of two integers A and B(A!=B), that means friend A and
friend B know each other. There will be a blank line between two cases.
- #include<stdio.h>
- void merge(int u,int v);
- int getf(int v);
- int f[];
- int main()
- {
- int T,n,m,a,b,i,sum;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&m);
- for(i=;i<=n;i++)
- f[i]=i;
- while(m--)
- {
- scanf("%d%d",&a,&b);
- merge(a,b);
- }
- for(sum=,i=;i<=n;i++)
- {
- if(f[i]==i)
- sum++;
- }
- printf("%d\n",sum);
- }
- return ;
- }
- void merge(int u,int v)
- {
- int t1,t2;
- t1=getf(u);
- t2=getf(v);
- if(t1 != t2)
- f[t2]=t1;
- }
- int getf(int v)
- {
- if(f[v]==v)
- return v;
- return f[v]=getf(f[v]);
- }
HDU 1213 How Many Tables(模板——并查集)的更多相关文章
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- hdu 1213 How Many Tables(并查集练习)
题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
- HDU 1213 How Many Tables(并查集裸题)
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- HDU 1213 How Many Tables【并查集】
解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 1213 How Many Tables(并查集求无向图有几个连通分量)
代码: #include<cstdio> #include<cstring> using namespace std; int n,m; int father[1005]; i ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
随机推荐
- 安装lamp代码
tar -zxvf mysqladdUser mysql -s /sbin/nologinmv mysql /usr/local/mysql (改目录下直接存储bin docs等目录)mkdir -p ...
- Linux第九讲随笔 -进程管理 、ps aux 、
Linux第九讲1,进程管理 Linux在执行每一个程序时,就会在内存中为这个程序建立一个进程,以便让内核可以管理这个运行中的进程,进程是系统分配各种资源,进程调度的基本单位. 怎么查看进程 一.ps ...
- 30分钟入门Java
技术只是工具,文档只是说明书,仅此而已. 写在前面 工作4年有余,盲人摸象般的走过弯路,也投机取巧的领悟到过一些类似"编程本质"的东西.现在开始我计划回顾下我的编程生涯.在这里分享 ...
- qt中进程的使用
qt中的进程使用需要用到头文件:include<QProcess> 首先来看看需要用到的主要的函数 (1)进程的定义: QProcess *mprocess; //定义一个进程参数 (2) ...
- Linux(CentOS6.5)下修改Nginx初始化配置
本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 首先备份相关文件: cp /comexHome/nginx/conf ...
- js跳转页面的几种方式
第一种: window.location.href="http://www.baidu.com"; 第二种: window.history.back(-1); 第三种: windo ...
- [编织消息框架][网络IO模型]Netty Reactor
严格来讲Netty Reactor是一种设计模式,一听模式两字就知道了吧,套路哈哈 Reactor中文译为“反应堆”. 看图netty处理流程 1.netty server 至少有两组reactor. ...
- MicroPython开发板:TPYBoard v102 播放音乐实例
0x00前言 前段时间看到TPYBoard的技术交流群(群号:157816561,)里有人问关于TPYBoard播放音乐的问题.最近抽空看了一下文档介绍,着手做了个实验.更多MicroPython的教 ...
- 【python3之文件操作】
一.文件操作 1.文件处理的流程 1)打开文件,得到文件句柄并赋值给一个变量 2)通过句柄对文件进行操作 3)关闭文件 例如: f = open('chenli.txt') #打开文件 first_l ...
- Upgrade with the Gradle Wrapper, gradlew升级
springboot 2.0需要gradle 1+, 而自动构建的都是3.+,手动升级如下 Upgrade with the Gradle Wrapper If your existing Gradl ...