Girls and Boys -独立集

本文探讨了一种解决最大独立集问题的算法,该问题旨在从一组学生中找出最大的子集,使得子集中任意两人之间不存在浪漫关系。通过使用深度优先搜索(DFS)和匹配算法,我们能够有效地找到满足条件的最大子集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  •  
  • 题意:学校对n个学生(男女都有)进行的调查了,发现了某些学生暗生情愫,现在需要你选出一个最大的集合,这个集合内部没有两个人暗生情愫。学生的编号是0~n-1The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. 
  • 思路:模板最大独立集。注意while输入~~~~scanf
  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 1001
    int n,net[maxn],x,m;
    vector<int>mmp[maxn];
    bool vis[maxn];
    bool dfs(int u)
    {
        for(int i=0; i<mmp[u].size(); i++)
        {
            int v=mmp[u][i];
            if(!vis[v])
            {
                vis[v]=1;
                if(net[v]==-1||dfs(net[v]))
                {
                    net[v]=u;
                    net[u]=v;
                    return true;
                }
            }
        }
        return false;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            for(int i=0; i<n; i++)
            {
                net[i]=-1;
                scanf("%d: (%d)",&x,&m);
                while(m--)
                {
                    scanf("%d",&x);
                    mmp[i].push_back(x);
                }
            }
            x=0;
            for(int i=0; i<n; i++)
                if(net[i]<0)
                {
                    memset(vis,0,sizeof(vis));
                    if(dfs(i))
                        x++;
                }
            printf("%d\n",n-x);
            for(int i=0; i<n; i++)
                mmp[i].clear();
        }
        return 0;
    }
    
  •  
  •  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值