A good question. I read other people's disintegration reports to worship Daniel!
Http://hi.baidu.com/saintlleo/blog/item/ab5e6b1b65f538c5a7866906.html
Divide men and women into two sets. If a prefers B, A connects a side to B, and B perfectly matches the question, B connects a side to.
Then, ask for a strong connected component. If a man and some women are in a strong connected component, they can get married. Note that women should find them in the list they like, some men and women may be in the same connected component, but men do not like women.
If some men and women are in a strongly connected component, the strongly connected component will certainly have a ring, and the number of vertices in the ring is an even number, all of which correspond to a man and a woman, any man in the circle can marry any other woman without causing any man to get married.
As a result, you can find the staggered track like finding the augmented path in the binary match, starting from the initial match, and connecting the first and end of the staggered track, forming a ring, that is, finding the strongly connected component.
Code:
# Include <iostream> <br/> # include <stack> <br/> # include <algorithm> <br/> using namespace STD; <br/> const int max = 202005; <br/> struct node <br/>{< br/> int V, next; <br/>} G [Max * 6]; <br/> int dfn [Max], low [Max], instack [Max], belong [Max], res [Max], adj [Max]; <br/> int e, index, CNT, K, num, N, top, stack [Max]; <br/> void Tarjan (int u) <br/>{< br/> int I, V; <br/> low [u] = dfn [u] = ++ index; <br/> stack [++ top] = u; <br/> instack [u] = 1; <br/> for (I = adj [u]; I! =-1; I = G [I]. Next) <br/>{< br/> V = G [I]. V; <br/> If (! Dfn [v]) <br/>{< br/> Tarjan (V); <br/> low [u] = min (low [u], low [v]); <br/>}< br/> else if (instack [v]) <br/>{< br/> low [u] = min (low [u], dfn [v]); <br/>}< br/> If (dfn [u] = low [u]) <br/>{< br/> CNT ++; <br/> DO <br/> {<br/> V = stack [top --]; <br/> belong [v] = CNT; <br/> instack [v] = 0; <br/>}while (u! = V); <br/>}< br/> int main () <br/>{< br/> int I, j, T, total; <br/> scanf ("% d", & N); <br/> memset (instack, 0, sizeof (instack); <br/> memset (adj, -1, sizeof (adj); <br/> memset (dfn, 0, sizeof (dfn )); <br/> E = Index = CNT = Total = Top = 0; <br/> for (I = 1; I <= N; I ++) <br/>{< br/> scanf ("% d", & K); <br/> total + = K; <br/> while (k --) <br/>{< br/> scanf ("% d", & T); <br/> G [e]. V = N + T; <br/> G [e]. next = adj [I]; <br/> adj [I] = e ++; <br/>}< Br/> for (I = 1; I <= N; I ++) <br/> {<br/> scanf ("% d", & T ); <br/> G [e]. V = I; <br/> G [e]. next = adj [n + T]; <br/> adj [n + T] = e ++; <br/>}< br/> for (I = 1; I <= N; I ++) <br/>{< br/> If (! Dfn [I]) <br/> Tarjan (I); <br/>}< br/> for (I = 1; I <= N; I ++) <br/>{< br/> num = 0; <br/> for (t = adj [I]; t! =-1; t = G [T]. next) <br/> {<br/> If (belong [I] = belong [G [T]. v]) <br/>{< br/> res [num ++] = G [T]. v-N; <br/>}< br/> printf ("% d", num); <br/> sort (Res, res + num); <br/> for (t = 0; t <num; t ++) <br/> {<br/> printf ("% d ", res [T]); <br/>}< br/> printf ("/N"); <br/>}< br/> return 0; <br/>}