当前位置:网站首页>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
边栏推荐
- Calculating the number of daffodils in C language
- 数据库基础知识(面试)
- Shell: operator
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- Basic knowledge of database (interview)
- Getting started stm32--gpio (running lantern) (nanny level)
- Nacos installation and service registration
- Ultrasonic sensor flash | LEGO eV3 Teaching
- Krypton Factor-紫书第七章暴力求解
- Selenium+pytest automated test framework practice
猜你喜欢

Matlab smooth curve connection scatter diagram

Alibaba Tianchi SQL training camp task4 learning notes

3:第一章:认识JVM规范2:JVM规范,简介;
![[screen recording] how to record in the OBS area](/img/34/bd06bd74edcdabaf678c8d7385cae9.jpg)
[screen recording] how to record in the OBS area

Selenium+pytest automated test framework practice

Common model making instructions

CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)

【经典控制理论】自控实验总结

Fix the memory structure of JVM in one article

Go语言实现原理——Map实现原理
随机推荐
audiopolicy
Sum of two numbers, sum of three numbers (sort + double pointer)
判断二叉树是否为完全二叉树
Douban scoring applet Part-2
Non rigid / flexible point cloud ICP registration
Use of shell:for loop
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Go language implementation principle -- map implementation principle
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
CJ mccullem autograph: to dear Portland
Common model making instructions
Detailed explanation of pointer and array written test of C language
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
两数之和、三数之和(排序+双指针)
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
终于搞懂什么是动态规划的
Using LNMP to build WordPress sites
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
Alibaba Tianchi SQL training camp task4 learning notes