Description
Solution
考虑枚举每一个x具体代表的是a,b,c中的哪个。
现在的问题是如何判断一个地图方案可行。
这显然是一个2-SAT。
如果不会2-SAT,这里推荐天天踩我的zjp_shadow的博客
对于每一个限制:
1.
hi=Si
h
i
=
S
i
,可以直接忽略这个限制。
2.
hi≠Si,hj=Sj
h
i
≠
S
i
,
h
j
=
S
j
,也就是我们不能选择
hi
h
i
,那么就从
hi
h
i
向
¬hi
¬
h
i
连边
3. 否则我们就从
hi
h
i
向
hj
h
j
连边,同时其逆否命题也要连边。
有一个优化:每个x只需枚举ab即可,因为ab涵盖了c的情况。
时间复杂度
O(2d(n+m))
O
(
2
d
(
n
+
m
)
)
Code
/**************************************
* Au: Hany01
* Prob: [UOJ317][BZOJ4945][NOI2017] 游戏
* Date: Apr 5th, 2018
* Email: hany01@foxmail.com
**************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _, __;
for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 50005, maxm = 200005, maxk = 100005, maxv = 100005;
struct Limit { int i, hi, j, hj; }p[maxk];
int n, m, S[maxn], dfn[maxv], low[maxv], beg[maxv], nex[maxm], v[maxm], e, tim, mp[maxv], id[maxn][3], d, tot, isfound, stk[maxv], sccs, co[maxv], isin[maxv], top, pos[maxn], cnt;
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
void tarjan(int u)
{
dfn[u] = low[u] = ++ tim, stk[++ top] = u, isin[u] = 1;
for (register int i = beg[u]; i; i = nex[i])
if (!dfn[v[i]]) tarjan(v[i]), chkmin(low[u], low[v[i]]);
else if (isin[v[i]]) chkmin(low[u], dfn[v[i]]);
if (dfn[u] == low[u]) {
++ sccs;
do co[stk[top]] = sccs, isin[stk[top]] = 0; while (stk[top --] != u);
}
}
#define ano(a, b) (3 - (a) - (b))
inline void check()
{
//Init
e = tim = tot = 0, Set(beg, 0), Set(dfn, 0);
For(i, 1, n) rep(j, 3) if (S[i] != j)
id[i][j] = ++ tot, mp[tot] = j;
//Build the 2-SAT graph
For(i, 1, m) if (p[i].hi != S[p[i].i]) {
if (p[i].hj != S[p[i].j])
add(id[p[i].i][p[i].hi], id[p[i].j][p[i].hj]),
add(id[p[i].j][ano(p[i].hj, S[p[i].j])], id[p[i].i][ano(p[i].hi, S[p[i].i])]);
else add(id[p[i].i][p[i].hi], id[p[i].i][ano(p[i].hi, S[p[i].i])]);
}
//Get the SCCs by algorithm Tarjan
For(i, 1, tot) if (!dfn[i]) tarjan(i);
//Judge if it is legal
rep(i, n) if (co[i << 1 | 1] == co[(i + 1) << 1]) return ;
isfound = 1;
//Print
For(i, 1, n)
if (co[(i - 1) << 1 | 1] < co[i << 1]) putchar(mp[(i - 1) << 1 | 1] + 65);
else putchar(mp[i << 1] + 65);
putchar('\n');
}
void dfs(int cur)
{
if (clock() * 1.0 / CLOCKS_PER_SEC > 0.9) { puts("-1"); exit(0); }
if (cur > cnt) { check(); return ; }
S[pos[cur]] = 0, dfs(cur + 1);
if (isfound) return ;
S[pos[cur]] = 1, dfs(cur + 1);
}
int main()
{
#ifdef hany01
File("uoj317");
#endif
static char c, ss[maxn];
n = read(), d = read(), scanf("%s", ss + 1), m = read();
For(i, 1, m)
p[i].i = read(), scanf("%c", &c), p[i].hi = c - 65,
p[i].j = read(), scanf("%c", &c), p[i].hj = c - 65;
For(i, 1, n) {
S[i] = ss[i] - 97;
if (S[i] > 2) S[i] = 3, pos[++ cnt] = i;
}
dfs(1);
if (!isfound) puts("-1");
return 0;
}