Hall定理证明可行性来贪心 + 模拟断流与增流: CF1009G

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/135333029

https://www.luogu.com.cn/problem/CF1009G

显然是流,然后贪心,然后要每次流一下证明可行性,这里提供两种解决方法:

Hall定理

左边点数只有6,我们直接 262^6 跑Hall定理

模拟断流

用EK实现网络流。我们直接少流当前位置的。显然一条流最多经过14个点,因此复杂度是对的。

Hall定理做法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 100010
int n, m, i, j, k, T;
int c[N], vis[N], mp[N], s, t;
char str[N];

bool check() {
int i;
for(s=1; s<(1<<6); ++s) {
int s1 = 0, s2 = 0;
for(i=0; i<6; ++i) if(s&(1<<i)) s1 += c[i];
for(t = s; t; t = (t-1) & s) s2 += mp[t];
if(s1 < s2) return false;
}
return true;
}

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
scanf("%s", str+1); n=strlen(str+1); m=read();
for(i=1; i<=n; ++i) c[str[i]-'a']++, vis[i]=(1<<6)-1;
for(i=1; i<=m; ++i) {
k=read(); scanf("%s", str+1); vis[k]=0;
for(j=1; str[j]; ++j) vis[k]|=(1<<str[j]-'a');
}
for(i=1; i<=n; ++i) if(!vis[i]) return printf("Impossible"), 0;
for(i=1; i<=n; ++i) debug("%d ", vis[i]); debug("\n");
for(i=0; i<6; ++i) debug("%d ", c[i]); debug("\n");
for(i=1; i<=n; ++i) mp[vis[i]]++;
for(i=1; i<=n; ++i) {
--mp[vis[i]];
for(j=0; j<6; ++j) {
if(!c[j]) continue;
if( (vis[i]|(1<<j)) != vis[i] ) continue;
--c[j];
if(check()) break;
++c[j];
}
if(j == 6) return printf("Impossible"), 0;
str[i] = j + 'a';
debug("%d : %c\n", i, str[i]);
}
str[i] = 0;
printf("%s", str+1);
return 0;
}