Zoj2470 poj1904 King's quest, strongly connected component

Source: Internet
Author: User

A good question, I thought it was a bipartite graph match, but I thought it was a strongly connected component. For more information, click here to open the link.

Question: There are n princes and n beautiful women. Each prince may like multiple beautiful women at the same time. The data has already provided a set of completely matched solutions. Ask which of the following objects can be selected by each prince and output them in ascending order if all the princes can match them completely. (For example, the title description is more concise ..)

In the beginning, you may think about how to split the edges of each prince and then re-select the objects to determine the feasibility. Note that the question data volume is large, with a maximum of 2000 nodes and 200000 sides. If direct brute force splitting is feasible, the complexity is O (n * m )...... It must be useful to note that a combination of methods has been provided for the question. Assume that the prince A and the original beauty B are removed from the relationship and match the beauty C, then the original beauty D prince of the beauty C must find another beauty again. If any prince can retrieve the beauty B match, prove that Wang a and beauty C are likely to match, otherwise it is impossible to match.

Composition: Each prince connects a directed edge to a favorite beauty, and then connects a directed edge to the matched prince according to the matching scheme... The simplified description after the diagram is as follows: if we start from point A, we can finally return to point a (forming a ring), then we can maintain the exact match. Therefore, we need to calculate a strongly connected component, determine whether each Prince is connected to his favorite beauty.

I would like to add that the numbers of male and female in each strongly connected component in this figure are the same. This is because men only connect to women, women only connect to men, and women connect only one connection to men.

/*************************************** **************************************** # Author: neo Fung # Email: neosfung@gmail.com # Last modified: 2012-07-24 # filename: zoj2470 poj1904 King's quest. CPP # description: **************************************** **************************************/# ifdef _ msc_ver # define debug # DEFINE _ crt_secure_no_deprecate # endif # include <fstream> # include <stdio. h> # include <iostream> # include <string. h> # include <string> # include <limits. h> # include <algorithm> # include <math. h> # include <numeric> # include <functional> # include <ctype. h ># include <vector> using namespace STD; const int Kmax = 10010; const double Keps = 10e-6; int stack [Kmax], Top = 0; // Tarjan Algorithm In the stack bool instack [Kmax]; // check whether int dfn [Kmax] is in the stack; // The depth first searches for the access order int low [Kmax]; // The earliest order int componentnumber that can be traced back to the stack is 0; // The number of strongly connected components of the directed graph int Index = 0; // Index Number Vector <int> edge [Kmax]; // The adjacent table indicates int incomponent [Kmax]; // record the int componentdegree [Kmax] of each point in the strongly connected component number; // record the void Tarjan (int I) {Int J of each strongly connected component; dfn [I] = low [I] = index ++; instack [I] = true; stack [++ top] = I; for (size_t E = 0; E <edge [I]. size (); e ++) {J = edge [I] [E]; If (Dfn [J] =-1) {Tarjan (j); low [I] = min (low [I], low [J]);} else if (instack [J]) // If the pointing node J is still in the stack, because J is in the stack before I, j has the path to I, at the same time, because I points to J, I and j constitute the loop low [I] = min (low [I], dfn [J]); // If the pointing node is thrown into the stack, the pointing node is not incorporated into the strongly connected component. // if both of the preceding judgment conditions are incorrect, then I and j are not in the same connected component} If (dfn [I] = low [I]) // the point in the early stack of the connected component {componentnumber ++; do {J = stack [top --]; instack [J] = false; incomponent [J] = componentnumber; // dye nodes on each connected component} while (J! = I) ;}} int output [Kmax]; void solve (INT N) // n indicates the number of vertices in the graph. Note that it is 0-indexed! {Memset (stack,-1, sizeof (stack); memset (instack, 0, sizeof (instack); memset (dfn,-1, sizeof (dfn )); memset (low,-1, sizeof (low); For (INT I = 1; I <= N; I ++) if (dfn [I] =-1) tarjan (I); int n = n/2; for (INT I = 1; I <= N; ++ I) {int x = incomponent [I]; size_t CNT = 0; For (size_t J = 0; j <edge [I]. size (); ++ J) if (incomponent [edge [I] [J] = X) output [CNT ++] = edge [I] [J]-N; sort (output, output + CNT); printf ("% d", CNT ); for (size_t J = 0; j <CNT; ++ J) PRI Ntf ("% d", output [J]); printf ("\ n");} // printf ("\ n");} int main (void) {# ifdef debug freopen (".. /stdin.txt "," r ", stdin); freopen (".. /stdout.txt "," W ", stdout); # endif int N, TMP, U, V; while (~ Scanf ("% d", & N) {for (INT I = 0; I <= 2 * n; ++ I) edge [I]. clear (); For (INT I = 1; I <= N; ++ I) {scanf ("% d", & TMP); While (TMP --) {scanf ("% d", & V); edge [I]. push_back (V + n) ;}}for (INT I = 1; I <= N; ++ I) {scanf ("% d", & U ); edge [U + N]. push_back (I);} solve (n + n);} 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.