Problem C. Bilingual



Problem

Elliot's parents speak French and English 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 English and one sentence that he's sure is French, and
some other sentences that could be either English or French. If a word appears in an English sentence, it must be a word in English. If a word appears in a French sentence, it must be a word in French.

Considering all the sentences that Elliot has heard, what is the minimum possible number of words that he's heard that must be words in both English and French?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each starts with a single line containing an integer NN lines follow, each of which contains a series
of space-separated "words". Each "word" is made up only of lowercase characters a-z. The first of those N lines is a "sentence" in English, and the second is a "sentence" in French. The rest could be "sentences" in either English or French.
(Note that the "words" and "sentences" are not guaranteed to be 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 the minimum number of words that Elliot has heard that must be words in both English and French.

Limits

1 ≤ T ≤ 25.

Each word will contain no more than 10 characters.

The two "known" sentences will contain no more than 1000 words each.

The "unknown" sentences will contain no more than 10 words each.

Small dataset

2 ≤ N ≤ 20.

Large dataset

2 ≤ N ≤ 200.

Sample



Input 

 


Output 

 
4
2
he loves to eat baguettes
il aime manger des baguettes
4
a b c d e
f g h i j
a b c i j
f g h d e
4
he drove into a cul de sac
elle a conduit sa voiture
il a conduit dans un cul de sac
il mange pendant que il conduit sa voiture
6
adieu joie de vivre je ne regrette rien
adieu joie de vivre je ne regrette rien
a b c d e
f g h i j
a b c i j
f g h d e
Case #1: 1
Case #2: 4
Case #3: 3
Case #4: 8

In Case #1, Elliot knows for sure that the first sentence is in English and the second is in French, so there is no ambiguity; the only word that must be in both English and French is "baguettes".



In Case #2, the last two sentences could either be: English English, English French, French English, or French French. The second of those possibilities is the one that minimizes the number of words common to both languages; that set turns out to be d, e, i,
and j.

最小割

#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 (10)
#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 add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class Max_flow //dinic+当前弧优化
{
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") // 以在del出现过的不论什么字符为分隔符
{
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);
printf("Case #%d: %d\n",kcase,ans);
} return 0;
}

GCJ 2015R2(Bilingual-最小割)的更多相关文章

  1. BZOJ 1391: [Ceoi2008]order [最小割]

    1391: [Ceoi2008]order Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1509  Solved: 460[Submit][Statu ...

  2. BZOJ-2127-happiness(最小割)

    2127: happiness(题解) Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1806  Solved: 875 Description 高一 ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. BZOJ3438 小M的作物(最小割)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=3438 Description 小M在MC里开辟了两块巨大的耕地A和B(你可以认为 ...

  5. 最大流-最小割 MAXFLOW-MINCUT ISAP

    简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...

  6. bzoj1412最小割

    太羞耻了,m n写反了(主要是样例n m相等) 建图方法比较高(ji)端(chu),对于可以加栅栏的地方连上1的边,然后求最小割即可 为了让代码优(suo)美(duan),我写了一个check,避免多 ...

  7. 【BZOJ1497】[NOI2006]最大获利 最小割

    裸的最小割,很经典的模型. 建图:要求总收益-总成本最大,那么将每条弧与源点相连,流量为成本,每个收益与汇点相连,流量为收益,然后每条弧与它所能到达的收益相连,流量为inf. 与源点相连的是未被选中的 ...

  8. 二分图&网络流&最小割等问题的总结

    二分图基础: 最大匹配:匈牙利算法 最小点覆盖=最大匹配 最小边覆盖=总节点数-最大匹配 最大独立集=点数-最大匹配 网络流: 技巧: 1.拆点为边,即一个点有限制,可将其转化为边 BZOJ1066, ...

  9. CQOI 2016 不同的最小割

    题目大意:一个无向图,求所有点对不同的最小割种类数 最小割最多有n-1个,这n-1个最小割构成一个最小割树 分治法寻找n-1个最小割.对于当前点集X,任选两点为ST做最小割,然后找出与S相连的所有点和 ...

随机推荐

  1. Unix domain socket IPC

    UNIX Domain socket 虽然网络socket也可用于同一台主机的进程间通讯(通过lo地址127.0.0.1),但是unix domain socket用于IPC更有效率:不需要经过网络协 ...

  2. Ubuntu下安装SSH服务

    判断是否安装ssh服务,可以通过如下命令进行: $ ssh localhost ssh: connect to host localhost port 22: Connection refused 如 ...

  3. java程序中输出console的日志到文本

    http://blog.sina.com.cn/s/blog_76a8411a01010u2h.html 首先:当我们引入data-integration\lib文件夹下的所有jar包后 运行java ...

  4. 关于Unity中的摄像机

    摄像机是挂载Camera组件的能把3D世界物体拍摄成2D画面显示到屏幕上面的节点,角度不一样,位置不一样,拍摄出来的东西就不一样. Clear Flags:没有物体的时候,摄像机拍摄出的屏幕要绘制什么 ...

  5. malloc 函数本身并不识别要申请的内存是什么类型

    malloc 函数本身并不识别要申请的内存是什么类型,它只关心内存的总字节数.我 们通常记不住 int, float 等数据类型的变量的确切字节数. 例如 int 变量在 16 位系统 下是 2 个字 ...

  6. 【BZOJ】1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(背包+高精度)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1655 背包就没什么好说的了,裸的完全背包.. 但是我一开始交开了ull都wa了T_T.. 精度太大. ...

  7. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛(差分序列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1635 差分序列是个好东西啊....很多地方都用了啊,,, 线性的进行区间操作orz 有题可知 h[a ...

  8. iframe超时处理。。。。

    function iframeTimeOut(url, timeOut_callback, width, height) { /// <summary> /// iframe超时处理 // ...

  9. Unity Shaders and Effects Cookbook (3-5) 金属软高光

    书上这一节看得我头昏脑胀,数学渣表示自理不能-- 并且也不了解这个效果的实际意义. 先记录下来,后面真正看懂了再来补充具体理论. 通过一张纹理贴图,定义高光的形状,利用到的纹理贴图有三种 这里并非把纹 ...

  10. Python3x 爬取妹子图

    思路:1.get_totalpages(url)  通过[性.感.美.女.图]获得该版块的总页数 [首页1234567891011下一页末页共 21页1034条] 2.get_sercoverurl( ...