HDU 1213 How Many Tables is the first question to query the set.

Source: Internet
Author: User

HDU 1213 How Many Tables is the first question to query the set.
How Many TablesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 15083 Accepted Submission (s): 7361



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 (! = 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

Author Ignatius. L
Source hangdian ACM provincial training team trials

Recommend

 

 

The code is not very long, but I have understood it for a long time, and I do not know whether my understanding is correct. Share it ..

5 3

1 2

2 3

4 5

 

2

 

In this case, there are five people numbered 1 to 5, three representing three pairs of friends. Here we represent 1 and 2 as friends, and 2 and 3 as friends, 4 and 5 are friends .. Output 2 represents two tables. Because 1 and 2 are friends, 2 and 3 are friends, and 1 and 3 are friends by default. So three people only need one table. 4 and 5 are enough tables. This is the question. Give you a number n. The following lists the relationship between m and people. Ask how many tables do you need at least. I did this question more than a month ago. At that time, I was naive and thought of a method. I finally got WA and ran to the discussion board to ask others (shame ).

 

 

Naive code in the past ..

Now I have attached the code that I understand. I do not know whether it is correct.

 

 

# Include
 
  
Int p [1005]; int find (int x) // This is used for the following search. {If (x! = P [x]) p [x] = find (p [x]); return p [x];} int hebing (int x, int y) // this function is used for merging. {Return p [x] = y; // assume a = 2, B = 3, p [2] = p [3] = 3. That is, 2 and 3 are in the same table .} Int main () {int t, I, a, B; scanf ("% d", & t); while (t --) {int n, m, ans = 0; scanf ("% d", & n, & m); for (I = 1; I <= n; I ++) p [I] = I; // initialize it so that the value numbered one is 1 and the value numbered 2 is 2. and so on. For (I = 1; I <= m; I ++) {scanf ("% d", & a, & B); a = find (); B = find (B); // assume a = 2, B = 3. After this search, p [2] is equivalent to p [3. If (! = B) hebing (a, B); // merge it into a value .} For (I = 1; I <= n; I ++) {if (p [I] = I) // After M merge, if you are a friend, or indirectly, they all correspond to the same value. So the table is reduced. Ans ++; // if the value still corresponds, it is not a friend relationship. Add a table .} Printf ("% d \ n", ans); // The output format must be empty .} Return 0 ;}
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.