UVaLive 4452 The Ministers' Major Mess (TwoSat)
题意:有 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)的更多相关文章
- uvalive 4452 The Ministers’ Major Mess
题意: 有一些部长需要对某些账单进行投票. 一个部长最多对4个账单进行投票,且每票对一个账单通过,要么否决. 问是否存在一个方案使得所有部长有超过半数的投票被通过,如果有,那么说明哪些账单的决定是明确 ...
- UVALive 4452 The Ministers' Major Mess(2-sat)
2-sat.又学到了一种使用的方法:当确定选择某中状态A时,从它的对立状态A^1引一条边add(A^1,A),从而使凡是dfs经过对立状态,必然return false:即保证若存在一种可能性,必然是 ...
- 【2-SAT】The Ministers’ Major Mess UVALive – 4452
题目链接:https://cn.vjudge.net/contest/209474#problem/C 题目大意: 一共有m个提案,n个政客,每个政客都会对一些提案(最多四个)提出自己的意见——通过或 ...
- UVALive-4452 The Ministers' Major Mess (2-SAT)
题目大意:有n个问题,m个人来投票,没人最多投4票,问该怎样决定才能使每个人都有超过一半的票数被认可? 题目分析:2-SAT问题.如果某个人投的票数少于2,则这两票军被采纳,如果票数至少三票,则最多有 ...
- uva1086 The Ministers' Major Mess
题意:有n 个议案,m 个大臣,每个大臣会对其中的ki 个议案投票,为赞成或反对.现要你判断是否存在一种方案,使得每个大臣有大于一半的投票被满足.若存在,还需判断某个议案是不是一定要通过,或者一定不能 ...
- UVALive - 3713 - Astronauts(图论——2-SAT)
Problem UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...
- UVALive - 3211 - Now or later(图论——2-SAT)
Problem UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
随机推荐
- java常用的Utils写法
Utils: 获取年龄 属性文件获取 BeanCopy 分页 MapUtils 获取年龄: /** * 根据传入的日期计算年龄,因时间戳是从1970年开始计算的 * @param date * ...
- UI5-学习篇-2-Hello World
创建Application Project 1.打开Eclipse,创建Project sap.ui.commons 和 sap.m 是两个不同的 UI 库,但现在因为跨平台的原因,sap.ui.co ...
- Java LinkList遍历方式
1.LinkedList的遍历方式 a.一般的for循环(随机访问) int size = list.size(); for (int i=0; i<size; i++) { list.get( ...
- Life is in the little things --- Spreading wildly on Facebook
这是在FaceBook上疯传的一组图 简笔画的图画的不算精细 但却狠狠地击中许多人的心灵 有时候生活中简单的一件小事, 恰恰是使得你的人生变得更有意义的一件大事! 别人总告诉你 人生是这样的 ▼ ...
- 吴裕雄 数据挖掘与分析案例实战(5)——python数据可视化
# 饼图的绘制# 导入第三方模块import matplotlibimport matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['S ...
- gtftools软件简单介绍(我自己不建议用,因为我发现不好用)
1)背景 生物信息学研究经常涉及计算或提取基因的各种特征,如基因ID作图,GC含量计算和不同类型的基因长度,通过操纵基因模型,这些模型通常以GTF格式注释,可从ENSEMBL或GENCODE数据库获得 ...
- Subquery typo with using in(转)
Subquery typo with using in Do you use the following syntax? SELECT * FROM TABLE WHERE COLUMN IN ( ...
- 等待时间,time.sleep()和implicitly_wait()
在运行一个以前执行的过的功能时,报错了,不能执行了. 功能描述:通过导航,选择下拉项(发布职位功能),下面是审查的元素: 获取元素的代码: 尝试了用xpath去获取:driver.find_eleme ...
- go,函数作为参数类型
package main import "fmt" type testInt func(int) bool // 声明了一个函数类型 func isOdd(integer int) ...
- ajax返回填充的数据不显示
原因:样式与id引用了其他的css或者js,删除其他样式,改变id就可以了