当前位置:网站首页>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
边栏推荐
- Déterminer si un arbre binaire est un arbre binaire complet
- Sum of two numbers, sum of three numbers (sort + double pointer)
- 并查集实践
- 3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
- Krypton Factor-紫书第七章暴力求解
- Composition of interface
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- 数学公式截图识别神器Mathpix无限使用教程
猜你喜欢

The method and principle of viewing the last modification time of the web page

2022.02.13 - SX10-30. Home raiding II

There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!

如何快速理解复杂业务,系统思考问题?

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

Finally understand what dynamic planning is

Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial

Element positioning of Web Automation

第十七周作业

Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
随机推荐
Using LNMP to build WordPress sites
asp.net弹出层实例
Alibaba Tianchi SQL training camp task4 learning notes
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Douban scoring applet Part-2
Go语言实现原理——Map实现原理
媒体查询:引入资源
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
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
Go language implementation principle -- lock implementation principle
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Week 17 homework
Use of shell:for loop
Use of grpc interceptor
regular expression
leecode-学习笔记
3:第一章:认识JVM规范2:JVM规范,简介;
Roman numeral to integer
Non rigid / flexible point cloud ICP registration