HDU 1213 how many tables

Source: Internet
Author: User
Problem descriptiontoday 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 (! = 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 input25 31 22 34 55 12 5 sample output2 4

 

Query the total number of nodes in the Set:

 1 #include<stdio.h> 2 #define LEN 10000 3  4 int p[LEN]; 5  6 int _find(int x) { 7     return p[x] == x ? x : (p[x] = _find(p[x])); 8 } 9 10 int main() {11     int num, ans, i, j;12     scanf("%d", &num);13     while(num--){14         int n, m;15         scanf("%d%d", &n, &m);16         for(i = 1; i <= n; i++)17             p[i] = i;18         for(i = 0; i < m; i++){19             int a, b, t1, t2;20             scanf("%d%d", &a, &b);21             t1 = _find(a);22             t2 = _find(b);23             if(t1 != t2)24                 p[t1] = t2;25         }26         ans = 0;27         for(i = 1; i <= n; i++)28             if(i == p[i])29                 ans++;30         printf("%d\n", ans);31     }32     return 0;33 }

 

 

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.