Wedding (poj 3648 2-SAT 输出随意一组解)


Language:
Default

Wedding

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9004   Accepted: 2722   Special Judge

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
side as her. It is considered bad luck to have a husband and wife seated on the same side of the table. Additionally, there are several pairs of people conducting adulterous relationships (both different-sex and same-sex relationships are possible), and it
is bad luck for the bride to see both members of such a pair. Your job is to arrange people at the table so as to avoid any bad luck.

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
couple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n - 1 with the bride and groom being 0w and 0h.

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
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0

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;
}
时间: 2024-11-24 19:24:32

Wedding (poj 3648 2-SAT 输出随意一组解)的相关文章

Light OJ 1251 Forming the Council 2-SAT输出任意一组解

题目来源:Light OJ 1251 Forming the Council 题意:若干了条件至少满足一个 求是否有方案 输出任意一种可能的方案 留下的人的个数 思路:2-SAT基础题 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 100010; int n, m; vector <int> G[maxn*2]; boo

HDU 1507 Uncle Tom&#39;s Inherited Land*(二分匹配,输出任意一组解)

<p style="margin: 10px auto; line-height: 19.5px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(40, 85, 126);"> 要输出任意一组解.</p><p style="margin: 10px auto; line-height: 19.5p

POJ 3648 Wedding(2-SAT 拓扑排序输出任意一种解决方案)

题目链接:http://poj.org/problem?id=3648 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 h

POJ 3648 Wedding (2-SAT+输出可行解)

题目地址:POJ 3648 这题终于AC了....没有专门对新郎新娘加一条边.. 这题前面一直读错题意了,调试了好长时间样例也没过..这题的意思是只要新郎那一边没有通奸的就可以,然后输出新娘那一边的人. 然后就是对那些有**关系的加边,由于新郎新娘必须要在两侧,所以最后要额外加一条边.然后用强连通判断,逆拓扑染色输出可行解即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include

poj 3683 2-sat问题,输出任意一组可行解

/* 2sat问题 输出任意一组可行解 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> using namespace std; #define N 2100 struct node { int u,v,next; }ff[N],bian[N*N*8]; int

BZOJ 3101(N皇后-N皇后O(n)构造一组解的方法)

3101: N皇后 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special Judge Submit: 70  Solved: 32 [Submit][Status] Description n*n的棋盘,在上面摆下n个皇后,使其两两间不能相互攻击- Input 一个数n Output 第i行表示在第i行第几列放置皇后 Sample Input 4 Sample Output 2 4 1 3 HINT 100%的数据3<n<1000000.输出

PHP中刷新输出缓冲详解[转载]

PHP中刷新输出缓冲详解 分类: PHP Web开发2011-07-23 17:42 1795人阅读 评论(0) 收藏 举报 phpbuffer浏览器outputapache模块脚本 buffer是一个内存地址空间,Linux系统默认大小一般为4096(1kb),即一个内存页.主要用于存储速度不同步的设备或者优先级不同的设备之间传办理数据的区域.通过buffer,可以使进程这间的相互等待变少.这里说一个通俗一点的例子,你打开文本编辑器编辑一个文件的时候,你每输入 一个字符,操作系统并不会立即把这

poj 3648 Wedding 2-SAT问题入门题目

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

poj 3648 Wedding

每对夫妻恰有一人坐在新娘对面,两个关系不正常的人不能都在新娘对面 问,是否有解 #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include&