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 ...
随机推荐
- Egret Engine 2D - 矢量绘图
绘制矩形 drawRect 绘制矩形边 lineStyle( 10, 0x00ff00 清空绘图 clear 绘制园形 drawCircle 绘制直线 moveTo lineTo 绘制曲线 cur ...
- iOS基础——通过案例学知识之xib、plist、mvc
透过案例学习xib的使用.plist的使用.mvc在iOS的使用,今天要做的案例效果图 1.xib和nib xib文件可以被XCode编译成nib文件,xib文件本质上是一个xml文件,而nib文件就 ...
- PAT A1018
A 1018 Public Bike Management 这个题目算是比较典型的一个.我分别用dfs,及dijkstra+dfs实现了一下. dfs实现代码: #include <cstdio ...
- PAT 2019 春
算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了. 7-1 Sexy Primes 读懂题意就行. #include <cstdio> #include & ...
- Python多线程,线程死锁及解决,生产者与消费者问题
1.Thread类 普通调用 t = Thread(target=test, args=(i,)) # test为目标函数名, 若函数需要参数将其以元组形 # 式赋给args, 若无参数可不写 t.s ...
- GPU 、APU、CUDA、TPU、FPGA介绍
购买显卡主要关注:显存.带宽和浮点运算数量 GPU :图形处理器(英语:Graphics Processing Unit,缩写:GPU),又称显示核心.视觉处理器.显示芯片,是一种专门在个人电脑. ...
- C语言笔记 15_标准库&locale&math&setjmp&signal&stdarg&stddef
<locale.h> 简介 locale.h 头文件定义了特定地域的设置,比如日期格式和货币符号.接下来我们将介绍一些宏,以及一个重要的结构 struct lconv 和两个重要的函数. ...
- JAVA初学者——Hello,World!
大家好,我是浩宇大熊猫 我本科专业学的是GIS(Geographical Information System),大学期间也学习了很多的编程语言,有C/C++/JAVA等 之前给我们授课的是韩冰老师, ...
- Python之路,Day1 - Python基础1 介绍、基本语法、流程控制
本节内容 1.python介绍 2.发展史 3.python 2.x or python 3.x ? 4.python 安装 5.第一个程序 Hello World 程序 6.变量 7.用户输入 8. ...
- XML文件读写编码不是UTF-8的问题
FileWriter和FileReader在写.读文件时,使用系统当前默认的编码方式. 在中文win下encoding基本是GB2312,在英文win下基本是ISO-8859-1.所以要创建一个UTF ...