zoukankan      html  css  js  c++  java
  • 寒假康复训练 B

    B - 2 HDU - 1213

    How Many Tables
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 50392 Accepted Submission(s): 25043

    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

    Sample Output
    2
    4

    题目大意:Ignatius过生日,来参加的人中只要认识的/间接认识的都坐在同一桌。请问需要准备多少张桌子?

    思路:初始化,查找(探明每个人的关系),合并(归类),明显是并查集的问题。
    tips:还可采用路径压缩算法

    #include<iostream>
    using namespace std;
    int pre[1005];
    int find(int x)//用于查找根结点函数
    {
    	if (x != pre[x])
    		pre[x] = find(pre[x]);
    	return  pre[x];
    }
    int join(int x, int y)//合并结点
    {
    	return pre[x] = y;
    }
    int main()
    {
    	int t;
    	cin >> t;
    	while (t--)
    	{
    		int n, m, a, b, ans = 0;
    		cin >> n >> m;
    		for (int i = 1; i <= n; i++)//初始化,将每个节点的前导设为自己
    			pre[i] = i;
    		for (int i = 0; i < m; i++)
    		{
    			cin >> a >> b;//输入两个有关系的座位/(代表)人
    			a = find(a);
    			b = find(b);
    			if (a != b)//判断它们的根节点是否相同
    				join(a, b);
    		}
    		for (int i = 1; i <= n; i++)//因为前面定义从1开始所以此处也需从1开始
    		{
    			if (pre[i] == i)
    				ans++;
    		}
    		cout << ans << endl;
    	}
    	return 0;
    }
    

    路径压缩算法

    int find(int x)
    {
        int r=x;
        while(pre[r]!=r)
        r=pre[r];//找到他的前导结点
        int i=x,j;
        while(i!=r)//路径压缩算法
        {
            j=pre[i];//记录x的前导结点
            pre[i]=r;//将i的前导结点设置为r根节点
            i=j;
        }
        return r;
    }
    //引用于:https://blog.csdn.net/The_best_man/article/details/62418823
    
  • 相关阅读:
    框架学习之Struts2 第五节 自定义拦截器
    框架学习之Struts2 第四节 文件上传
    2011_7_23 第三次评审
    框架学习之Struts2 第二节 Action的详解
    框架学习之Struts2 第一节 开发环境的搭建和第一个应用开发
    框架学习之Struts2 第七节 国际化
    关于LookUp的总结
    UML自学手册___事务模式——事务与人、地、物
    css合并外边距详解
    开发中的函数相关
  • 原文地址:https://www.cnblogs.com/gidear/p/10433277.html
Copyright © 2011-2022 走看看