poj 3648 Wedding

来源:互联网 发布:电影台词拼接的软件 编辑:程序博客网 时间:2024/05/21 23:00

每对夫妻恰有一人坐在新娘对面,两个关系不正常的人不能都在新娘对面

问,是否有解

#include<iostream>#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<algorithm>using namespace std;const int MAXN = 100;struct twosat{      int n,c;      vector<int> g[MAXN<<1];      bool mark[MAXN<<1];      int s[MAXN<<1];      bool dfs(int x) {  int i;        if (mark[x^1]) return 0;          if (mark[x]) return 1;          mark[x] = 1;          s[c++] = x;          for (i = 0; i < g[x].size(); i++)              if (!dfs(g[x][i])) return 0;          return 1;      }          void init(int n) {  int i,t=n<<1;        this->n = n;          for (i = 0; i < t; i++)               g[i].clear();          memset(mark, 0, sizeof(mark));      }        void add_clause(int x,int y) {          g[x].push_back(y^1);          g[y].push_back(x^1);    }        bool solve() {  int i,t=n<<1;        for (i = 0; i < t; i += 2)               if (!mark[i] && !mark[i + 1]) {                  c = 0;                  if (!dfs(i)) {                      while (c > 0) mark[s[--c]] =0;                       if (!dfs(i + 1)) return 0;                  }              }        return 1;      }  }ender;int main(){bool flag;int i,n,m,a,b,x,y;char t1,t2;while(cin>>n>>m){if(n==0&&m==0)break;ender.init(n);ender.g[0].push_back(1);while(m--){scanf("%d%c%d%c",&a,&t1,&b,&t2);if(t1=='w')x=0;elsex=1;if(t2=='w')y=0;elsey=1;a=a*2+x;b=b*2+y;ender.add_clause(a,b);}if(ender.solve()){n<<=1;flag=0;for(i=2;i<n;i++)if(ender.mark[i]&&i!=1){if(flag)cout<<" ";cout<<i/2;if(i&1)cout<<"w";elsecout<<"h";flag=1;}}elsecout<<"bad luck";cout<<endl;}return 0;}

Wedding
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8519 Accepted: 2571 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 givesn, 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 ton - 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 63h 7h5w 3w7h 6w8w 3w7h 3w2w 5h0 0

Sample Output

1h 2h 3w 4h 5h 6h 7h 8h 9h

Source

Waterloo Local Contest, 2007.9.29

0 0
原创粉丝点击