当前位置:网站首页>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
边栏推荐
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Krypton Factor purple book chapter 7 violent solution
- 秒杀系统的设计与实现思路
- Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
- Use of grpc interceptor
- Un article traite de la microstructure et des instructions de la classe
- Registration and skills of hoisting machinery command examination in 2022
- Calculating the number of daffodils in C language
- Dynamic memory management (malloc/calloc/realloc)
- Negative sampling
猜你喜欢
Getting started stm32--gpio (running lantern) (nanny level)
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Dynamic memory management (malloc/calloc/realloc)
TypeError: this. getOptions is not a function
Detailed explanation of pointer and array written test of C language
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Douban scoring applet Part-2
Common model making instructions
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
Activate function and its gradient
随机推荐
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
[untitled]
Multi camera stereo calibration
终于搞懂什么是动态规划的
数学公式截图识别神器Mathpix无限使用教程
Vision Transformer (ViT)
CJ mccullem autograph: to dear Portland
Development specification: interface unified return value format [resend]
Common model making instructions
From the perspective of quantitative genetics, why do you get the bride price when you get married
Leetcode buys and sells stocks
3 find the greatest common divisor and the least common multiple
如何快速理解复杂业务,系统思考问题?
Practice of concurrent search
Fix the memory structure of JVM in one article
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
判断二叉树是否为完全二叉树
2:第一章:认识JVM规范1:JVM简介;
实现反向代理客户端IP透传
Object detection based on impulse neural network