POJ 3648 Wedding
2-SAT,直接选择新娘一侧的比较难做,所以处理的时候选择新郎一侧的,最后反着输出就可以。
A和B通奸的话,就建边 A->B'以及B->A’,表示 A在新郎一侧的话,B一定不在;B在新郎一侧的话,A一定不在。
然后再把新郎的mark标记为1,表示新郎一定选择。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=+;
int N,m;
char s1[],s2[]; struct TwoSAT
{
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int S[maxn*],c; bool dfs(int x)
{
if(mark[x^]) return false;
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=;i<G[x].size();i++)
if(!dfs(G[x][i])) return false;
return true;
} void init(int n)
{
this->n=n;
for(int i=;i<n*;i++) G[i].clear();
memset(mark,,sizeof mark);
} void add_clause(int x,int y)
{
G[x].push_back(y^);
G[y].push_back(x^);
} bool solve()
{
for(int i=;i<*n;i+=)
if(!mark[i]&&!mark[i+])
{
c=;
if(!dfs(i))
{
while(c>) mark[S[--c]]=false;
if(!dfs(i+)) return false;
}
}
return true;
} //输出字典序最小的解
void Printf()
{
for(int i=;i<n;i++)
{ if(mark[*i]) printf("%dw",i);
else printf("%dh",i); if(i<n-) printf(" ");
else printf("\n");
}
}
}; int main()
{
TwoSAT T;
while(~scanf("%d%d",&N,&m))
{
if(N==&&m==) break;
T.n=N; T.init(T.n);
for(int i=;i<m;i++)
{
int a=,b=;
scanf("%s%s",s1,s2); for(int j=;j<strlen(s1)-;j++)
a=a*+s1[j]-'';
for(int j=;j<strlen(s2)-;j++)
b=b*+s2[j]-''; if(s1[strlen(s1)-]=='h') a=a*;
else a=a*+; if(s2[strlen(s2)-]=='h') b=b*;
else b=b*+; T.add_clause(a,b);
}
T.mark[]=;
if(T.solve()) T.Printf();
else printf("bad luck\n");
}
return ;
}
POJ 3648 Wedding的更多相关文章
- 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问题入门题目
Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ...
- POJ 3648 Wedding (2-SAT,经典)
题意:新郎和新娘结婚,来了n-1对夫妻,这些夫妻包括新郎之间有通奸关系(包括男女,男男,女女),我们的目地是为了满足新娘,新娘对面不能坐着一对夫妻,也不能坐着有任何通奸关系的人,另外新郎一定要坐新娘对 ...
- POJ.3648.Wedding(2-SAT)
题目链接 题意看这吧..https://www.cnblogs.com/wenruo/p/5885948.html \(Solution\) 每对夫妇只能有一个坐在新娘这一边,这正符合2-SAT初始状 ...
- POJ - 3648 Wedding (2-SAT 输出解决方案)
题意:有N-1对夫妇和1对新郎新娘要出席婚礼,这N对人要坐在走廊两侧.要求每对夫妇要坐在不同侧.有M对人有通奸关系,对于这一对人,不能同时坐在新娘对面(新娘新郎也可能和别人有通奸关系).求如何避免冲突 ...
- poj 3648 Wedding【2-SAT+tarjan+拓扑】
看错题*n,注意是输出新娘这边的-- 按2-SAT规则连互斥的边,然后注意连一条(1,1+n)表示新娘必选 然后输出color[belong[i]]==color[belong[1+n(新娘)]]的点 ...
- 【POJ】3648 Wedding
http://poj.org/problem?id=3648 题意:n对人(编号0-n-1,'w'表示第一个人,'h'表示第二个人),每对两个,人坐在桌子两侧.满足:1.每对人中的两个人不能坐在同一侧 ...
- Wedding (poj 3648 2-SAT 输出随意一组解)
Language: Default Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9004 Accept ...
- POJ 3648 2-sat
题目大意: 有一对新人结婚,邀请n对夫妇去参加婚礼. 有一张很长的桌子,人只能坐在桌子的两边,还要满 足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇 之中可能有通奸关系(包括男男,男女,女女) ...
随机推荐
- POJ 2387 Til the Cows Come Home(dij+邻接矩阵)
( ̄▽ ̄)" //dijkstra算法: //这题建邻接矩阵的时候有坑(先读入边后读入点),还有重边: #include<iostream> #include<cstdio ...
- phpstudy vhost 配置
server { listen 80 ; server_name user.fudaobank.com; index index.html index.htm index.php; root /ali ...
- 用 js 做 URL 跳转带来的 Referer 丢失问题.
http 302 重定向是可以保持 referer 的.例:在 A 页面上提交登录表单到 B,B 返回一个重定向页面到 C,在 C 处理里面检查 Referer 可知道它的来源是 A 而不是 B. 但 ...
- jq插件开发总结
http://www.cnblogs.com/silverLee/archive/2009/12/22/1629925.html jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQue ...
- js打印保存用户输入的内容
在用js打印局部页面时,遇到用户新输入的内容没能打印出来,经过观察,发现我采用的js打印方法是读取页面源代码,而用户输入的内容如果不将其写入到页面源代码中去,是打印不出来的,下面是我的解决方法: // ...
- 关于在MyEclipse中页面中文乱码的问题
1.首先在Window>preferences>General>Workspace中改为UTF-8. 2.将项目的Properties>Resource改为UTF-8. 3.将 ...
- 使WiFi具有保存历史连接的功能
在wpa_supplicant.conf里面添加这个功能 update_config=1 就能更新了,保存了历史的连接AP,不用再输入密码
- Redis 笔记
Redis是一个key-value存储系统.和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表).sets( ...
- Exams
Exams time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...
- Batch Sort
Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...