Gym 101987K TV Show Game(2-SAT)
题目链接: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)的更多相关文章
- codeforces gym #101987K -TV ShowGame(2-SAT)
题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...
- Codeforces Gym 100338I TV Show 傻逼DFS,傻逼题
Problem I. TV ShowTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- Codeforces gym 100685 A. Ariel 暴力
A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- Gym 101606L - Lounge Lizards - [计算几何+LIS]
题目链接:https://codeforces.com/gym/101606/problem/L 题解: 在同一条线上的所有蜥蜴,他们的斜率都是相通的,换句话说可以直接通过斜率将蜥蜴分组. 每一组即代 ...
- TV Show Game 【2-SAT】
问题 K: TV Show Game 时间限制: 1 Sec 内存限制: 512 MB Special Judge 提交: 51 解决: 10 [提交] [状态] [命题人:admin] 题目描 ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...
- Gym 101917 E 简单计算几何,I 最大流
题目链接 https://codeforces.com/gym/101917 E 题意:给定一个多边形(n个点),然后逆时针旋转A度,然后对多边形进行规约,每个点的x规约到[0,w]范围内,y规约到[ ...
- upc组队赛4 TV Show Game 【2-SAT】
TV Show Game 题目描述 Mr. Dajuda, who is famous for a TV show program, occasionally suggests an interest ...
随机推荐
- Arduino -- functions
For controlling the Arduino board and performing computations. Digital I/O digitalRead() digitalWrit ...
- Day2-T4
原题目 当然这是原题+,要输路径的.所以必须DFS. Describe:DP or DFS code: #include<bits/stdc++.h> using namespace st ...
- 在Mac上使用docker+sql server+Navicat
1. 版本: 2. 安装Kubernetes(并不知道安装这个有什么用) git clone https://github.com/maguowei/k8s-docker-desktop-for-m ...
- k8s CI/CD--Jenkinsfile例子
试用k8s内部jenkins,并且配置好podtemplate pipeline{ agent any stages{ stage('get the code'){ steps{ ...
- MyBatis 关联查询的实现:一对多
有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...
- 利用方法HttpUtility.HtmlEncode来预处理用户输入
利用方法HttpUtility.HtmlEncode来预处理用户输入.这样能阻止用户用链接注入JavaScript代码或HTML标记,比如//Store/Broswe?Genre=<script ...
- centos7-DNS(主从)
一.安装DNS. 本机ip:192.168.1.3 [root@localhost ~]# yum -y install bind bind-utlis 二.编辑配置文件. [root@localho ...
- CCCC 正整数A+B
题意: 本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空格分开.问题是A和B不 ...
- 第一个eclipse maven项目!我超全!
前言:以前一直用idea做东西,今天突然想试一下,没想到配置起来是真的麻烦!!!!会出现各种各样的问题,太晚了,本文只做出几处非常严重的问题,如有疑问,请私信,留言 准备:本文 JDK 1.8 ...
- javaweb02
第一个web服务器程序:开发部署到Tomcat服务器下运行 1).在eclipse新建一个Javaproject2).在java项目下创建web开发的目录结构 -Webcontent -WEB-INF ...