P2763 试题库问题

原题链接

题解

很抽象的建模,我一开始想的是韦恩图,然后韦恩图里选取 若干 个点,还要保证每个图都能选上,然后把韦恩图抽象成点,图中的点抽象成待匹配的点,然后就是二分图匹配了

code

#include<bits/stdc++.h>
#define ll long long
#define lb long double
#define lowbit(x) ((x)&(-x))
using namespace std;
const ll inf=1e18;
const ll mod=1e9+7;
const ll N=4e5;
ll qpow(ll a,ll n)
{
    ll res=1;
    while(n)
    {
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}
ll inv(ll x)
{
    return qpow(x,mod-2);
}
ll fa[2000005];
ll finds(ll now){return now==fa[now]?now:finds(fa[now]);}

vector<ll> G[200005];

ll dfn[200005],low[200005];
ll cnt=0,num=0;
ll in_st[200005]={0};
stack<ll> st;
ll belong[200005]={0};

void scc(ll now,ll fa)
{
    dfn[now]=++cnt;
    low[now]=dfn[now];
    in_st[now]=1;
    st.push(now);

    for(auto next:G[now])
    {
        if(next==fa) continue;

        if(!dfn[next])
        {
            scc(next,now);
            low[now]=min(low[now],low[next]);
        }
        else if(in_st[next])
        {
            low[now]=min(low[now],dfn[next]);
        }
    }

    if(low[now]==dfn[now])
    {
        ll x;
        num++;
        do
        {
            x=st.top();
            st.pop();
            in_st[x]=0;
            belong[x]=num;
        }while(x!=now);
    }
}

ll match[1005]={0};

ll vis[1005]={0};

bool zhao(ll now)
{
    for(auto next:G[now])
    {
        if(vis[next]) continue;
        vis[next]=1;
        if(!match[next]||zhao(match[next]))
        {
            match[next]=now;
            return 1;
        }
    }
    return 0;
}

ll need[1005];
void solve()
{
    ll k,n;
    cin>>k>>n;

    for(ll i=1;i<=k;i++) cin>>need[i];

    for(ll i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        for(ll j=1;j<=x;j++)
        {
            ll y;
            cin>>y;

            G[y].push_back(i);
        }
    }


    for(ll i=1;i<=k;i++)
    {
        memset(vis,0,sizeof vis);
        ll cnt=0;
        for(ll j=1;j<=need[i];j++)
        {
            if(zhao(i)) cnt++;
        }
        if(cnt!=need[i])
        {
            cout<<"No Solution!";
            return;
        }
    }
    vector<ll> ans[k+1];
    for(ll i=1;i<=n;i++)
    {
        if(match[i])
        {
            ans[match[i]].push_back(i);
        }
    }

    for(int i=1;i<=k;i++)
    {
        cout<<i<<": ";
        for(auto it:ans[i]) cout<<it<<' ';
        cout<<'\n';
    }
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int TT=1;
    //cin>>TT;
    while(TT--) solve();
    return 0;
}


posted @   纯粹的  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示