Podcast Lookup

Google Code Jam 2015 Round2

某岛

  • June 1, 2021

About this episode

Problem A

简单扫一遍,对每个箭头,上下左右扫描,如果有孤立的结点那么显然无解。否则如果该箭头不指向其他箭头,那么答案 +=1。

const int N = int(1e2) + 9; char Map[N][N]; int n, m; bool impossible(int x, int y) { for (int i=0;i<n;++i) if="" (i="" !="x" &&="" map[i][y]="" )="" return="" 0;="" for="" (int="" i="0;i<m;++i)" map[x][i]="" 1;="" }="" bool="" to_edge(int="" x,="" int="" y)="" {="" (map[x][y]="=" '^')="" (map[i][y]="" 'v')="" '<')="" (map[x][i]="" '="">') { for (int i=y+1;i<m;++i) if="" (map[x][i]="" !="." )="" return="" 0;="" 1;="" }="" void="" gao()="" {="" int="" z="0;" rd(n,="" m);="" rep(i,="" n)="" rs(map[i]);="" rep_2(i,="" j,="" n,="" m)="" (map[i][j]="" ){="" (impossible(i,="" j))="" puts("impossible");="" return;="" (to_edge(i,="" ++z;="" printf("%d\n",="" z);="" main(){="" #ifndef="" online_judge="" freopen("="" users="" minakokojima="" documents="" github="" acm-training="" workspace="" in.txt",="" "r",="" stdin);="" out.txt",="" "w",="" stdout);="" #endif="" t;="" cin="">> T; REP_1(i, T) { printf("Case #%d: ", i); gao(); } }

</m;++i)></n;++i)>

Problem C

最小割。对每个 word 拆点,流量为 1。对每个 sentence 连所有关联的 word,流量无穷大。然后直接求 sentence0 到 sentence1 的最小割即可。

为啥?这样建模后,考察所有 s 到 t 的路径,中间相邻的每个 word 必定属于同一种语言,且第一个单词必属于 s 语言,最后一个单词必属于 t 语言,则中间必有一个单词同时属于 s、t 语言,此时的最小割既最少有多少种语言同时属于 s、t。

const int N = 5009, M = 2 * 30009; //struct Network_Flow{ int D[N], hd[N], suc[M], to[M], cap[M]; int n, m, s, t; inline void ae(int x, int y, int c){ suc[m] = hd[x], hd[x] = m, to[m] = y, cap[m++] = c, suc[m] = hd[y], hd[y] = m, to[m] = x, cap[m++] = 0; } inline void aee(int x, int y, int c){ suc[m] = hd[x], hd[x] = m, to[m] = y, cap[m++] = c, suc[m] = hd[y], hd[y] = m, to[m] = x, cap[m++] = c; } #define v to[i] #define c cap[i] #define f cap[i^1] bool bfs(){ static int Q[N]; int cz = 0, op = 1; fill(D, D+n, 0), D[Q[0] = s] = 1; while (cz &amp;amp;lt; op){ int u = Q[cz++]; REP_G(i, u) if (!D[v]&amp;amp;amp;&amp;amp;amp;c){ D[Q[op++]=v] = D[u]+1; if (v==t) return 1; } } return 0; } LL Dinitz(){ LL z=0; while (bfs()){ static int cur[N], pre[N]; int u=s;pre[s]=-1;cur[s]=hd[s];while (~u){ #define i cur[u] if (u==t){ int d=INF;for(u=s;u!=t;u=v)checkMin(d,c); z+=d;for(u=s;u!=t;u=v)f+=d,c-=d;u=s; } #undef i int i;for(i=cur[u];i;i=suc[i])if(D[u]+1==D[v]&amp;amp;amp;&amp;amp;amp;c){cur[u]=i,cur[v]=hd[v],pre[v]=u,u=v;break;} if (!i)D[u]=0,u=pre[u]; } } return z; } #undef f #undef c #undef v //} G; void gao(){ RD(n), m = 2; s = 0, t = 1; map&amp;lt;string, int=""&amp;gt; H; REP(i, n) { string s; getline(cin, s); istringstream iss(s); while (iss &amp;amp;gt;&amp;amp;gt; s) { if (!H[s]) H[s] = n+(2*(H.size()-1)); int u = H[s], v = u + 1; //cout &amp;amp;lt;&amp;amp;lt; u &amp;amp;lt;&amp;amp;lt; " " &amp;amp;lt;&amp;amp;lt; v &amp;amp;lt;&amp;amp;lt; " " &amp;amp;lt;&amp;amp;lt; i &amp;amp;lt;&amp;amp;lt; endl; ae(i,u,INF); ae(v,i,INF); } } REP(i, H.size()) { int u = n, v = u+1; ae(u,v,1); n += 2; } OT(Dinitz()); fill(hd, hd+n, 0); } int main(){ #ifndef ONLINE_JUDGE //freopen("/Users/minakokojima/Documents/GitHub/ACM-Training/Workspace/in.txt", "r", stdin); //freopen("/Users/minakokojima/Documents/GitHub/ACM-Training/Workspace/out.txt", "w", stdout); #endif int T; cin &amp;amp;gt;&amp;amp;gt; T; REP_1(i, T) { printf("Case #%d: ", i); gao(); } }

</string,>

Problem D

TBD。
计数题,分类讨论比较复杂,Polay 计数法或者 DP 加维。