HDU 1213 How many Tables first and check the set of questions.

Source: Internet
Author: User

How many Tables Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15083 Accepted Submission (s): 7361


Problem descriptiontoday is Ignatius ' birthday. He invites a lot of friends. Now it ' s dinner time. Ignatius wants to know what many tables he needs at least. You has to notice that not all the friends know each of the other, and all the friends does not want to stay with strangers.

One important rule for this problem yes if I tell you A knows B, and b knows C, that means A, B, C know They can stay in one table.

For Example:if I-a knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E has 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 is starts with the integers N and M (1<=n,m<=1000). N indicates the number of friends, the Friends is marked from 1 to N. Then M lines follow. Each line consists of integers a and B (a!=b), which means friend A and friend B know each other. There'll be a blank line between the cases.

Outputfor Each test case, just output how many tables Ignatius needs at least. Don't print any blanks.

Sample Input
25 31 22 34 55 12 5

Sample Output
24

Authorignatius.l
SOURCE Hangzhou Electric ACM Provincial Training Team Qualifying match

Recommend

The code is not very long, but I have understood for a long time, do not know whether their understanding is right. Anyway, share a bit.

5 3

1 2

2 3

4 5

2

This case stands for five persons numbered 1 to 5, three stands for three to friends, the following represent 1 and 2 are friends, 2 and 3 are friends, 4 and 5 are friends. The output 2 represents two tables. Since 1 and 2 are friends, 2 and 3 are friends, the default 1 and 3 are also friends. So three people just need a table. 4 and 51 tables is enough. This is what the title means. give you a number n, below the M-to-person relationship, ask you how many tables you need to prepare at least. One months ago I did this problem, then very naïve, casually think of a method, the result of WA, but also run to discuss the area to ask others (embarrassing).

Previously naïve code:

Now I enclose the code that I understand. I don't know if I understand it correctly.

#include <stdio.h>int p[1005];int Find (int x)     //The function of this is the following lookup. {if (x!=p[x]) P[x]=find (p[x]); return p[x];} int hebing (int x,int y)  //The function of this is to merge. {return  p[x]=y;     Suppose a=2,b=3, there should be p[2]=p[3]=3 at this time. That's 2 and 3 to the same table. }int Main () {    int t,i,a,b;scanf ("%d", &t), while (t--) {int n,m,ans=0;scanf ('%d%d ', &n,&m); for (i=1;i <=n;i++) p[i]=i;   Initialize it so that a value of 1 is numbered one, and a value of 2 is 2. And so on. for (i=1;i<=m;i++) {scanf ("%d%d", &a,&b); A=find (a);   B=find (b);   Suppose A=2,b=3, I think after this search p[2] is equal to p[3]. if (a!=b) hebing (A, b);  Merge to a value. }for (i=1;i<=n;i++) {if (p[i]==i)   //After M merges, if it's a friend, or an indirect friend, they have the same value. So the table is reduced. ans++;     If the value is still corresponding, then it is not a friend, add a table. }printf ("%d\n", ans);//output Note format with only one blank line. }return 0;}


HDU 1213 How many Tables first and check the set of questions.

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.