King ' s Quest
Time limit:15000ms Memory limit:65536k
Case Time Limit:2000ms
Description
Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew on each of his sons which of those girls he does like. The sons of the king were young and light-headed, so it is possible for a son to like several girls.
So the King asked his wizard to find for each of his sons the girl he liked, so that he could marry her. And the King ' s wizard did it–for each son the girl that he could marry is chosen, so that he liked this girl and, of the CO Urse, each beautiful girl had to marry only one of the king's sons.
However, the king looked at the list and said: "I like the list that you had made, but I am not completely satisfied. For each son I would like to know all the girls that he can marry. Of course, after he marries any of those girls, for each other son you must still is able to choose the girl he likes to M Arry. "
The problem the King wanted the wizard to solve had become too hard for him. You must save Wizard's head by solving this problem.
Input
The first line of the input contains n–the number of King ' s sons (1 <= N <= 2000). Next N lines for each of the king's sons contain the list of the girls he likes:first ki–the number of those girls, and the n Ki different integer numbers, ranging from 1 to N denoting the girls. The sum of all Ki does is not exceed 200000.
The last line of the case contains the original list the wizard had made–n different integer numbers:for each son the N Umber of the girl he would marry in compliance with this list. It is guaranteed, and the list is correct, which is, each son likes the girl he must marry according to this list.
Output
Output N lines. For each king's son first print li–the number of different girls he likes and can marry so if he marriage it is Possible to marry each of the other King ' s sons. After the print Li different integer numbers denoting those girls, in ascending order.
Sample Input
4
2 1 2
2 1 2
2 2 3
2 3 4
1 2 3 4
Sample Output
2 1 2
2 1 2
1 3
1 4
Hint
This problem have huge input and output data,use scanf () and printf () instead of CIN and cout to read data to avoid time Li MIT Exceed.
Source
Northeastern Europe 2003
Test Instructions: give a binary graph and one of the complete matches, for each x point, all the Y points that can be matched, so that a complete match can still be obtained.
Ideas:
You can refer to here: Polla analysis is very good.
Just see this question will go to the two-figure matching on the thought (although I went directly to see what is a stable marriage problem, but this problem and stable marriage does not matter). If you omit the given complete match, you can enumerate all Y-points and X matches, and then make a two-match decision. But the complexity is too high and is bound to tle.
The complete match given must contain some useful information. Since the information in X is all given in the input data, the complete match must contain some information about Y. Some x points are definitely not with the Y point, for example, the Prince No. 3rd and number 2nd girls must not be together, because in this case, Prince 1th and Prince 2nd are bound to have a single person. The information that the complete match gives is the "x-point that every Y-point can like". Although not to full, but a sufficient.
X points to the preferred Y-point, and then the Y-point of the given complete match to the X-point is also connected to the edge. Assuming that the complete match is all Xi and Yi matches, then if Xi and YJ match, Yi and XJ are bound to look for another companion, Imagine Xi and YJ in the same strong connected component, the strong connected component must contain a ring, then XJ and Yi must be able to find another companion.
so is it that as long as we give a complete set of matches, all the other complete matching schemes are in the same strong connected component?
I think there should be no mistake in saying so. There should be some common information between all of the complete matching schemes. That must be a ring.
P.S. If the output output plug, g++ evaluation can be 500+ms. However, the IO plug-in is not valid for C + + evaluation.
The code is as follows:
/* * ID:J.SURE.1 * PROG: * lang:c++ * *#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define PB push_back#define LL Long Longusing namespace STD;Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-8;/****************************************/Const intN =4444, M =222222;intN, tot, scc_cnt, deep;intHead[n], Dfn[n], scc_id[n];structEdge {intV, next; Edge () {} Edge (int_v,int_next): V (_v), Next (_next) {}}e[m]; Stack <int>S vector<int>AnsvoidInit () {tot = deep = scc_cnt =0;memset(Head,-1,sizeof(head));memset(DFN,0,sizeof(DFN));memset(SCC_ID,0,sizeof(scc_id));}voidAddintUintV) {E[tot] = Edge (V, Head[u]); Head[u] = tot++;}intDfsintu) {intLowu = Dfn[u] = ++deep; S.push (U); for(inti = Head[u]; ~i; i = e[i].next) {intv = e[i].v;if(!dfn[v]) {intLOWV = DFS (v); Lowu = min (Lowu, LOWV); }Else if(!scc_id[v]) {Lowu = min (Lowu, dfn[v]); } }if(Lowu = = Dfn[u]) {scc_cnt++; while(1) {intx= S.top (); S.pop (); SCC_ID[X] = scc_cnt;if(x = = u) Break; } }returnLowu;}voidTarjan () { for(inti =1; I <=2*n; i++) {if(!dfn[i]) DFS (i); }}intMain () {#ifdef j_sureFreopen ("000.in","R", stdin);//freopen ("999.out", "w", stdout);#endif while(~scanf("%d", &n)) {intM, J; Init (); for(inti =1; I <= N; i++) {scanf("%d", &m); while(m--) {scanf("%d", &j); Add (i, j+n); } } for(inti =1; I <= N; i++) {scanf("%d", &j); Add (j+n, i); } Tarjan (); for(intU =1; U <= N; u++) {ans.clear (); for(inti = Head[u]; ~i; i = e[i].next) {intv = e[i].v;if(Scc_id[u] = = Scc_id[v]) ans. PB (V-N); } size_t len = Ans.size ();printf("%d", (int) len); Sort (Ans.begin (), Ans.end ()); for(size_t k =0; K < Len; k++) {printf("%d", Ans[k]); }puts(""); } }return 0;}
"Connected Graph | Binary match + strong connected component" POJ-1904 King's Quest