GCJ 2015r2 (bilingual-min cut)

Source: Internet
Author: User

problem C. Bilingual
Problem

Elliot ' s parents speak French and 中文版 to him at home. He has heard a lot of words, but it isn ' t always clear to him which word comes from which language! Elliot knows one sentence that he's sure is 中文版 and one sentence that he's sure are French, and some other sentences th At could is either 中文版 or French. If A word appears in an 中文版 sentence, it must is a word in 中文版. If A word appears in a French sentence, it must is a word in French.

Considering all the sentences that Elliot have heard, what's the minimum possible number of words that he's heard that Mus T is words in both 中文版 and French?

Input

The first line of the input gives the number of test cases, T. T test Cases follow. Each starts with a, containing an integer N. N lines follow, each of which contains a series of space-separated "words". Each "word" is made the only of lowercase characters a-Z. The first of those N lines is a ' sentence ' in 中文版, and the second is a ' sentence ' in French. The rest could is "sentences" in either 中文版 or French. (Note that the "words" and "sentences" is not guaranteed to being valid in any real language.)

Output

For each test case, output one line containing ' case #x: Y ', where x is the ' Test Case Number ' (starting from 1) and y-is th e minimum number of words that Elliot have heard that must being words in both 中文版 and French.

Limits

1 ≤ T ≤25.
Each word would contain no more than ten characters.
The "known" sentences'll contain no more than-words each.
The "unknown" sentences would contain no more than words each.

Small DataSet

2 ≤ N ≤20.

Large DataSet

2 ≤ N ≤200.

Sample

Input

Output
42he loves to eat Baguettesil aime manger des baguettes4a b c D EF g h i ja b c i jf G H D E4he drove into a  Cul de Sacelle a conduit sa voitureil a conduit dans un cul de sacil mange pendant que il conduit sa voiture6adieu joie de Vivre je ne regrette rienadieu joie de vivre je ne regrette riena b c D EF g h i ja b c i jf G H d e 
Case #1:1Case #2:4Case #3:3Case #4:8
In case #1, Elliot knows for sure, the first sentence are in 中文版 and the second are in French, so there are no Ambigu ity; The only word, that must are in both 中文版 and French is "baguettes".

In case #2, the last of the sentences could either Be:english 中文版, English French, French 中文版, or French French. The second of those possibilities is the one, the minimizes the number of words common to both languages; That set turns out to be D, E, I, and J.


Minimum cut



#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime> #include <map> #include <string> #include <vector>using namespace std; #define for (i,n) for (int i=1;i<=n;i++ ) #define Fork (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int. i=0;i<n;i++) #define ForD (i,n) for (int i=n;i; i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for (int p=pre[x];p, p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a) ), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset (A,128,sizeof (a)), #define INF (2139062143) #define F ( 100000007) #define MAXT (25+10) #define MAXLEN (1000*11+10) #define MAXWORD1 (1000+10) #define MAXWORD2 (Ten) #define Maxtotword (2000+10*200+10) #define MAXN (200+10) #define MAXM (200000+10) #define MAXN (1000000+2) #define MAXM ((1000000+2) *2+100) long long mul (long long A,long long B) {return (a*b)%F;} Long Long (long A,long long B) {return (a+b)%F;} Long Long sub (long A,long long B) {return (a-b+ (a)/f*f+f)%F;}        typedef long Long Ll;class max_flow//dinic+ current arc optimization {public:int n,s,t;        int Q[MAXN];        int edge[maxm],next[maxm],pre[maxn],weight[maxm],size;              void Addedge (int u,int v,int W) {edge[++size]=v;              Weight[size]=w;              Next[size]=pre[u];          Pre[u]=size;         } void Addedge2 (int u,int v,int W) {Addedge (u,v,w), Addedge (v,u,0);}        BOOL B[MAXN];        int D[MAXN];            BOOL SPFA (int s,int t) {for (i,n) D[i]=inf;              MEM (b) d[q[1]=s]=0;b[s]=1;              int head=1,tail=1;                  while (head<=tail) {int now=q[head++]; Forp (now) {int &v=edge[p];                          if (Weight[p]&&!b[v]) {d[v]=d[now]+1;                      B[v]=1,q[++tail]=v;          }}} return b[t];      } int ITER[MAXN];          int dfs (int x,int f) {if (x==t) return F;              Forpiter (x) {int v=edge[p];                    if (Weight[p]&&d[x]<d[v]) {int Nowflow=dfs (v,min (weight[p],f));                      if (nowflow) {weight[p]-=nowflow;                      Weight[p^1]+=nowflow;                    return nowflow;      }}} return 0;          } int Max_flow (int s,int t) {int flow=0;              while (SPFA (s,t)) {for (i,n) iter[i]=pre[i];              int F;           while (F=dfs (S,inf)) flow+=f;  }        return flow;          } void mem (int n,int s,int t) {(*this). N=n;            (*this). t=t;                      (*this). S=s;            size=1;    MEM (PRE)}}s; int t,n;vector<string> Split (string s,string del = "\n\0")//with any character that appears in Del is a delimiter {vector<string> ret;s+=del[0 ];string p= ""; int sz=s.size (); Rep (I,sz) {if (Del.find (S[i]) ==string::npos) {p+=s[i];} Else{if (p!= "") {Ret.push_back (p);p = "";}}} return ret;} Vector<string> get_line_words () {static string Buf;getline (Cin,buf, ' \ n '); return split (BUF);} Map<string,int> H;int get_id (string s) {Map<string,int>::iterator it=h.find (s); if (It==h.end ()) return H[s ]=h.size (); return it->second;} Vector<string> a[maxn];int A2[maxn][maxword1];int Main () {freopen ("gcj2015r2cc-large-practice.in", "R", stdin) ; Freopen ("Gcj2015r2cc-large-practice.out", "w", stdout);cin>>t; for (kcase,t) {h.clear (); scanf ("%d\n", &n); for (i,n) {string s;a[i]=get_line_words (); A2[i][0]=a[i].size (); ReP (j,a2[i][0]) a2[i][j+1]=get_id (A[i][j]);} for (I,n)//{//for (j,a2[i][0]) cout<<a2[i][j]<< ";//cout<<endl;////}//int m = H.size (), s=1,t=2 *m+n; S.mem (t,s,t); for (i,m) {s.addedge2 (i+1,i+1+m,1);} for (J,a2[1][0]) {s.addedge2 (s,1+a2[1][j],inf);} for (J,a2[2][0]) {s.addedge2 (1+a2[2][j]+m,t,inf);} Fork (I,3,n) {for (j,a2[i][0]) {s.addedge2 (2*m+1+i-2,1+a2[i][j],inf); S.addedge2 (1+a2[i][j]+m,2*m+1+i-2,inf); }}int Ans=s.max_flow (s,t);p rintf ("Case #%d:%d\n", Kcase,ans);} return 0;}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

GCJ 2015r2 (bilingual-min cut)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.