Wedding (poj 3648 2-SAT 输出随意一组解)
|
Language:
Default
Wedding
Description Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. The bride and groom sit at one end, opposite each other, and the bride wears an elaborate headdress that keeps her from seeing people on the same Input The input consists of a number of test cases, followed by a line containing 0 0. Each test case gives n, the number of couples, followed by the number of adulterous pairs, followed by the pairs, in the form "4h 2w" (husband from couple 4, wife from Output For each case, output a single line containing a list of the people that should be seated on the same side as the bride. If there are several solutions, any one will do. If there is no solution, output a line containing "bad luck". Sample Input 10 6 Sample Output 1h 2h 3w 4h 5h 6h 7h 8h 9h Source
Waterloo Local Contest, 2007.9.29
|
||||||||||
题意:题目太绕了=-=有一对新人结婚。非常多对夫妇參加婚礼,共n对。如今安排全部人坐在一张桌子两边,且(1)每对夫妇不能坐在同一側(2)n对夫妇中有通奸关系(包含男男,男女,女女),有通奸关系的不能坐在新娘的对面。问是否存在可行的安排方案。若存在输出与新娘同側的人。
思路:注意新郎必须在还有一边,所以就要加一条新娘到新郎的边以满足情况。
代码:
#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 100;
const int MAXM = 2010;
const int N = 1005; struct Edge
{
int to,next;
}edge[MAXM]; int n,m;
int tot,head[MAXN];
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
bool Instack[MAXN];
int top,Index,scc; void init()
{
tot=0;
memset(head,-1,sizeof(head));
} void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void Tarjan(int u)
{
int v;
Low[u]=DFN[u]=++Index;
Instack[u]=true;
Stack[top++]=u;
for (int i=head[u];~i;i=edge[i].next)
{
v=edge[i].to;
if (!DFN[v])
{
Tarjan(v);
if (Low[u]>Low[v]) Low[u]=Low[v];
}
else if (Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if (Low[u]==DFN[u])
{
scc++;
do
{
v=Stack[--top];
Instack[v]=false;
Belong[v]=scc;
}while (v!=u);
}
return ;
} bool solvable(int n)
{
memset(DFN,0,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
top=Index=scc=0;
for (int i=0;i<n;i++)
if (!DFN[i])
Tarjan(i);
for (int i=0;i<n;i+=2)
{
if (Belong[i]==Belong[i^1])
return false;
}
return true;
} queue<int>q1;
vector<vector<int> >dag;
char color[MAXN];
int indeg[MAXN];
int cf[MAXN]; void solve(int n)
{
dag.assign(scc+1,vector<int>());
memset(indeg,0,sizeof(indeg));
memset(color,0,sizeof(color));
for (int u=0;u<n;u++)
{
for (int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if (Belong[u]!=Belong[v])
{
dag[Belong[v]].push_back(Belong[u]);
indeg[Belong[u]]++;
}
}
}
for (int i=0;i<n;i++)
{
cf[Belong[i]]=Belong[i^1];
cf[Belong[i^1]]=Belong[i];
}
while (!q1.empty()) q1.pop();
for (int i=1;i<=scc;i++)
if (indeg[i]==0)
q1.push(i);
while (!q1.empty())
{
int u=q1.front();
q1.pop();
if (color[u]==0)
{
color[u]='R';
color[cf[u]]='B';
}
int sz=dag[u].size();
for (int i=0;i<sz;i++)
{
indeg[dag[u][i]]--;
if (indeg[dag[u][i]]==0)
q1.push(dag[u][i]);
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,a,b;
char c1,c2;
while (~scanf("%d%d",&n,&m))
{
if (n==0&&m==0) break;
init();
for (i=0;i<m;i++)
{
scanf("%d%c%d%c",&a,&c1,&b,&c2);
if (c1=='w') a=2*a; else a=2*a+1;
if (c2=='w') b=2*b; else b=2*b+1;
addedge(a,b^1);
addedge(b,a^1);
}
addedge(0,1);
if (!solvable(2*n))
{
printf("bad luck\n");
continue;
}
solve(2*n);
for (i=1;i<n;i++)
{
if (i>1) printf(" ");
if (color[Belong[2*i]]=='R') printf("%dh",i);
else printf("%dw",i);
}
printf("\n");
}
return 0;
}
Wedding (poj 3648 2-SAT 输出随意一组解)的更多相关文章
- HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- poj 3648 2-sat 输出任意一组解模板
转载地址:http://blog.csdn.net/qq172108805/article/details/7603351 /* 2-sat问题,题意:有对情侣结婚,请来n-1对夫妇,算上他们自己共n ...
- POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)
Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10427 Accepted: 3170 Specia ...
- POJ 3648 Wedding (2-SAT,经典)
题意:新郎和新娘结婚,来了n-1对夫妻,这些夫妻包括新郎之间有通奸关系(包括男女,男男,女女),我们的目地是为了满足新娘,新娘对面不能坐着一对夫妻,也不能坐着有任何通奸关系的人,另外新郎一定要坐新娘对 ...
- poj 3683 2-sat问题,输出任意一组可行解
/* 2sat问题 输出任意一组可行解 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #in ...
- SAM I AM UVA - 11419(最小顶点覆盖+输出一组解)
就是棋盘问题输出一组解 https://blog.csdn.net/llx523113241/article/details/47759745 http://www.matrix67.com/blog ...
- PHP中刷新输出缓冲详解[转载]
PHP中刷新输出缓冲详解 分类: PHP Web开发2011-07-23 17:42 1795人阅读 评论(0) 收藏 举报 phpbuffer浏览器outputapache模块脚本 buffer是一 ...
- 《手把手教你》系列基础篇(八十六)-java+ selenium自动化测试-框架设计基础-Log4j实现日志输出(详解教程)
1.简介 自动化测试中如何输出日志文件.任何软件,都会涉及到日志输出.所以,在测试人员报bug,特别是崩溃的bug,一般都要提供软件产品的日志文件.开发通过看日志文件,知道这个崩溃产生的原因,至少知道 ...
- 《手把手教你》系列基础篇(九十五)-java+ selenium自动化测试-框架之设计篇-java实现自定义日志输出(详解教程)
1.简介 前面宏哥一连几篇介绍如何通过开源jar包Log4j.jar.log4j2.jar和logback实现日志文件输出,Log4j和logback确实很强大,能生成三种日志文件,一种是保存到磁盘的 ...
随机推荐
- CMA连续物理内存用户空间映射---(二)
摘要: 相对于上一篇測试程序CMA连续物理内存用户空间映射---(一) 添加功能: 1.分配和映射统一放在IOCTL,一次完毕,能够连续多次分配并映射到用户空间,提高操作性: 2.驱动添加链表,使分配 ...
- qml自学笔记------自己写相似于劲舞团的按键小游戏(中)
接上篇<qml自学笔记------自己写类似于劲舞团的按键小游戏(上)> 第三部分DisplayPart.qml 代码的其它部分都是渣,就这里花了点时间,整个小游戏就靠这个文件. 首先,屏 ...
- docker集群——Mesos集群下的负载均衡marathon-lb
前面的章节介绍了Mesos+Zookeeper+Marathon的Docker管理平台,接下来介绍如何在该平台下构建负载均衡. 默认情况下,mesos marathon会把app发布到随机节点的随机端 ...
- jQuery 创建html
jQuery 创建html
- 详解Android首选项框架的使用
首选项这个名词对于熟悉Android的朋友们一定不会感到陌生,它经常用来设置软件的运行参数. Android提供了一种健壮并且灵活的框架来处理首选项.它提供了简单的API来隐藏首选项的读取和持久化,并 ...
- 【Python3 爬虫】04_urllib.request.urlretrieve
urllib模块提供的urlretrieve()函数,urlretrieve()方法直接将远程的数据下载到本地 urllib语法 参数url:传入的网址,网址必须得是个字符串 参数filename:指 ...
- Oracle学习笔记(5)——查询
基本查询语句 SELECT [DISTINCT] column_name1,...|* FROM table_name [WHERE conditions] 在SQL*PLUS中设置格式 更改显示字段 ...
- Sql Server2005 Synonyms
1. 同义词(SYNONYM)是SQL Server 2005中新特性 它是一种对已有的或潜在的新对象给予的别名.可以在同一个数据库或者跨数据中中使用这个别名,这个别名替代了原有对象.可以建别名的对象 ...
- atitit.atiLinq v2新特性attilax大总结 q326
atitit.atiLinq v2新特性attilax大总结 q326 1. V3规划 (分开sql2obj sql2sql sql2xml)1 2. V2新特性 Url linq的定义1 3. V1 ...
- Atitit避免出现空指针异常解决方案
Atitit避免出现空指针异常解决方案 1. Null的问题1 2. 强制区分一般引用vs 可空引用 vs 强制引用,或者说非空引用2 3. ?运算符(问号运算符) !感叹号运算符避免出现空指针异常, ...