并查集求连通分量的个数之hdu1213
来源:互联网 发布:沈阳工业大学知乎 编辑:程序博客网 时间:2024/06/06 18:41
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8755 Accepted Submission(s): 4283
Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
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.
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
The 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.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
25 31 22 34 55 12 5
Sample Output
24#include <iostream>using namespace std;int set[1001];//保存根节点int vis[1001];int findx(int x)//查找根节点{ int r=x; while(r!=set[r]) { r=set[r]; } return r;}void merge(int a,int b)//合并a,b{ int fa=findx(a); int fb=findx(b); if(fa!=fb)set[fa]=set[fb];}int main(){ int t; cin>>t; while(t--) { for(int i=0;i<=1001;i++)//初始化 { set[i]=i; vis[i]=0; } int n,m; cin>>n>>m; for(int i=1;i<=m;i++) { int a,b; cin>>a>>b; merge(a,b); vis[a]=1;vis[b]=1; } int cnt=0; for(int i=1;i<=n;i++)//连通分量的个数 { if(set[i]==i)cnt++; } cout<<cnt<<endl; } return 0;}
- 并查集求连通分量的个数之hdu1213
- 并查集求连通分量的个数
- 并查集找连通分量的个数
- 连通分量的个数-->并查集的使用
- 连通分量个数(并查集的应用)
- hdu 1232 并查集 求连通分量的个数
- sdut 1488 连通分量的个数(并查集)
- 连通分量个数(连通分量_并查集)
- csu 1601: War(并查集求每次去掉一条边后连通分量的个数)
- 强连通分量个数的tarjan算法
- (java)DFS求连通分量的个数
- 数据结构实验:连通分量的个数
- 连通分量个数
- hdu 1198 dfs||并查集求连通分量个数,关键建图
- 数据结构实验:连通分量个数(并查集)
- 数据结构实验:连通分量个数(并查集)
- 数据结构实验:连通分量个数——并查集
- 暑假集训 8.17 数据结构实验:连通分量个数(并查集判断连通分量个数 路径压缩)sdutoj1488
- hdu 1003 Max Sum
- SPRING IBATIS 多数据源
- hibernate主键生成策略
- 360手机root权限怎么获取
- uva10003 - Cutting Sticks(简单动规)
- 并查集求连通分量的个数之hdu1213
- NSLayoutConstraint
- 题目4:找出由两个三位数乘积构成的回文。
- 基于3D卷积神经网络的人体行为理解(论文笔记)
- hdoj_2033 人见人爱A+B
- linux上手工创建数据库
- java IO流操作学生信息管理系统
- spring3.0注解(2)
- TQ2440-UART(查询及中断方式)