Poj1904: http://poj.org/problem? Id = 1904
The king has n sons. Now the N sons should choose their favorite among the N girls. Some may like multiple sons. Finally, the king's guide will give him a match, if there are n matching numbers, it means that a son and a girl can get married. Knowing these conditions, you need to find out which girls each son can marry.
Question: first, the son and the girl who like to build an edge, then the last girl and the son build an edge, and then scale down. The reason for this is still being studied.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <vector> 6 # include <set> 7 using namespace std; 8 vector <int> ans; 9 const int n = 4000 + 30; 10 const int M = 400010; 11 const int INF = 0 xffffffff; 12 struct edge {13 int, next; 14} edge [m]; 15 int n, m, temp, CNT, DEP, top, Atype; 16 int dfn [N], low [N], vis [N], head [N], St [N], belong [N], in [N], out [N], sum [N]; 17 // sum [I] records the vertex of the I-th Connected Graph Number, in [I], out [I], indicates the entry degree and initial degree of the point after the contraction. 18 void Init () {19 CNT = Dep = Top = Atype = 0; 20 memset (Head,-1, sizeof (head); 21 memset (dfn, 0, sizeof (dfn); 22 memset (low, 0, sizeof (low); 23 memset (VIS, 0, sizeof (VIS); 24 memset (belong, 0, sizeof (belong); 25 memset (in, 0, sizeof (in); 26 memset (Out, 0, sizeof (out); 27 memset (sum, 0, sizeof (SUM); 28} 29 void addedge (int u, int v) {30 edge [CNT]. to = V; 31 edge [CNT]. next = head [u]; 32 head [u] = CNT ++; 33} 34 35 void Tarjan (int u) {3 6 dfn [u] = low [u] = ++ Dep; 37 st [top ++] = u; 38 vis [u] = 1; 39 For (INT I = head [u]; I! =-1; I = edge [I]. Next) {40 int v = edge [I]. To; 41 if (! Dfn [v]) {42 Tarjan (V); 43 Low [u] = min (low [u], low [v]); 44} 45 else if (vis [v]) {46 low [u] = min (low [u], dfn [v]); 47} 48} 49 Int J; 50 if (dfn [u] = low [u]) {51 Atype ++; 52 do {53 J = sT [-- top]; 54 belong [J] = Atype; 55 sum [Atype] ++; // records the number of the midpoint of each connected component 56 vis [J] = 0; 57} 58 While (u! = J); 59} 60} 61 int main () {62 while (~ Scanf ("% d", & N) {63 Init (); 64 for (INT I = 1; I <= N; I ++) {65 scanf ("% d", & M); 66 for (Int J = 1; j <= m; j ++) {67 scanf ("% d ", & temp); 68 addedge (I, temp + n); 69} 70} 71 for (INT I = 1; I <= N; I ++) {72 scanf ("% d", & temp); 73 addedge (temp + N, I); 74} 75 for (INT I = 1; I <= 2 * N; I ++) 76 if (! Dfn [I]) 77 Tarjan (I); 78 for (INT I = 1; I <= N; I ++) {79 ans. clear (); 80 for (Int J = head [I]; J! =-1; j = edge [J]. next) {81 int v = edge [J]. to; 82 If (belong [I] = belong [v]) 83 ans. push_back (V-N); 84} 85 int SZ = ans. size (); 86 sort (ans. begin (), ans. end (); 87 printf ("% d", SZ); 88 for (Int J = 0; j <SZ; j ++) {89 printf ("% d", ANS [J]); 90} 91 puts (""); 92} 93} 94}
View code