萌新60pts求助
查看原帖
萌新60pts求助
235657
hwx12233楼主2020/5/26 18:05
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>

using namespace std;

template <typename T> void chkmax(T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin(T &x, T y) {x = x > y ? y : x;}

typedef long long ll;

const int INF = (1ll << 30);

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl
#define REP(i, l, r) for(int i = (l); i <= (r); i++)
#define PER(i, r, l) for(int i = (r); i >= (l); i--)

template <typename T> void read (T &x) {
	x = 0; bool f = 1; char ch;
	do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
	do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
	x = f ? x : -x;
}

template <typename T> void write (T x) {
	if (x < 0) x = ~x + 1, putchar ('-');
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
}

const int N = 5e5 + 7;
const int M = 3e5 + 7;

struct EDGE {
	int to, nxt;
} edge[M << 2];

struct Quest {
	int a, b;
	char ha, hb;
} q[M];

int n, m, d, E, cnt, top, scc_cnt, s[N], dfn[N], pos[N], low[N], head[N], scc_num[N];
char ch[N];
bool vis[N];

inline int Hash(int a, char A) {
	if (ch[a] == 'a') {
		if (A == 'B') return a;
		else return a + n;
	} 
	if (ch[a] == 'b') {
		if (A == 'A') return a;
		else return a + n;
	} 
	if (ch[a] == 'c') {
		if (A == 'A') return a;
		else return a + n;
	}
}

inline int opposite(int x) {
	return (x + n - 1) % (2 * n) + 1;
}

inline void addedge(int u, int v) {
	edge[++E].to = v;
	edge[E].nxt = head[u];
	head[u] = E;
}

void tarjan (int u) {
    dfn[u] = low[u] = ++cnt;
    s[++top] = u; vis[u] = 1;
    for(int i = head[u]; i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) {
        scc_cnt ++ ;
        while (s[top + 1]  != u) {
            scc_num[s[top]] = scc_cnt;
            vis[s[top -- ]] = 0;
        }
    }
}

inline void pre(char A) {
	if (A == 'a') printf("B");
	else printf("A");
}

inline void nxt(char A) {
	if (A == 'c') printf("B"); 
	else printf("C"); 
}

inline void clear() {
	cnt = E = top = scc_cnt = 0; 
	REP (i, 0, n << 1) vis[i] = dfn[i] = low[i] = head[i] = scc_num[i] = 0;
}

inline bool check() {
	clear();
	REP (i, 1, m) {
		if (q[i].ha == ch[q[i].a]) continue;
		if (q[i].hb == ch[q[i].b]) {
			int u = Hash(q[i].a, q[i].ha);
			addedge(u, opposite(u));
			continue;
		}
		int u = Hash(q[i].a, q[i].ha), v = Hash(q[i].b, q[i].hb);
		addedge(u, v); addedge(opposite(v), opposite(u));
	}
	REP (i, 1, n << 1) if (!dfn[i]) tarjan(i);
	REP (i, 1, n) if (scc_num[i] == scc_num[i + n]) return false;
	REP (i, 1, n) if (scc_num[i] > scc_num[i + n]) nxt(ch[i]); else pre(ch[i]);
	return true;
}

inline void dfs(int dep) {
	if (dep > pos[0]) {
		if (check()) exit(0);
		return;
	}
	ch[pos[dep]] = 'a'; // 可选的位置有(b, c) 
	dfs(dep + 1);
	ch[pos[dep]] = 'b'; // 可选的位置有(a, c) 
	dfs(dep + 1);
}

int main() {
	read(n); read(d); scanf("%s", ch + 1);
	REP (i, 1, n) if (ch[i] == 'x') pos[++pos[0]] = i;
	read(m);
	REP (i, 1, m) read(q[i].a), q[i].ha = getchar(), read(q[i].b), q[i].hb = getchar();
	dfs(1); puts("-1");
	return 0;
}
2020/5/26 18:05
加载中...