HDU 2208 (DFS enumeration)

Source: Internet
Author: User

I wrote wa many times for a long time.

Later, I asked you to stay on the stars.

The topic means that you need to use and check the set and then perform enumeration like Vertex coloring. It is very simple to enumerate, but the brute force will not have to re-learn.

The Code is as follows:

 

 

# Include <iostream>
Using namespace STD;
Bool hash [20] [20];
Long root [20];
Long n, m;
Bool finish;
Void DFS (long M, long s)
{
If (finish) // you can allocate
{
Return;
}
If (S> m) // too many balls are used
{
Return;
}
If (M = N) // The person has been added
{
Finish = true; // The ball is finished before it is fully split.
Return;
}
Int I, j, FG;
For (I = 0; I <m; I ++)
{
If (root [I]! = I) // If the I-th individual is not a collection representative
Continue; // continue to find the Representative
Fg = 1;
For (j = I; j <M & FG; j ++)
Fg =! (Root [J] = I & (hash [m] [J] = 0 | hash [J] [m] = 0 )); // if no one in the set where I is located rejects m
If (FG)
{
Root [m] = I; // make m belong to the I set
DFS (m + 1, S); // Add another person
Root [m] = m; // M restore continues to enumerate the next set
}
}
DFS (m + 1, S + 1); // when the next person adds one more ball
}

Bool check ()
{

Long I;

Finish = false;

For (I = 0; I <n; ++ I)
{
Root [I] = I;
}

DFS (0, 0 );

If (finish)
{
Return true;
}

Return false;
}

Int main ()
{

While (scanf ("% LD % lD", & N, & M )! = EOF)
{

Memset (hash, 0, sizeof (hash ));

Long I, J;
For (I = 0; I <n; ++ I)
{
Long N;
Scanf ("% lD", & N );
For (j = 0; j <n; ++ J)
{
Long T;
Scanf ("% lD", & T );
Hash [I] [T] = true;
}
}

If (n <= M | check ())
{
Puts ("yes ");
}
Else
{
Puts ("no ");
}

}
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.