离散作业之goodguy and badguy

 1. There are two types of professional wrestlers: "good guys" and "bad guys." Between any pair
of professional wrestlers, there may or may not be a rivalry. Suppose we have n professional
wrestlers and we have a list of r pairs of wrestlers for which there are rivalries. Give an O(n +
r)-time algorithm that determines whether it is possible to designate some of the wrestlers as
good guys and the remainder as bad guys such that each rivalry is between a good guy and a
bad guy. If is it possible to perform such a designation, your algorithm should produce it.

 

这个月太忙,都没时间写博客

刚写了离散作业,实际是BFS的变体

直接上代码

 

bool Graph::designation(int s){
 initBFS(); 

 graNodes[s].color=GRAY;
 graNodes[s].level=0;
 
 deque<Node*> blocks;
 blocks.clear();

 blocks.push_back(&graNodes[s]);
 Node* tempNode;
 while(!blocks.empty()){
  tempNode=blocks.front();
  blocks.pop_front();
  cout<<"*"<<tempNode->nodeID<<"*";
  for (vector<Node*>::iterator ivl = (tempNode->adjList).begin(); ivl != (tempNode->adjList).end(); ivl++) {
   if(((*ivl)->color)!=WHITE){
    if(((*ivl)->level%2)==(tempNode->level%2)){    
     cout<<"节点"<<(*ivl)->nodeID<<"和节点"<<(tempNode->nodeID)<<"不能满足条件"<<endl;
     return false;
    }
   }
   else{
    (*ivl)->color=GRAY;
    (*ivl)->level=tempNode->level+1;
    (*ivl)->pre=tempNode->nodeID;
    blocks.push_back(*ivl);
   }
   
  }
  tempNode->color=BLACK;
 }
 

 return true;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值