题意:n个数,每个2种情况,m组,每组中至少满足2个。
2-sat
#include <bits/stdc++.h>
using namespace std;
const int MOD = int(1e9 + 7);
const int MAX = 2e4 + 5, MAXE = 3e6 + 5;
int n;
int head[MAX], nxt[MAXE], to[MAXE];
int edgeCount;
void addEdge(int f, int t) {
nxt[edgeCount] = head[f];
head[f] = edgeCount;
to[edgeCount++] = t;
}
int low[MAX], dfn[MAX], index;
int stk[MAX], stksz;
int compId[MAX], compnum;
void tarjanDFS(int cur) {
low[cur] = dfn[cur] = index++;
stk[stksz++] = cur;
for (int i = head[cur]; i != -1; i = nxt[i]) {
int j = to[i];
if (compId[j] == -1) {
if (dfn[j] == -1)
tarjanDFS(j);
low[cur] = min(low[cur], low[j]);
}
}
if (low[cur] == dfn[cur]) {
do {
compId[stk[stksz - 1]] = compnum;
} while (stk[--stksz] != cur);
compnum++;
}
}
void SCC() {
compnum = 0;
index = 0;
memset(compId, -1, sizeof(compId));
memset(dfn, -1, sizeof(dfn));
for (int i = 0; i < n; i++) {
if (dfn[i] == -1)
tarjanDFS(i);
}
}
int NOT(int cur) {
return cur ^ 1;
}
void addOR(int i, int j) {
addEdge(NOT(i), j);
addEdge(NOT(j), i);
}
int invComp[MAX], sortedOrder[MAX], in[MAX], sorSize;
vector<vector<int>> adjComp;
int compres[MAX], visComp[MAX];
void dfs(int u) {
if (visComp[u])return;
visComp[u] = true;
for (int nxt : adjComp[u]) {
dfs(nxt);
}
if (compres[u] == -1) {
compres[u] = 1;
int invID = invComp[u];
compres[invID] = 0;
}
}
void assign() {
memset(compres,-1,sizeof(compres));
memset(in,0,sizeof(in));
sorSize = 0;
for(int i=0;i<(int)adjComp.size();i++)
for(int k=0;k<(int)adjComp[i].size();k++)
in[adjComp[i][k]]++;
for(int i=0;i<(int)adjComp.size();i++)
if (!in[i])
dfs(i);
}
bool _2sat() {
SCC();
for(int i=0;i<n/2;i++)
{
if (compId[i*2] == compId[NOT(i*2)])
return false;
invComp[compId[i*2]] = compId[NOT(i*2)];
invComp[compId[NOT(i*2)]] = compId[i*2];
}
adjComp.clear(), adjComp.resize(compnum);
for(int ii=0;ii<n;ii++) {
for (int kk = head[ii]; kk != -1; kk = nxt[kk]) {
int jj = to[kk];
int i = compId[ii], j = compId[jj];
if (i == j)
continue;
adjComp[i].push_back(j);
}
}
assign();
return true;
}
string ans;
pair<int, char> arr[MAX][3];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
edgeCount = 0;
memset(head, -1, sizeof(head));
int k;
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
for (int j = 0; j < 3; j++) {
scanf(" %d %c", &arr[i][j].first, &arr[i][j].second);
arr[i][j].first--;
for (int k = 0; k < j; k++) {
int a = arr[i][j].first*2;
if (arr[i][j].second == 'B') a = NOT(a);
int b = arr[i][k].first*2;
if (arr[i][k].second == 'B') b = NOT(b);
addOR(a, b);
}
}
}
n <<= 1;
if (!_2sat()) {
cout << -1 << "\n";
}
else {
n >>= 1;
for (int i = 0; i < n; i++) {
int ii = compId[i*2];
bool flag = compres[ii];
if (flag)ans += 'R';
else ans += 'B';
printf("%c", flag ? 'R' : 'B');
}
printf("\n");
}
return 0;
}