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 ...
随机推荐
- SqlServer 集合运算符
1.集合运算符概述 (1)集合运算符运用与集合之间的运算. (2)多元集合: 指的是来自两个输入查询的集合,可能包含重复项 (3)T-SQL 支持三种集合运算符 union .intersect .e ...
- web应用中并发控制的实现,各种锁的集合
参考:http://blog.csdn.net/xiangwanpeng/article/details/55106732 B/S构架的应用越来越普及,但由于它有别于C/S构架的特殊性,并发控制始终没 ...
- 由于TableView的Section的头部和尾部高度设置的不规范引起的部分Section中的图片无法正常显示
当tableview的组的头部和尾部的高度设置如下时: -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
- 关于fdisk命令
fdisk命令用于观察硬盘实体使用情况,也可对硬盘分区. [root@loclhost ~]# fdisk /dev/sdb Command (m for help): m #输入m列出常用的命令 C ...
- 一天一个设计模式——工厂方法(FactoryMethod)模式
一.模式说明 在前一个模板方法(Template Method)模式中,父类定义了处理流程,而流程中用到的方法交给子类去实现.类似的,在工厂方法模式中,父类决定如何生成实例,但并不决定所要生成的具体类 ...
- Codeforces 1296C - Yet Another Walking Robot
题目大意: 给定一个机器人的行走方式 你需要取走一段区间 但要保证取走这段区间后机器人最终到达的终点位置是不变的 问这段区间最短时是哪一段 解题思路: 易得,如果重复走到了某些已经走过的点,那么肯定就 ...
- POJ 1731:Orders
Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9940 Accepted: 6048 Descriptio ...
- salt如何查看文档帮助
1.查看普通模块和函数使用方法 salt 'minion' sys.doc module_name salt ‘minion' sys.doc module_name.function_name ...
- SpingBoot项目搭建(详细)
SpingBoot (原创:黑小子-余) springboot官网:->点击<- spring官网:->点击<- 一.SpringBoot简介 Spring Boot是由Piv ...
- dll hook 共享内存数据
unit UnitDll; interface uses Windows; * ; // 文件映射到内存的大小 const HOOK_MEM_FILENAME = 'MEM_FILE'; // 映像文 ...