问题 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】的更多相关文章

  1. POJ 1966 Cable TV Network (点连通度)【最小割】

    <题目链接> 题目大意: 给定一个无向图,求点连通度,即最少去掉多少个点使得图不连通. 解题分析: 解决点连通度和边连通度的一类方法总结见   >>> 本题是求点连通度, ...

  2. [CodeForces-1225B] TV Subscriptions 【贪心】【尺取法】

    [CodeForces-1225B] TV Subscriptions [贪心][尺取法] 标签: 题解 codeforces题解 尺取法 题目描述 Time limit 2000 ms Memory ...

  3. Windows 7 封装篇(一)【母盘定制】[手动制作]定制合适的系统母盘

    Windows 7 封装篇(一)[母盘定制][手动制作]定制合适的系统母盘 http://www.win10u.com/article/html/10.html Windows 7 封装篇(一)[母盘 ...

  4. 第一个C#应用 【搜索软件】

    搜索软件V1.0 [附软件截图][http://pan.baidu.com/s/1mihEbe4] 设备搜索:支持广播搜索[local search],指定ip[range search]搜索,直接w ...

  5. 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...

  6. C#设计模式之二十二访问者模式(Visitor Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第九个模式,该模式是[访问者模式],英文名称是:Visitor Pattern.如果按老规矩,先从名称上来看看这个模式,我根本不能获 ...

  7. 【WebApi系列】浅谈HTTP

    [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...

  8. C#设计模式之二十一访问者模式(Visitor Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第九个模式,该模式是[访问者模式],英文名称是:Visitor Pattern.如果按老规矩,先从名称上来看看这个模式,我根本不能获得任何对理解该模式有用的 ...

  9. 【FJOI 20170305】省选模拟赛

    题面被改成了个猪... T1猪猪划船(boat) [题目描述] 6只可爱的猪猪们一起旅游,其中有3只大猪A,B,C,他们的孩子为3只小猪a,b,c.由于猪猪们十分凶残,如果小猪在没有父母监护的情况下, ...

  10. 【WebApi系列】浅谈HTTP在WebApi开发中的运用

    WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...

随机推荐

  1. Struts2自定义Field级别的错误提示信息

    自定义Field级别的错误提示信息步骤: 在action包中新建一个以Action命名的properties文件,如:RegisterAction.properties 2. 然后在该属性文件中指定每 ...

  2. DBA角色职责

    MySQL DBA分架构DBA,运维DBA和开发DBA三种角色,职责介绍如下: MySQL数据库系统日常管理职责 日常管理的主要职责是对MySQL服务器程序mysqld的运行情况进行管理,使数据库用户 ...

  3. springboot之session、cookie

    1-  获取session的方案 session:  https://blog.csdn.net/yiifaa/article/details/77542208 2-  session什么时候创建? ...

  4. yii2验证密码->手机号码短信发送>手机短信发送频繁问题

    <?php namespace frontend\models; use Yii; use yii\base\Model; class ChangeMobileSendRequestForm e ...

  5. Game (思维)

    #include<bits/stdc++.h> using namespace std; ; char str[maxn][maxn]; int cntx[maxn], cnty[maxn ...

  6. Extjs4前端开发代码规范参考

    准则:  一致性, 隔离与统一管理, 螺旋式重构改进, 消除重复, 借鉴现有方案 1.    保证系统实现的一致性,寻求一致性方案, 相同或相似功能尽量用统一模式处理: 2.    尽可能使用隔离技术 ...

  7. 20165215 实验三 敏捷开发与XP实践

    20165215 实验三 敏捷开发与XP实践 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:张家佳 学号:20165215 指导教师:娄嘉鹏 实验日期:2018年4月28日 实验时 ...

  8. Git相关整理以及学习

    1.取消还未进入暂存区的修改 git checkout + 文件名 2.取消已经进入暂存区但是还未提交的修改 git reset HEAD + 文件名 3.清理在远程仓库已经被删除的本地分支 git ...

  9. 高性能NIO框架Netty入门篇

    http://cxytiandi.com/blog/detail/17345 Netty介绍 Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具 ...

  10. 专题8:javascript函数详解

    函数是一段可以反复调用的代码块.函数还能接受输入的参数,不同的参数会返回不同的值. 函数的声明 JavaScript 有三种声明函数的方法. (1)function 命令 function命令声明的代 ...