HDU 1213 how many tables (DFS)

Source: Internet
Author: User
HDU 1213 how many tablesproblem 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

 

// I am using guangsuo to solve this problem.
# Include <iostream>
Using namespace STD;
Bool mark [1001] [1001]; // used to mark recognition or not
Int num, N;
Void BFS (int I, Int J)
{
Mark [I] [J] = 0;
Int K;
For (k = 0; k <n; k ++) // horizontal vertical search
{
If (MARK [I] [k])
BFS (I, K );
If (MARK [k] [I])
BFS (K, I );
If (MARK [k] [J])
BFS (K, J );
If (MARK [J] [k])
BFS (j, k );
}
}
Int main ()
{
Int T, M, I, a, B, J;
Int flag [1001];
Cin> T;
While (t --)
{
Num = 0;
Cin> N> m;
Memset (mark, 0, N * n * sizeof (bool ));
Memset (flag, 0, sizeof (FLAG ));
For (I = 0; I <m; I ++)
{
Cin> A> B;
Mark [A-1] [b-1] = 1; // mark the person you recognize
Mark [b-1] [A-1] = 1;
Flag [a] = 1; // mark the number that has already occurred
Flag [B] = 1;
}
For (I = 1; I <= N; I ++)
If (flag [I])
Num ++; // count the number of workers
Num = N-num; // calculates the number of people who do not contain numbers. That is to say, they do not know such a large number of tables.
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
If (MARK [I] [J]) // if it is marked, start searching.
{
BFS (I, j );
Num ++;
}

Cout <num <Endl;
}
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.