I really can't figure out how to build it.
I think it is to find every teacher in the SCC of all the fate of it.
Transfer from http://www.cnblogs.com/whatbeg/p/3765621.html
Practice: To build a map, each teacher and the fate it wants to have a forward edge, and then, in the initial match given to the opposite side, that is, if the first party teacher now found is the fate of U, then build edge u->i. This finds all the strong connected components, each strong connected component of the Chinese teacher and the number of predestination must be equal, so each teacher can find with him in the same strong connected component of the fate, because the strong connected components in each point is accessible, a teacher found its strong connectivity components in the non-original point, Then the original fate must be able to find in the strong connectivity components of the "sweetheart." You can draw a picture to see it.
Due to the construction of the non-dichotomy diagram, the fate of the number from n+1, to 2n.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <cmath> -#include <stdlib.h> -#include <vector> the#include <stack> -#include <Set> - using namespacestd; - #defineINF 1000000007 + #defineMAXN 4010 - #defineMod 1000007 + #defineN 100007 A #defineNN 30 at #defineSigma_size 3 - Const intMAXN = 6e5 +Ten; - using namespacestd; -typedefLong LongLL; - Const DoublePI = ACOs (-1); - in -vector<int>G[MAXN]; to intDFN[MAXN], LOW[MAXN], INSTK[MAXN], SCCNO[MAXN], ANS[MAXN]; +stack<int>S; - intTime , CNT, N, Res; the voidTarjan (intu) * { $Dfn[u] = Low[u] = + +Time ;Panax Notoginseng s.push (u); -Instk[u] =1; the for(inti =0; I < g[u].size (); ++i) { + intv =G[u][i]; A if(!Dfn[v]) { the Tarjan (v); +Low[u] =min (Low[u], low[v]); - } $ Else if(Instk[v]) { $Low[u] =min (Low[u], dfn[v]); - } - } the if(Low[u] = =Dfn[u]) { -cnt++;Wuyi intv; the Do{ -v =s.top (); Wu S.pop (); -INSTK[V] =0; AboutSCCNO[V] =CNT; $} while(V! =u); - } - } - A voidFIND_SCC () + { the for(inti =1; I <= N; ++i) - if(!Dfn[i]) Tarjan (i); $ } the the voidInit () the { the for(inti =0; I <= N; ++i) - g[i].clear (); inTime = CNT =0; thememset (DFN,0,sizeof(DFN)); thememset (Low,0,sizeof(Low)); Aboutmemset (Sccno,0,sizeof(SCCNO)); thememset (INSTK,0,sizeof(INSTK)); the while(!S.empty ()) S.pop (); the } + - intMain () the {Bayi intk, u, v; the while(~SCANF ("%d",&N)) { the init (); - for(inti =1; I <= N; ++i) { -scanf"%d", &k); the for(intj =0; J < K; ++j) { theCIN >>v; theG[i].push_back (v +n); the } - } the for(inti =1; I <= N; ++i) { thescanf"%d", &v); theG[v +N].push_back (i);94 } the FIND_SCC (); the for(intU =1; U <= N; ++u) { theK =0;98 for(inti =0; I < g[u].size (); ++i) { Aboutv =G[u][i]; - if(Sccno[u] = =Sccno[v])101ans[k++] = v-N;102 }103Sort (ans, ans +k);104printf"%d", k); the for(inti =0; I < K; ++i)106printf"%d", Ans[i]);107Puts"");108 }109 } the return 0;111}
Uestc Fang teacher and Fate