当前位置:网站首页>UVA11294-Wedding(2-SAT)
UVA11294-Wedding(2-SAT)
2022-07-05 23:03:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
题意:有n对夫妻參加一个婚宴。
全部人都坐在一个长长的餐桌的左边或者右边。全部夫妻都仅仅能面对面坐,包含新娘和新郎。新娘仅仅能看到坐在她不同側的人。有m对人超过架,新娘不希望看到他们坐在同一側。
问有没有分配方案满足新娘的要求。
思路:2-SAT问题。如果每对夫妇为一个变量xi。如果xi为true时,妻子与新娘坐同一側;xi为false时,丈夫与新娘坐同一側。当xi和xj同为丈夫时,则需满足~xi V ~xj,表示两个丈夫最多仅仅有一个坐在与新娘不同側;当xi和xj同为妻子时。则需满足xi V xj。表示两个妻子最多仅仅有一个坐在与新娘不同側。当xi和xj为异性时,则需满足~xi V xj或者xi V ~xj当中一个。表示两个最多就一个坐在与新娘不同側。
综上所述。就是要满足丈夫~xi,妻子xi。最后要注意初始化mark[1] = 1。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 1005;
struct TwoSAT{
int n;
vector<int> g[MAXN * 2];
bool mark[MAXN * 2];
int s[MAXN * 2], c;
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].size(); i++)
if (!dfs(g[x][i])) return false;
return true;
}
void init(int n) {
this->n = n;
for (int i = 0; i < n * 2; i++)
g[i].clear();
memset(mark, 0, sizeof(mark));
mark[1] = 1;
}
void add_clause(int x, int xval, int y, int yval) {
x = x * 2 + xval;
y = y * 2 + yval;
g[x^1].push_back(y);
g[y^1].push_back(x);
}
bool solve() {
for (int i = 0; i < n * 2; i += 2)
if (!mark[i] && !mark[i + 1]) {
c = 0;
if (!dfs(i)) {
while (c > 0) mark[s[--c]] = false;
if (!dfs(i + 1)) return false;
}
}
return true;
}
};
TwoSAT solver;
int n, m;
int main() {
while (scanf("%d%d", &n, &m)) {
if (n == 0 && m == 0)
break;
solver.init(n);
char a, b;
int xval, yval, u, v;
while (m--) {
scanf("%d%c%d%c", &u, &a, &v, &b);
xval = (a == 'h') ? 0 : 1; yval = (b == 'h') ? 0 : 1; solver.add_clause(u, xval, v, yval); } if (!solver.solve()) printf("bad luck\n"); else { for (int i = 1; i < n; i++) { printf("%d%c", i, solver.mark[2*i] ? 'h' : 'w'); if (i == n - 1) printf("\n"); else printf(" "); } } } return 0;}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117543.html原文链接:https://javaforall.cn
边栏推荐
- 【Note17】PECI(Platform Environment Control Interface)
- asp.net弹出层实例
- Ultrasonic sensor flash | LEGO eV3 Teaching
- UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
- One article deals with the microstructure and instructions of class
- openresty ngx_lua正则表达式
- 使用rewrite规则实现将所有到a域名的访问rewrite到b域名
- Masked Autoencoders Are Scalable Vision Learners (MAE)
- Expectation, variance and covariance
猜你喜欢
Realize reverse proxy client IP transparent transmission
查看网页最后修改时间方法以及原理简介
Un article traite de la microstructure et des instructions de la classe
audiopolicy
Marginal probability and conditional probability
Use of grpc interceptor
【原创】程序员团队管理的核心是什么?
February 13, 2022-4-symmetric binary tree
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Non rigid / flexible point cloud ICP registration
随机推荐
[untitled]
Multi camera stereo calibration
YML configuration, binding and injection, verification, unit of bean
AsyncSocket长连接棒包装问题解决
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
Marginal probability and conditional probability
Three. JS VR house viewing
3 find the greatest common divisor and the least common multiple
Practice of concurrent search
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
【Note17】PECI(Platform Environment Control Interface)
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
C Primer Plus Chapter 9 question 10 binary conversion
TypeError: this. getOptions is not a function
Selenium+pytest automated test framework practice
Three. Js-01 getting started
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
LeetCode——Add Binary
Using LNMP to build WordPress sites