题解:P13074 [GCJ 2020 Finals] Replace All
__staring__ · · 题解
模拟赛题解感觉太神秘了,你谷怎么也没有题解。
首先发现对于操作
套路地转到图上考虑,对于操作
缩点,对于一个强连通分量
而当
这样分量内的限制就考虑完了,对于分量间的限制(出边),在绕圈绕到出边的入点时顺手做掉就行。
现在考虑怎么解决分量间的传递,考场上的思路是,先小贪一手,发现会有反悔调整的情况,有点像匹配……网络流?
于是设源汇点
还有一些细节问题没有解决,比如有可能分量
以及孤立分量的情况,如果
由于笔者不想写 tarjan,所以这里的缩点是平方的复杂度。考场代码修改输出后如下,可以发现是个类二分图匹配问题,复杂度
::::info[]
#include <bits/stdc++.h>
using namespace std;
namespace IO
{
constexpr int S = 1 << 20;
char b1[S], *p1, *p2;
#define G (p1 == p2 && (p2 = (p1 = b1) + fread(b1, 1, S, stdin), p1 == p2) ? EOF : *p1 ++)
int read()
{
int x = 0, c = G;
while (!isdigit(c)) c = G;
while (isdigit(c)) x = x * 10 + (c & 15), c = G;
return x;
}
char readC() {return G;}
}
using IO::read;
using IO::readC;
typedef long long LL;
constexpr int N = 128, S = 62, M = 8000 * 2, INF = 1e9;
int n, m;
int idx[128];
int use[N], scc[N], siz[N], cnt[N];
LL gra[N];
int out[N];
int he[N], ne[M], to[M], ca[M];
int cur[N], dep[N], que[N];
void add(int a, int b, int c = INF)
{
to[m] = b, ca[m] = c, ne[m] = he[a], he[a] = m ++;
to[m] = a, ca[m] = 0, ne[m] = he[b], he[b] = m ++;
}
int bfs()
{
memset(dep, 0, sizeof(dep));
memcpy(cur, he, sizeof(cur));
dep[n * 2] = 1, que[0] = n * 2;
for (int hh = 0, tt = 1; hh < tt; hh ++)
for (int u = que[hh], i = he[u]; i; i = ne[i])
{
int v = to[i];
if (!dep[v] && ca[i]) dep[v] = dep[u] + 1, que[tt ++] = v;
}
return dep[n * 2 + 1];
}
int dfs(int u = n * 2, int f = INF)
{
if (u == n * 2 + 1) return f; int r = f;
for (int i = cur[u]; i && r; i = cur[u] = ne[i])
{
int v = to[i];
if (dep[v] != dep[u] + 1 || !ca[i]) continue;
int ff = dfs(v, min(r, ca[i]));
r -= ff, ca[i] -= ff, ca[i ^ 1] += ff;
if (!ff) dep[v] = 0;
}
return f - r;
}
int solve()
{
memset(use, 0, sizeof(use));
memset(gra, 0, sizeof(gra));
while (1)
{
char c = readC();
if (c == ' ') break;
use[idx[c]] = 1;
}
n = read();
while (n --)
{
char a = readC(), b = readC(); readC();
gra[idx[a]] |= 1ll << idx[b];
}
n = 0;
for (int j = 0; j < S; j ++)
for (int i = 0; i < S; i ++)
if (gra[i] >> j & 1) gra[i] |= gra[j];
for (int i = 0; i < S; i ++)
{
int flag = 0;
for (int j = 0; j < i; j ++)
if ((gra[i] >> j & 1) && (gra[j] >> i & 1))
{scc[i] = scc[j], flag = 1; break;}
if (!flag) siz[n] = cnt[n] = 0, scc[i] = n ++;
siz[scc[i]] += 1, cnt[scc[i]] += use[i];
}
m = 2;
memset(out, 0, sizeof(out));
memset(he, 0, sizeof(he));
for (int i = 0; i < S; i ++)
for (int j = 0; j < S; j ++)
if ((gra[i] >> j & 1) && scc[i] != scc[j])
add(scc[i], scc[j]), out[scc[i]] = 1;
for (int i = 0; i < n; i ++)
{
if (out[i] && cnt[i] == siz[i])
{
for (int t = he[i]; t; t = ne[t])
if (~t & 1) add(n + i, to[t]);
add(n * 2, n + i, 1);
}
if (out[i] || siz[i] > 1) cnt[i] -= cnt[i] == siz[i];
add(n * 2, i, cnt[i]), add(i, n * 2 + 1, siz[i]);
}
int res = 0;
while (bfs()) res += dfs();
return res;
}
int main()
{
for (int i = 0; i < 10; i ++) idx[i + '0'] = i;
for (int i = 0; i < 26; i ++) idx[i + 'A'] = i + 10;
for (int i = 0; i < 26; i ++) idx[i + 'a'] = i + 36;
int T = read();
for (int i = 1; i <= T; i ++)
printf("Case #%d: %d\n", i, solve());
return 0;
}
::::