题意:有 m 个人对 n 个方案投票,每个人最多只能对其中的4个方案投票(其他的相当于弃权),每一票要么支持要么反对。问是否存在一个最终决定,使得每个投票人都有超过一半的建议被采纳,在所有可能的最终决定中,哪些方案的态度是确定的。

析:注意这个题是超过一半,是TwoSat 算法,对于投小于三票的,他的票必须都成立(因为要超过一半),同理大于两票的最多一票不成立,这样考虑就是twosat的问题了,也就是说对于小于三票的,那么就相当于是标记了必须成立,对于大于两票的,那么就是如果有一个不成立,那么其他的必须全成立,那么就全连上边。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 1e6 + 5;
const int mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} char ans[maxn]; struct TwoSat{
int n;
vector<int> G[maxn<<1];
bool mark[maxn<<1];
int S[maxn<<1], c; void init(int n){
this-> n = n;
for(int i = 0; i < (n<<1); ++i) G[i].cl;
ms(mark, 0);
} void add_clause(int x, int xval, int y, int yval){
x = x << 1 | xval;
y = y << 1 | yval;
G[x^1].pb(y);
G[y^1].pb(x);
} bool dfs(int x){
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x] = true;
S[c++] = x;
for(int i = 0; i < G[x].sz; ++i)
if(!dfs(G[x][i])) return false;
return true;
} bool solve(){
for(int i = 0; i < (n<<1); i += 2)
if(!mark[i] && !mark[i^1]){
c = 0;
bool ok1 = dfs(i);
while(c) mark[S[--c]] = 0;
bool ok2 = dfs(i^1);
while(c) mark[S[--c]] = 0;
if(ok1 && ok2) ans[i>>1] = '?';
else if(ok1) ans[i>>1] = 'n';
else if(ok2) ans[i>>1] = 'y';
else return false;
}
ans[n] = 0;
return true;
}
};
TwoSat twosat; int a[10], val[10]; int main(){
int kase = 0;
while(scanf("%d %d", &n, &m ) == 2 && n+m){
twosat.init(n);
for(int i = 0; i < m; ++i){
int num; scanf("%d", &num);
for(int j = 0; j < num; ++j){
char ch;
scanf("%d %c", a+j, &ch);
--a[j];
val[j] = ch == 'y';
}
if(num < 3) for(int j = 0; j < num; ++j)
twosat.add_clause(a[j], val[j], a[j], val[j]);
else for(int j = 0; j < num; ++j)
for(int k = j+1; k < num; ++k)
twosat.add_clause(a[j], val[j], a[k], val[k]);
}
printf("Case %d: ", ++kase);
if(!twosat.solve()) puts("impossible");
else puts(ans);
}
return 0;
}

  

UVaLive 4452 The Ministers' Major Mess (TwoSat)的更多相关文章

  1. uvalive 4452 The Ministers’ Major Mess

    题意: 有一些部长需要对某些账单进行投票. 一个部长最多对4个账单进行投票,且每票对一个账单通过,要么否决. 问是否存在一个方案使得所有部长有超过半数的投票被通过,如果有,那么说明哪些账单的决定是明确 ...

  2. UVALive 4452 The Ministers' Major Mess(2-sat)

    2-sat.又学到了一种使用的方法:当确定选择某中状态A时,从它的对立状态A^1引一条边add(A^1,A),从而使凡是dfs经过对立状态,必然return false:即保证若存在一种可能性,必然是 ...

  3. 【2-SAT】The Ministers’ Major Mess UVALive – 4452

    题目链接:https://cn.vjudge.net/contest/209474#problem/C 题目大意: 一共有m个提案,n个政客,每个政客都会对一些提案(最多四个)提出自己的意见——通过或 ...

  4. UVALive-4452 The Ministers' Major Mess (2-SAT)

    题目大意:有n个问题,m个人来投票,没人最多投4票,问该怎样决定才能使每个人都有超过一半的票数被认可? 题目分析:2-SAT问题.如果某个人投的票数少于2,则这两票军被采纳,如果票数至少三票,则最多有 ...

  5. uva1086 The Ministers' Major Mess

    题意:有n 个议案,m 个大臣,每个大臣会对其中的ki 个议案投票,为赞成或反对.现要你判断是否存在一种方案,使得每个大臣有大于一半的投票被满足.若存在,还需判断某个议案是不是一定要通过,或者一定不能 ...

  6. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  7. UVALive - 3211 - Now or later(图论——2-SAT)

    Problem   UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...

  8. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  9. UVALive - 3211 (2-SAT + 二分)

    layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...

随机推荐

  1. JS 时间 获取 当天,昨日,本周,上周,本月,上月

    调用 setTimeRange (2); function  setTimeRange (v) { var fmt = 'YYYY-MM-DD HH:mm'; var now = new Date() ...

  2. FD 设置字体大小

    英文版: 依次选择菜单 Tools ->Syntax Coloring 中文版本: 如依次选择菜单 工具 ->语法配色器

  3. 低版本eclipse离线集成svn步骤,亲测有效!!!

    1.下载svn离线版的插件: 百度云盘链接:http://pan.baidu.com/s/1eSnMoHO 密码:6oef 2.解压出来的额目录如下: 3.将features和plugins里面的ja ...

  4. setTranslatesAutoresizingMaskIntoConstraints

    [viewItem setTranslatesAutoresizingMaskIntoConstraints:NO]; 在给继承UIView的类设置此属性后,UIView的某些属性可能发生变化.例如f ...

  5. 安卓上为什么不能用system.io.file读取streammingAssets目录下的文件

    首先,看文档: Streaming Assets   Most assets in Unity are combined into the project when it is built. Howe ...

  6. tnsping 命令解析

    C:\Users\nowhill>tnsping jljcz Oracle Net 工具(命令)tnsping,是一个OSI会话层的工具,它用来: 1)验证名字解析(name resolutio ...

  7. 与servlet相关的接口

    (二)与servlet相关的接口 从servlet仅有的5个方法当中,我们知道其涉及3个接口,分别是: ServletConfig ServletRequest ServletResponse 2.1 ...

  8. dubbo2.5.3升级到dobbo2.8.4(dubbox) jar

    需要注意的地方: 1.pom文件中 dubbo的版本由2.5.3变为2.8.4,maven依赖如下:     <dependency>         <groupId>com ...

  9. Cache Server

    [Cache Server] Whenever a source Asset like a .psd or an .fbx file is modified, Unity detects the ch ...

  10. Saving Tang Monk II(bfs+优先队列)

    Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...