• HDU 1213 - How Many Tables(并查集 or dfs)



    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.

    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

    2
    5 3
    1 2
    2 3
    4 5
    
    
    5 1
    2 5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Sample Output

    2
    4
    
    • 1
    • 2

    题目大意
    n n n个人参加派对,在他们之间有 m m m条相识关系,并且这种相识关系是可以传递的A —> B、B —> C可以得出A —> C,而题目要求就是为这些人安排一个桌子的坐法,使得每章桌子上的人都认识(或通过传递认识),然后给出需要的桌子个数。


    首刷自解:dfs遍历

    可以将每个人看作是图上的一个结点,如果两个人相识,就说明他们之间需要加上一条边,且注意这条边是无向边,可以直观想象,当所有边都加上之后,满足可以坐在同一张桌子上的人构成了一棵,因为在这树上任意两个结点都是可达的,即他们之间有机会相识。而图中构成的树其实也就是图中的连通分量,基于此,只需要将图中连通分量的个数统计出来就可以。有一个较快的统计方法就是利用dfs:若在图中dfs几遍,就是有几个连通分量

    C++代码

    #include 
    #include 
    using namespace std;
    typedef long long ll;
    #define Max_N 1010
    
    //所有人的关系共同组成一个图,每个相互认识的团体构成一个连通分量
    //几张桌子就是几个连通分量,计算一下dfs的次数就行 
    
    int T, N, M;
    int g[Max_N][Max_N];
    bool visit[Max_N];
    
    void dfs(int n){
    	visit[n] = true;
    	for(int i = 1;i <= N;i ++){
    		if(g[n][i] && !visit[i])
    			dfs(i);
    	}
    }
    
    int dfsCount(){
    	int count = 0;
    	for(int i = 1;i <= N;i ++){
    		if(!visit[i]){
    			count ++;
    			dfs(i);
    		}
    	}
    	return count;
    }
    
    
    int main(){
    	cin >> T;
    	int A, B;
    	while (T --){
    		cin >> N >> M;
    		memset(g, 0, sizeof(g));
    		memset(visit, false, sizeof(visit));
    		
    		while(M --){
    			cin >> A >> B;
    			g[A][B] = 1;
    			g[B][A] = 1;	//初始化图 
    		}
    		int count = dfsCount();
    		cout << count << endl;
    	}
    	
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    二刷自解:并查集

    上面考虑连通分量的做法实际上就是在统计集合的个数,既然是涉及到集合,就可以考虑利用并查集优化,将每对存在相识关系的人都放入到一个集合内,最后再统计一下有多少个单独的集合。

    注*: 当满足x == find(x)时说明找到了一个集合的根节点,此时计数+1。

    C++代码

    #include 
    #include 
    using namespace std;
    
    const int N = 1010;
    
    int n, m, T;
    int fa[N];
    
    int find(int x){
        if(x != fa[x])      fa[x] = find(fa[x]);
        return fa[x];
    }
    
    void Union(int x, int y){
        x = find(x), y = find(y);
        if(x != y){
            fa[x] = y;
        }
    }
    
    int main(){
        cin >> T;
        while(T --){
            cin >> n >> m;
            for(int i = 1;i <= n;i ++)      fa[i] = i;
            
            for(int i = 1;i <= m;i ++){
                int a, b;
                cin >> a >> b;
                Union(a, b);
            }
            
            int cnt = 0;
            for(int i = 1;i <= n;i ++)
                if(i == find(i))        cnt ++;     //统计集合个数,即找各个集合的个数的根结点
            
            cout << cnt << endl;
        }
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    es(Elasticsearch)安装使用(01安装篇)
    多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
    剖析一下"抢茅台"脚本底层逻辑
    Python| GUI界面进行学生与作业匹配
    使用Nginx可视化管理工具+Cpolar在本地搭建服务器并实现远程访问【内网穿透】
    c++STL容器(看这一篇就够)
    07 MySQL 从入门到精通——运算符、流程控制语句
    【Redis】Redis常见面试题总结
    JAVA语言学习-面向对象(1)
    指针进阶(3)
  • 原文地址:https://blog.csdn.net/weixin_53024141/article/details/127729395