HDU4685 Prince and Princess 完美匹配+強連通,hdu4685princess

來源:互聯網
上載者:User

HDU4685 Prince and Princess 完美匹配+強連通,hdu4685princess

題意:現在有n個王子,m個公主。現在要給他們配對,王子會和他喜歡的一個人結婚,而公主不能做選擇。

這題啃得好費勁,有個類似的題目poj1904,那個題目也是給王子與公主配對,但那個是王子公主各n個,且給定了一個完美匹配,然後求每個王子可以做出的選擇且不影響最大匹配數目。那題是先建各條喜歡關係的邊,然後在由被選擇的公主連一條邊到與之配對的王子,強連通之後如果一個王子和一個公主在一個強連通分量中,那麼他們結合的話,他們的另一半也各自能在強連通中找到另外的匹配,就是符合題意的結果了。

這個題目算是升級版把,我們需要做的先是用匈牙利演算法求出最大匹配res,然後建立m-res個虛擬王子與m-res單身公主準備匹配,建立n-res個虛擬公主與n-res個單身王子準備匹配,過程就是虛擬王子要喜歡每一個公主,同樣虛擬公主也要被每一個王子喜歡,這樣最大匹配一定是n+m-res.求出一個這樣的完美匹配,然後再套用poj1904的思路用強連通做。建議先做下1904呦。

代碼:

#include<iostream>#include<cstdio>#include<string>#include<cmath>#include<queue>#include<stack>#include<map>#include<cstring>#include<algorithm>#define rep(i,a,b) for(int i=(a);i<(b);i++)#define rev(i,a,b) for(int i=(a);i>=(b);i--)#define clr(a,x) memset(a,x,sizeof a)#define inf 0x3f3f3f3ftypedef long long LL;using namespace std;const int eps=0.00000001;const int maxn=2005;const int maxm=maxn*maxn/2;int first[maxn],link[maxn];int nex[maxm],w[maxm],v[maxm],u[maxm];bool done[maxn],g[maxn][maxn];int n,m,ecnt;void add_(int a,int b,int c=0){    u[ecnt]=a;    v[ecnt]=b;    w[ecnt]=c;    nex[ecnt]=first[a];    first[a]=ecnt++;}bool dfs(int s){    for(int e=first[s];~e;e=nex[e])    if(!done[v[e]])    {        done[v[e]]=true;        if(link[v[e]]==-1||dfs(link[v[e]]))        {            link[v[e]]=s;            return true;        }    }    return 0;}int hungary(int n){    int ans=0;    clr(link,-1);    for(int i=1;i<=n;i++)    {        clr(done,false);        if(dfs(i))ans++;    }    return ans;}int low[maxn],dfn[maxn],stck[maxn],belong[maxn];int index,top,scc;bool ins[maxn];int num[maxn];int in[maxn],out[maxn];void tarjan(int u){    low[u]=dfn[u]=++index;    stck[top++]=u;    ins[u]=1;    for(int e=first[u];~e;e=nex[e])    {        if(!dfn[v[e]])        {            tarjan(v[e]);            low[u]=min(low[u],low[v[e]]);        }        else if(ins[v[e]])low[u]=min(low[u],dfn[v[e]]);    }    if(low[u]==dfn[u])    {        int v;        scc++;        do        {            v=stck[--top];            ins[v]=false;            belong[v]=scc;            num[scc]++;        }while(v!=u);    }}void solve(int n){    clr(dfn,0);    clr(ins,0);    clr(num,0);    index=scc=top=0;    for(int i=1;i<=n;i++)        if(!dfn[i])tarjan(i);}int main(){    int t,a,b,c,k,cas=1,key=1000;    scanf("%d",&t);    while(t--)    {        clr(first,-1);ecnt=0;        clr(g,false);        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)        {            scanf("%d",&k);            while(k--)            {                scanf("%d",&a);                if(!g[i][a])                {                    g[i][a]=1;                    add_(i,a+key);                }            }        }        int res=hungary(n);        int nn=n+m-res;        for(int i=n+1;i<=nn;i++)            for(int j=1;j<=nn;j++)            add_(i,j+key),g[i][j]=1;        for(int i=1;i<=n;i++)            for(int j=m+1;j<=nn;j++)            add_(i,j+key),g[i][j]=1;        hungary(nn);        ecnt=0;clr(first,-1);        for(int i=1;i<=nn;i++)            if(link[i+key]!=-1)add_(i+nn,link[i+key]);        for(int i=1;i<=nn;i++)            for(int j=1;j<=nn;j++)            if(g[i][j])add_(i,j+nn);        solve(2*nn);        printf("Case #%d:\n",cas++);        int ans[1000];        for(int i=1;i<=n;i++)        {            int en=0;            for(int j=1;j<=m;j++)                if(g[i][j]&&belong[j+nn]==belong[i])ans[en++]=j;            printf("%d",en);            for(int i=0;i<en;i++)                printf(" %d",ans[i]);            puts("");        }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.