Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10556   Accepted: 3220   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
题意:题意:一对新婚的夫妇邀请(n-1)对夫妇来参加自己的宴会,这对新人以及这些受邀请的夫妇都坐在长桌子的两边,新娘和新郎分别坐在桌子的两侧,新娘不希望看到她邀请来的那些夫妇之中有妻子和丈夫坐在同一边的情况(即妻子和丈夫要分作桌子的两边),在这n对夫妇中有一些男女存在着暧昧的关系,所以新娘也不希望看到有暧昧关系的人坐在她对面的那一侧.求解是否存在一种满足新娘要求的座位分配方案,如果存在的话,那么就输出这方案,否则输出"bad luck".
这个问题本身就有矛盾点了,即男性还是女性坐在女0对面,所以不用拆点。
另外注意加边只要加两条单向边,因为只关注女0的对面有没有奸夫淫妇,所以就是一旦有一个奸夫淫妇中的一个在新娘对面,那么另外一个的配偶必然也会在其对面,所以建边两个,注意没有同性恋有暧昧关系也是一个非常重要的条件
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N=;
vector<int>v[N];
vector<int>g[N];
bool instack[N];
int q[N],low[N],dfn[N],bl[N],con[N],col[N],in[N];
int l,scnt,cnt,n,m;
void init(){
   memset(dfn,,sizeof(dfn));
   memset(in,,sizeof(in));
   memset(col,,sizeof(col));
   for(int i=;i<=*n;++i) v[i].clear(),g[i].clear();
   l=scnt=cnt=;
}
inline void add(int a,int b){v[a].push_back(b);}
void Tarjan(int u){
   instack[u]=;
   low[u]=dfn[u]=++cnt;
   q[l++]=u;
   for(int i=;i<(int)v[u].size();++i) {
    int x=v[u][i];
    if(!dfn[x]) {
        Tarjan(x);
        low[u]=min(low[u],low[x]);
    }
    else if(instack[x]&&dfn[x]<low[u]) low[u]=dfn[x];
   }
   if(low[u]==dfn[u]){
    int  t;++scnt;
    do{
        t=q[--l];
        bl[t]=scnt;
        instack[t]=;
    }while(t!=u);
   }
}
void rebuild(){
    for(int i=;i<*n;++i) for(int j=;j<(int)v[i].size();++j){
        int a=bl[i],b=bl[v[i][j]];
        if(a!=b) {
            ++in[a];
            g[b].push_back(a);
        }
    }
}
void topsort(){
    queue<int>Q;
    for(int i=;i<=scnt;++i) if(!in[i]) Q.push(i);
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        if(!col[u]) {
            col[u]=;
            col[con[u]]=;
        }
        for(int i=;i<(int)g[u].size();++i) {
            int x=g[u][i];
            --in[x];
            if(!in[x]) Q.push(x);
        }
    }
}
void solve(){
    for(int i=;i<*n;++i) if(!dfn[i]) Tarjan(i);
    for(int i=;i<*n;i+=) {if(bl[i]==bl[i+]) {puts("bad luck");return;}
    int a=bl[i],b=bl[i+];
    con[a]=b;con[b]=a;}
    rebuild();
    topsort();
    for(int i=;i<*n;i+=){
        if(i!=) printf(" ");
        if(col[bl[i]]==col[bl[]]) printf("%dw",i/);
        else printf("%dh",i/);
    }
    puts("");
}
int main(){
    while(scanf("%d%d",&n,&m),n+m){
        init();
        int a,b;
        char c,d;
        while(m--){
            scanf("%d%c %d%c",&a,&c,&b,&d);
            if(c=='h') a=*a+;else a=*a;
            if(d=='h') b=*b+;else b=*b;
            add(a,b^);
            add(b,a^);
        }
        add(,);
        solve();
    }
}

最新文章

  1. 事件的委托处理 javascript
  2. ASP.net的文件扩展名
  3. 福利-&gt;KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型
  4. 【struts2】Result和ResultType
  5. Nginx学习笔记(八) Nginx进程启动分析
  6. ASP.NET跨服务器上传文件的相关解决方案
  7. 总结隐藏Ribbon菜单的方法
  8. 关于Token
  9. How to distribute your own Android library through jCenter and Maven Central from Android Studio
  10. IIS报500.0错误
  11. HDU 1276 士兵队列训练问题
  12. 大约apache 2.4.X虚拟主机配置问题的版本号后,
  13. [转载] Kafka+Storm+HDFS整合实践
  14. JavaScript Date 时间对象方法
  15. (七十)Xcode5及以上对于状态栏和导航栏样式的设定方法
  16. ELK搭建&lt;三&gt;:安装Kibana
  17. 设计模式四: 抽象工厂(Abstract Factory)
  18. 【原创】大数据基础之Flink(1)简介、安装、使用
  19. ui component 是一个前端 mvc 开发框架
  20. Windows下安装单机Kafka

热门文章

  1. oracle查询当前系统时间前10天的数据
  2. 【10月新版】Aspose.Pdf 10月新版V17.10发布 | 附下载
  3. WordPress发布文章/页面时自动添加默认的自定义字段
  4. 熟透vue手机购物商城开发的重要性
  5. centos6 yum安装jdk1.8+
  6. 算法竞赛进阶指南--在单调递增序列a中查找小于等于x的数中最大的一个(即x或x的前驱)
  7. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
  8. codeforce 270C Magical Boxes
  9. python selenium(键盘事件 Keys 类)
  10. muduo网络库源码学习————Exception类