题目链接:https://vj.z180.cn/b4aacc08fc7aab6ce14e7baf13816c24?v=1571542994

题目要求n个灯(R,B),给出m组赋值方式,每一组中至少有两个是正确的,问是否能找到一组正确的赋值方式.

2-SAT模板运用强连通分量解决此类真值指派问题.

对于一组赋值: a x b y c z 来说

若a假,则bc为真

若b假,则ac为真

若c假,则ab为真

建边跑SCC(强连通分量即可)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + ;
const int inf = 0x3f3f3f3f;
int dfn[maxn],low[maxn],head[maxn],belong[maxn];
//dfs序编号,最早能访问到的祖先编号,belong[i]代表i属于哪一个强连通分量
int num[maxn];//num[i]代表i这个强连通分量中有多少个元素
stack<int>sta;
int dfs_clock,cnt,scc_cnt,n,m;//DFS中的编号,边的编号,强连通分量的编号
struct node
{
int v,next;
}e[maxn];
void add(int u,int v)
{
e[cnt]=(node){v,head[u]};
head[u]=cnt++;
}
void DFS(int u)
{
sta.push(u);
low[u]=dfn[u]=++dfs_clock;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!dfn[v]){
DFS(v);
low[u]=min(low[u],low[v]);
}
else if(!belong[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){//以u为起点的搜索子树是一个强连通分量
scc_cnt++;
for(;;){
int now=sta.top();
sta.pop();
belong[now]=scc_cnt;
if(now==u)break;
}
}
}
int two_sat(){
for(int i = ;i <= * n;i++){
if(!dfn[i]){
DFS(i);
}
}
for(int i = ;i <= n;i++){
if(belong[i] == belong[i+n])return ;
}
return ;
}
int x,y,z,xx,yy,zz;
int main(){
ios::sync_with_stdio();
cin >> n >> m;
for(int i = ;i <= n * ;i++)head[i] = -;
for(int i = ;i <= m;i++){
int a,b,c;
char s1,s2,s3;
cin >> a >> s1 >> b >> s2 >> c >> s3;
if(s1 == 'R')x = ;
else x = ;
if(s2 == 'R')y = ;
else y = ;
if(s3 == 'R')z = ;
else z = ;
xx = x ^ ;
yy = y ^ ;
zz = z ^ ;
add(a + xx * n,b + y * n);
add(a + xx * n,c + z * n);//a错,bc肯定对
add(b + yy * n,a + x * n);
add(b + yy * n,c + z * n);//b错,ac肯定对
add(c + zz * n,a + x * n);
add(c + zz * n,b + y * n);//c错,ab肯定对
}
int x = two_sat();
if(x){
for(int i = ;i <= n;i++){
if(belong[i] > belong[i+n])cout << 'R';
else cout << 'B';
}
cout << endl;
}
else cout << - << endl;
return ;
}

Gym 101987K TV Show Game(2-SAT)的更多相关文章

  1. codeforces gym #101987K -TV ShowGame(2-SAT)

    题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...

  2. Codeforces Gym 100338I TV Show 傻逼DFS,傻逼题

    Problem I. TV ShowTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  3. Codeforces gym 100685 A. Ariel 暴力

    A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...

  4. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  5. Gym 101606L - Lounge Lizards - [计算几何+LIS]

    题目链接:https://codeforces.com/gym/101606/problem/L 题解: 在同一条线上的所有蜥蜴,他们的斜率都是相通的,换句话说可以直接通过斜率将蜥蜴分组. 每一组即代 ...

  6. TV Show Game 【2-SAT】

    问题 K: TV Show Game 时间限制: 1 Sec  内存限制: 512 MB  Special Judge 提交: 51  解决: 10 [提交] [状态] [命题人:admin] 题目描 ...

  7. (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest

    layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...

  8. Gym 101917 E 简单计算几何,I 最大流

    题目链接 https://codeforces.com/gym/101917 E 题意:给定一个多边形(n个点),然后逆时针旋转A度,然后对多边形进行规约,每个点的x规约到[0,w]范围内,y规约到[ ...

  9. upc组队赛4 TV Show Game 【2-SAT】

    TV Show Game 题目描述 Mr. Dajuda, who is famous for a TV show program, occasionally suggests an interest ...

随机推荐

  1. C++编程学习(五) C++ 存储类

    一.auto 存储类 根据初始化表达式自动推断被声明的变量的类型. auto f=3.14; //double auto s("hello"); //const char* aut ...

  2. 刷题48. Rotate Image

    一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...

  3. 63.Python中contains和icontains

    1. contains: 进行大小写敏感的判断,某个字符串是否包含在指定的字段中,这个判断条件使用大小写敏感进行判断,因此在被翻译成"SQL"语句的时候,会使用"like ...

  4. JDBC模板CRUD

    创建一个Util工具类 创建工具类为了方便后期管理,例如对数据库做出修改的时候只需要在工具类里修改一处即可! import java.sql.Connection; import java.sql.D ...

  5. 文献及代码阅读报告 - SS-LSTM:A Hierarchical LSTM Model for Pedestrian Trajectory Prediction

    概览 简述 SS-LSTM全称Social-Scene-LSTM,是一种分层的LSTM模型,在已有的考虑相邻路人之间影响的Social-LSTM模型之上额外增加考虑了行人背景的因素.SS-LSTM架构 ...

  6. vue组件化应用构建

    组件系统是 Vue 的另一个重要概念,因为它是一种抽象,允许我们使用小型.独立和通常可复用的组件构建大型应用.仔细想想,几乎任意类型的应用界面都可以抽象为一个组件树: 在 Vue 里,一个组件本质上是 ...

  7. Flink并行度

    并行执行 本节介绍如何在Flink中配置程序的并行执行.FLink程序由多个任务(转换/操作符.数据源和sinks)组成.任务被分成多个并行实例来执行,每个并行实例处理任务的输入数据的子集.任务的并行 ...

  8. POJ 1011:Sticks 经典搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 128734   Accepted: 30173 Descrip ...

  9. Mysql插入数据里有中文字符出现Incorrect string value的错误

    问题:Mysql插入数据里有中文字符出现Incorrect string value的错误   描述:CMD里直接敲代码插入数据   提示的部分截取为:ERROR 1366 (HY000): Inco ...

  10. E. Double Elimination (DP)

    题目:传送门 题意:有 2^n 个人进行比赛,对他们编号 1~2^n,起初1和2打,3和4打,5和6打,7和8打...,然后1和2打完胜利的 和 3和4打完胜利的再打一场,1和2打完失败的和3和4打完 ...