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 ...
随机推荐
- 编写jQuery插件(一)——插件约定及插件中的闭包
编写插件的目的是给已经有的一系列方法或函数做一个封装,以便在其他地方重复使用,提高开发效率和方便后期维护. 在编写jQuery插件的时候,我们一般会遵循一些约定: jQuery插件推荐命名为:jque ...
- hibernate中1对1的注解配置
hibernate中1对1的注解配置分为:外键关联映射和主键关联映射 1.外键配置 //一方@Entity@Table(name="test_classinfo")public c ...
- ldd3 源码相关问题参见
参考地址: http://www.verydemo.com/demo_c92_i235635.html http://blog.csdn.net/silvervi/article/details/64 ...
- Enterprise Library 企业库
微软企业库,提供了一套日志,缓存等功能的库.可以通过NuGet安装.
- HSTS详解
1. 缘起:启用HTTPS也不够安全 有不少网站只通过HTTPS对外提供服务,但用户在访问某个网站的时候,在浏览器里却往往直接输入网站域名(例如www.example.com),而不是输入完整的URL ...
- 97. Interleaving String (String; DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 7.Reverse Integer (INT; Overflow)
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:要注意溢出 ...
- 133克隆图 · Clone Graph
[抄题]: 克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors. [思维问题]: [一句话思路]: 先BFS克隆点(一个点+扩展所有邻居),再克隆邻居(一个点+扩展所有 ...
- [leetcode]269. Alien Dictionary外星字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 利用redis完成自动补全搜索功能(二)
前面介绍了自动完成的大致思路,现在把搜索次数的功能也结合上去.我采用的是hash表来做的,当然也可以在生成分词的时候,另外一个有序集合来维护排序, 然后2个有序集合取交集即可.这里介绍hash的方式来 ...