HDU 1213 how many Tables (and collection)

Source: Internet
Author: User

How many TablesTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14683 Accepted Submission (s): 7187


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
Is the last number of guests to invite the table, and check set:
#include <iostream> #include <stdio.h> #include <string.h>using namespace Std;int pre[1010];int Union    _find (int node) {int leaf=node;    while (Node!=pre[node]) Node=pre[node];        while (leaf!=node) {int t_p=pre[leaf];        Pre[leaf]=node;    leaf=t_p; } return node;    int main (int argc, char *argv[]) {//Freopen ("1213.in", "R", stdin); int t;//test Case Quantity T (1<=t<=25) int n,m;//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.    (1<=n,m<=1000) int A, B;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        memset (pre,0,sizeof (pre));        int total=n;        for (int i=1;i<=n;++i) pre[i]=i;            while (m--) {scanf ("%d%d", &a,&b);            int P=union_find (a);            int Q=union_find (b); if (p!=q) {pre[p]=q;            total--;    }} printf ("%d\n", total); } return 0;}


HDU 1213 how many Tables (and collection)

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.