HDU - 1213 How Many Tables [and look up]

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.

InputThe 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.
OutputFor 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
Question 4 

Meaning: Today is Ignatius' birthday. She invited some friends to have dinner at her house. These friends only sit at the same table with people they know. For example, A knows B and B knows C. Then A, B, and C can be Sitting at a table, now ask Ignatius how many tables should I prepare for dinner?
Idea: Use union search to find the number of connected components.

Before using union search, we must first understand what union search is.
As the name implies, the core of union search is merging and querying; The tree structure is more like the operation of the forest in practical applications. Each set represents a tree. The query refers to querying which tree a node
belongs to, and then determines the merging conditions of the trees according to the meaning of the question. Some trees are merged to get the desired result.

Query -> find function
What is the find function used for? Take this question as an example, it is used to query the circle of friends that a person belongs to. If A knows B, and B knows C, find(C) is to find which circle of friends C is in, and we take the first one as a friend. The leader of the circle, that is, A, A marks a circle of friends, so find(C) = A;

merge
If A knows B and C, A, B, and C want to sit at a table, and D knows E and F, D, E, and F want to sit at the same table. Then there is another condition: A knows D, then the two table people can sit together. At this time, the merge operation is completed as long as the two leaders are connected together.

After understanding the function of the union search, this problem is easily solved~

AC code:
there are some details in the code comments
#include<cstdio> 
#include <cstring> 
#include <algorithm>
 using  namespace std;
 const  int maxn = 1050 ;
 int fa[maxn];   // Leaders of the circle of friends 
int T, N, M;
 int u, v ;    // u and v know 
int Find( int u)   // Find the leader of u's Moments 
{
     if (u == fa[u]) return fa[u];
     else  return fa[u] = Find(fa [u]);
}
void adde( int u, int v)   // u and v are friends 
{
     int x = Find(u);   // find the leader of the circle of friends where x is 
    int y = Find(v);   // find the circle of friends where y is The leader of   
    if (x != y) fa[x] = fa[y];   // If the current circle of friends x and y are not the same, then combine the two leaders into one 
}
 int main ()
{
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &N, &M);
        for(int i = 1; i <= N; i++)
            fa[i] = i;    // Everyone is a small group at first 
        for ( int i = 1 ; i <= M; i++ )
        {
            scanf("%d%d", &u, &v);
            adde(u, v);    // The circle of friends where u and v are merged 
        }
         int ans = 0 ;   // The number of leaders, that is, the number of tables 
        for ( int i = 1 ; i <= N; i++ )
        {
            if(fa[i] == i) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167931&siteId=291194637