King ' s Quest
Test instructions: There are n princes and N Sisters; (1 <= N <= 2000) The first Prince likes ki a sister; (see sample) give a perfect match, that is, every prince and his favorite sister married; ask every prince there are several options ( Choose in your favorite sister , and output the optional sister's label (ascending);
Sample Input
4 (N) 2 1 2 (Ki) 2 1 22 2 32 3 41 2 3 4 (Perfect Match)
Sample Output
2 1 22 1 21 31 4
Analysis: Figure matching problem, 1~n for the Prince's number, n~2n for sister's number; input has a forward edge;
Focus: for a given set of matches, as a reverse edge, that is, from the sister point back to the prince, so that after the Tarjan contraction point, you can traverse the side (to the prince's favorite sister's choice ...) See if still in the same strong connected component, if the sister or prince in the same SCC, can be married;
Proof: Why is it possible to have a strong connected component? The side must be connected to the prince and sister , in the premise of not repeating a side, will know that the number of the Prince and sister is the same , and each side of the prince like the conditions of the sister;
PS: The first time the problem using the output plug, very useful Ah!! The time is reduced by at least 1/10 ...
Thinking pit point: think can be directly in the Tarjan contraction point, each strong connected component inside of the sister write vec[], so that after each VEC can be directly ordered, and then call the belong[] output of the SCC is the number of sister. The idea is good, but test instructions AH!!! Not in a connected component of the sister is such a prince like ... So we have to traverse the edge to find the inside of a connected component;
//532ms#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<map>#include<queue>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#definePB Push_backTemplate<typename t>voidRead (T &L) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}Const intN =2020<<1;//Multiplier pointsConst intM =202200;intHead[m],tot;structedge{intTo,w,next;} E[M];voidInsintAintBintW =0) {e[++tot]. Next =Head[a]; E[tot].to=b; E[TOT].W=W; Head[a]=tot;}intPre[n],dfs_clock,low[n];intBelong[n],scc,n;stack<int>S;BOOLStk[n];voidTarjan (intu) {Pre[u]= Low[u] = + +Dfs_clock; S.push (U); Stk[u]=true; intV//the degree to which the connected component of the point U is located; for(inti = Head[u];i;i =E[i]. Next) {v=e[i].to; if(Pre[v] = =0) {Tarjan (v); Low[u]=min (low[u],low[v]); }Else if(Stk[v]) {Low[u]=min (low[u],pre[v]); } } if(Pre[u] = = Low[u]) {//root node of strongly connected components++SCC; Do{v=S.top (); S.pop (); Stk[v]=false; //if (v <= N)BELONG[V] =SCC; //Else VEC[SCC].PB (v);} while(V! =u); }}intAns[n];intMain () {intV,t,kase =1; Read (n); REP1 (U,1, N) { intK; Read (k); Rep0 (J,0, K) {Read (v); INS (u,v+N);//sister marking to add N;}} rep1 (U,1, N) {Read (v); Ins (v+N,U);//Reverse Edge * * *} rep1 (U,1, N)if(Pre[u] = =0) Tarjan (U); REP1 (U,1, N) { intCNT =0; for(inti = Head[u];i;i = E[i]. Next) {//traversing edgesv=e[i].to; if(Belong[u] = = Belong[v])//The same strong connected componentans[cnt++] = VN; } sort (Ans,ans+CNT); out(CNT); Rep0 (i,0, CNT) {Putchar (' '); out(Ans[i]); } puts (""); } return 0;}
View Code
POJ 1904 King ' s Quest