题目链接: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. mybatis中实体类跟数据库属性不一致解决方案

    1.在Mapper.xml映射配置文件中给sql语句起别名 select id as uid,username as name from user 2.mybatis中可以单独的配置查询结果的列名和实 ...

  2. BZOJ:2186: [Sdoi2008]沙拉公主的困惑

    问题:可能逆元不存在吗? 题解: Gcd(a,b)==Gcd(b,a-b); 从数据范围可以看出应该求M!的欧拉函数: 然后通过Gcd转化过去 一开始没想到 #include<iostream& ...

  3. springboot整合mybatis与thymeleaf

    1.创建springboot项目 (1)选择Spring Initializr (2)填写自己的Group 与 Artifact (3)选择依赖框架 等待maven下载好依赖和插件即可 2.主配置文件 ...

  4. Monthly Expense(最大值最小化问题)

                                                                                POJ-3273                 ...

  5. 干货|微软远程桌面服务蠕虫漏洞(CVE-2019-1182)分析

    2019年8月,微软发布了一套针对远程桌面服务的修复程序,其中包括两个关键的远程执行代码(RCE)漏洞,CVE-2019-1181和CVE-2019-1182.与之前修复的"BlueKeep ...

  6. PTA天梯赛L2

    L2-001 紧急救援 题意:就是给你一张n<500的图:让你求最短路径,最短路条数,以及路径: 做法,先用dijkstra求最短路,然后dfs找最短路条数,以及点权的最大值: 一般dfs不就可 ...

  7. c 循环左移

    char b[11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; const int iShift = 4; for (int j = ...

  8. 基于python的小波阈值去噪算法

    https://blog.csdn.net/alwaystry/article/details/52756051 发表于 2018-01-10 16:32:17 嵌入式设计应用 +关注 小波图像去噪原 ...

  9. oracle(9) 序列和约束

    序列 SEQUENCE 也是数据库对象之一,作用:根据指定的规则生成一些列数字. 序列通常是为某张表的主键提供值使用. 主键:通常每张表都会有主键字段,该字段的值要求非空且唯一, 使用该字段来确定表中 ...

  10. 数组 bash shell

    http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html #!/bin/bash # 脚本检测到离线主机会发送邮件通知,可一直运行. ...