TV Show Game 【2-SAT】
问题 K: TV Show Game
时间限制: 1 Sec 内存限制: 512 MB Special Judge
提交: 51 解决: 10
[提交] [状态] [命题人:admin]
题目描述
Mr. Dajuda, who is famous for a TV show program, occasionally suggests an interesting game for the audience and gives them some gifts as a prize. The game he suggested this week can be explained as follows.
The k(> 3) lamps on the stage are all turned off at the beginning of the game. For convenience, lamps are numbered from 1 to k. Each lamp has a color, either red or blue. However, the color of a lamp cannot be identified until it is turned on. Game participants are asked to select three lamps at random and to guess the colors of them. Then each participant submits a paper on which the predicted colors of selected lamps are recorded to Mr. Dajuda, the game host. When all the lamps are turned on, each participant checks how many predicted colors match the actual colors of the lamps. If two or more colors match, he/she will receive a nice gift as a prize.
Mr. Dajuda prepared a special gift today. That is, after reviewing all the papers received from the game participants he tries to adjust the color of each lamp so that every participant can receive a prize if possible.
Given information about the predicted colors as explained above, write a program that determines whether the colors of all the lamps can be adjusted so that all the participants can receive prizes.
输入
Your program is to read from standard input. The input starts with a line containing two integers, k and n (3 < k ≤ 5,000, 1 ≤ n ≤ 10,000), where k is the number of lamps and n the number of game participants. Each of the following n lines contains three pairs of (l, c), where l is the lamp number he/she selected and c is a character, either B for blue or R for red, which denotes the color he/she guessed for the lamp. There is a blank between l and c and each pair of (l, c) is separated by a blank as well as shown in following samples.
输出
Your program is to write to standard output. If it is possible that all the colors can be adjusted so that every participant can receive a prize, print k characters in a line. The ith character, either B for blue or R for red represents the color of the ith lamp. If impossible, print -1. If there are more than one answer, you can print out any of them.
样例输入
7 5
3 R 5 R 6 B
1 B 2 B 3 R
4 R 5 B 6 B
5 R 6 B 7 B
1 R 2 R 4 R
样例输出
BRRRBBB
题意 : 有k个灯,5个嘉宾,每个嘉宾会选择3个灯进行猜颜色(只有红色和蓝色),猜中两个以上有奖,问怎么设置灯的颜色能使所有嘉宾都能得奖。
前天比赛的时候 和zn一起 写的暴搜 调试改错 花了两个小时吧 终于能过样例了 然而结果肯定是超时了
正解是 建立图论里的2-SAT模型
关于什么是2 - SAT 可以看看大神的讲解 https://blog.csdn.net/jarjingx/article/details/8521690
研究了一天多...目前还是半知半解,等完全明白了回来更新
照着题解写了个AC代码
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;++i)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long ll;
const int maxn = 10005;
const int maxm = 5005;
int T;
int n,k;
int tmp;
int vis[maxn * 2];
vector<int> sb[maxn],dd[maxn];
map<char,int>c;
stack<int> st;
int dfs(int u){
if(vis[u]) return 1;
if(vis[u ^ 1]) return 0;
vis[u] = 1;
st.push(u) ;
for(int i = 0; i < dd[u].size(); i++){
int v = dd[u][i];
for(int j = 0; j < sb[v].size(); j++){
int g = sb[v][j];
if(u != g){
if(!dfs(g ^ 1)) return 0;
}
}
}
return 1;
}
int _2sat(){
memset(vis,0,sizeof vis);
for(int i = 2; i <= k * 2; i += 2){
if(vis[i] || vis[i ^ 1]) continue;
while(!st.empty()) st.pop();
if(!dfs(i)) {
while(!st.empty()){
vis[st.top()] = 0;
st.pop();
}
if(!dfs(i ^ 1)) return 0;
}
}
return 1;
}
int main()
{
c['R'] = 0;
c['B'] = 1;
int pos;
char col;
read2(k,n);
for(int i = 1; i < maxn ;i ++) {
dd[i].clear();
sb[i].clear();
}
for(int i = 1; i <= n; i++){
for(int j = 0; j < 3; j++){
scanf("%d %c",&pos,&col);
tmp = pos * 2 + (c[col] ^ 1);
sb[i].pb(tmp);
dd[tmp].pb(i);
}
}
if(_2sat()){
for(int i = 1; i <= k; i++){
if(vis[i * 2]) printf("R");
else printf("B");
}
printf("\n");
}
else printf("-1");
return 0;
}
TV Show Game 【2-SAT】的更多相关文章
- POJ 1966 Cable TV Network (点连通度)【最小割】
<题目链接> 题目大意: 给定一个无向图,求点连通度,即最少去掉多少个点使得图不连通. 解题分析: 解决点连通度和边连通度的一类方法总结见 >>> 本题是求点连通度, ...
- [CodeForces-1225B] TV Subscriptions 【贪心】【尺取法】
[CodeForces-1225B] TV Subscriptions [贪心][尺取法] 标签: 题解 codeforces题解 尺取法 题目描述 Time limit 2000 ms Memory ...
- Windows 7 封装篇(一)【母盘定制】[手动制作]定制合适的系统母盘
Windows 7 封装篇(一)[母盘定制][手动制作]定制合适的系统母盘 http://www.win10u.com/article/html/10.html Windows 7 封装篇(一)[母盘 ...
- 第一个C#应用 【搜索软件】
搜索软件V1.0 [附软件截图][http://pan.baidu.com/s/1mihEbe4] 设备搜索:支持广播搜索[local search],指定ip[range search]搜索,直接w ...
- 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...
- C#设计模式之二十二访问者模式(Visitor Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第九个模式,该模式是[访问者模式],英文名称是:Visitor Pattern.如果按老规矩,先从名称上来看看这个模式,我根本不能获 ...
- 【WebApi系列】浅谈HTTP
[01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...
- C#设计模式之二十一访问者模式(Visitor Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第九个模式,该模式是[访问者模式],英文名称是:Visitor Pattern.如果按老规矩,先从名称上来看看这个模式,我根本不能获得任何对理解该模式有用的 ...
- 【FJOI 20170305】省选模拟赛
题面被改成了个猪... T1猪猪划船(boat) [题目描述] 6只可爱的猪猪们一起旅游,其中有3只大猪A,B,C,他们的孩子为3只小猪a,b,c.由于猪猪们十分凶残,如果小猪在没有父母监护的情况下, ...
- 【WebApi系列】浅谈HTTP在WebApi开发中的运用
WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...
随机推荐
- Unity shader学习之遮罩纹理
什么是遮罩? 遮罩允许我们可以保护某些区域,使它们奂于某些修改. 例如下面的例子,使用遮罩来控制高光反射. 转载请注明出处:http://www.cnblogs.com/jietian331/p/71 ...
- ruby中的alias和alias_method
ruby中的alias和alias_method都可以重命名一个方法,它们的区别如下: 1.alias是ruby的一个关键字,因此使用的时候是alias :newname :oldname alias ...
- 在lnmp1.3布置的web服务器上运行thinkphp3.2.3项目pathinfo路径模式
通过我的经历希望能给大家带来一些帮助: 我是在Linux系统上通过https://lnmp.org/install.html设置Nginx服务器,使用的是lnmp1.3版本,之后将一个thinkphp ...
- hdu5067
题意 给了一个n*m的网格 然后一台挖掘机从(0,0) 这个位置出发,收集完全部的石头回到(0,0)挖掘机可以有无限的载重 用旅行商处理 dp[k][i] 表示在这个集合中最后到达i的最小距离,用集合 ...
- 【2017-04-17】类库、通用变量、is和as、委托
类库dll文件,里边有很多被编译后的C#代码,不可阅读,不可修改,只能调用 1.类库创建 新建项目为类库,类库文件编写完成后,选择生成—生成解决方案,在debug文件夹下找到dll文件 2.类库引用 ...
- GDB && QString
[1]GDB && QString GDB的print命令仅能打印基本数据类型,而像QString这样的复杂类型就无能为力了! 如果调试时不能看QString的值,很让人抓狂!!!幸好 ...
- ubuntu_查看software
感谢原博主的分享 ubuntu安装和查看已安装 说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行 ...
- 怎样从外网访问内网Jupyter Notebook?
本地安装了一个Jupyter Notebook,只能在局域网内访问,怎样从外网也能访问到本地的Jupyter Notebook呢?本文将介绍具体的实现步骤. 准备工作 安装并启动Jupyter Not ...
- codevs——1003——电话连线
题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...
- 相关Linux命令
#若服务不小心开启但是想关闭时,可以使用这个 命令:adstrtal.sh apps/apps(脚本 数据库用户名/密码) 若想启动命令:adstrtal.sh apps/apps ———————— ...