NOJ [1182] Counter-Strike

二分匹配,求最大匹配

之前学了链式前向星,一直没用过,现在拿来试试,不会链式前向星的可以去我博客看看,点击打开链接

  • 问题描述
  • Do you ever remember the classic game - Counter-Strike?

    STANMASH is a team member of a CS Team.
    Today his team is against another team.
    Before the starting, STANMASH, the leader of the team, decided to analyze the Combat Effectiveness of each team member. He found that - member A can kill someone in another team; member B can kill someone in another team, etc.
    So he made a list that who can kill whom. We assume that one member only can kill one enemy in that map.

    How many enemies can they kill in maximum? (We assume that STANMASH's team is larger than or the same as another's, but they can only let the same number as the enemy team member to play this match).
  • 输入
  • This problem contains several cases.
    The first line of each case is two integers N and M (1 <= M <= N <= 500), indicate the number of STANMASH team's members and another team's members.
    Then follow N lines. The first integer of ith line Ki indicates the number of enemies that ith team member can kill (0 <= Ki <= M). Then follow Ki integers, each integer is the ID of enemy that he can kill. (1 <= ID <= M)
  • 输出
  • For each case, you should output that the maximum number of enemies that they can kill.

#include<stdio.h>
#include<string.h>
#define maxn 510
bool vis[maxn]; //记录v2的点有没有被访问过
int match[maxn];
int n,m;
struct node
{
  int next,to;
}mat[maxn*maxn];
int head[maxn*maxn];
int edgenum;
void add(int begin,int end)
{
  mat[edgenum].to=end;
  mat[edgenum].next=head[begin];
  head[begin]=edgenum++;
}
bool dfs(int u)
{
  int i;
  for(i=head[u];i!=-1;i=mat[i].next)
  {
    int to=mat[i].to;

    if(!vis[to]) 
    {
      vis[to]=1;
      if(match[to]==-1 || dfs(match[to]))  //如果i没出现在匹配中,或者i在匹配中,但从i的临界点出发,可以有增广路
      {
        match[to]=u;

        return true;
      }
    }
  }
  return false;
}

int main()
{
  while(~scanf("%d%d",&n,&m))
  {int i,j,k,cnt;
    memset(head,-1,sizeof(head));
    memset(match,-1,sizeof(match));
    
    int ans=0;
    edgenum=0;
    for(i=1;i<=n;i++)
    {
      scanf("%d",&cnt);
      while(cnt--)
      {
        scanf("%d",&k);
        add(i,k);
      }
    }
     
    for(i=1;i<=n;i++)
   {
       memset(vis,0,sizeof(vis));
       if(dfs(i))
       {
        ans++;
        //printf("%d\n",ans);
       }
         
    }
    printf("%d\n",ans);
  }
  return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值