问题 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. C# 对数据库操作的帮助类SQLHelper.cs

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  2. c# 使用MS SqlServer,连接成功,但是还报异常A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0。。。。

    c# 使用MS SqlServer,连接成功,但是还报异常A connection was successfully established with the server, but then an ...

  3. Unity shader学习之阴影,衰减统一处理

    使用unity AutoLight.cginc文件里的内置函数 UNITY_LIGHT_ATTENUATION shader如下: // Upgrade NOTE: replaced 'mul(UNI ...

  4. 关于用户登录状态存session,cookie还是数据库或者memcache的优劣

    session中保存登陆状态: 优:整个应用可以从session中获取用户信息,并且查询时很方便.在session中保存用户信息是不可缺少的(web应用中) 缺:session中不宜保存大量信息,会增 ...

  5. Linux 查看磁盘或文件夹及文件大小

    当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力.    du可以查看文件及文件夹的大小. ...

  6. modelsim仿真正确FPGA运行不正确的可能原因 - cm4写寄存器错

    困住整整一周了,工作进行不下去,中午偶遇导师,指导意见是有两种可能: 1.  FPGA编译器优化代码,可以考虑把综合过程中所有的warning排查一下 2.  verilog里有不可综合的语句. 又及 ...

  7. c# 类的知识

    在英语中类(class)是分类(classification)的根词.设计类的过程就是对信息进行分类,将相关信息放到有意义的实体中. 封装的目的: 封装就是定义类的重要原则.中心思想是:使用类的程序不 ...

  8. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  9. 如何给webview页面自定义404页面

    //示例地图类 package com.can2do.doimobile.news; import android.os.Bundle; import android.os.Handler; impo ...

  10. P1258 小车问题

    P1258 小车问题 蒟蒻精神自强不息蒟蒻精神永不言败加油加油ヾ(◍°∇°◍)ノ゙yeah yeah yeah 据说这是道小学奥数题抱歉蒟蒻的我没学过奥数,算了大概三大张演草纸,不得不说这题对于蒟蒻本 ...