General Graph Matching with a flower tree, matching with a flower tree

Source: Internet
Author: User
Tags cmath

General Graph Matching with a flower tree, matching with a flower tree

The matching of bipartite graphs mostly adopts the Hungary algorithm, while the matching of General graphs is a tree with flowers.

Problem DescriptionA new season of Touhou M-1 Grand Prix is approaching. girls in gensow.cannot wait for participant ipating it. before the registration, they have to decide which combination they are going to compete. every girl in Gensokyo is both a boke (funny girl) and a tsukkomi (straight girl ). every candidate combination is made up of two girls, a boke and a tsukkomi. A girl may belong to zero or more candidate combinations, but one can only register as a member of one formal combination. the host of Touhou M-1 Grand Prix hopes that as scheduled formal combinations as possible can participant ipate in this year. under these constraints, some candidate combinations are actually redundant as it \'s impossible to register it as a formal one as long as the number of formal combinations has to be maximized. so they want to figure out these redundant combinations and stop considering about them.
 
InputThere are multiple test cases. Process to the End of File.
The first line of each test case contains two integers: 1 ≤ N ≤ 40 and 1 ≤ M ≤ 123, Where N is the number of girls in gensoations, and M is the number of candidate combinations. the following M lines are M candidate combinations, one by each line. each combination is represented by two integers, the index of the boke girl 1 ≤ Bi ≤ N and the index of the tsukkomi girl 1 ≤ Ti ≤ N, where Bi! = Ti.
OutputFor each test case, output the number of redundant combinations in the first line. Then output the space-separated indexes of the redundant combinations in ascending order in the second line.
Sample Input
4 41 32 32 43 16 61 23 23 45 25 45 6
 
Sample Output
1232 4 5
 
AuthorZejun Wu (watashi)
Practice: Build an undirected graph. Generally, the graph is matched with flowers to determine whether the maximum matching effect is achieved.

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;pil temp[250];int ans[50];deque<int>Q;const int MAXN=110;bool mp[MAXN][MAXN],inque[MAXN],inblossom[MAXN],G[MAXN][MAXN];int match[MAXN],pre[MAXN],base[MAXN];int findancestor(int u,int v){    bool inpath[MAXN]={false};    while(1)    {        u=base[u];        inpath[u]=true;        if(match[u]==-1)break;        u=pre[match[u]];    }    while(1)    {        v=base[v];        if(inpath[v])return v;        v=pre[match[v]];    }}void reset(int u,int anc){    while(u!=anc)    {        int v=match[u];        inblossom[base[u]]=1;        inblossom[base[v]]=1;        v=pre[v];        if(base[v]!=anc)pre[v]=match[u];        u=v;    }}void contract(int u,int v,int n){    int anc=findancestor(u,v);    memset(inblossom,0,sizeof(inblossom));    reset(u,anc);reset(v,anc);    if(base[u]!=anc)pre[u]=v;    if(base[v]!=anc)pre[v]=u;    for(int i=1;i<=n;i++)        if(inblossom[base[i]])        {            base[i]=anc;            if(!inque[i])            {                Q.push_back(i);                inque[i]=1;            }        }}bool dfs(int S,int n){    for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;    Q.clear();Q.push_back(S);inque[S]=1;    while(!Q.empty())    {        int u=Q.front();Q.pop_front();        for(int v=1;v<=n;v++)        {            if(mp[u][v]&&base[v]!=base[u]&&match[u]!=v)            {                if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);                else if(pre[v]==-1)                {                    pre[v]=u;                    if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1;                    else                    {                        u=v;                        while(u!=-1)                        {                            v=pre[u];                            int w=match[v];                            match[u]=v;                            match[v]=u;                            u=w;                        }                        return true;                    }                }            }        }    }    return false;}int solve(int n){    CLEAR(match,-1);    int sum=0;    for(int i=1;i<=n;i++)        if(match[i]==-1&&dfs(i,n))            sum++;    return sum;}int main(){    int n,m;    int x,y;    while(~scanf("%d%d",&n,&m))    {        CLEAR(mp,0);        REPF(i,1,m)        {            scanf("%d%d",&x,&y);            temp[i].first=x;            temp[i].second=y;            mp[x][y]=mp[y][x]=1;        }        memcpy(G,mp,sizeof(mp));        int s=solve(n);        int k=0;        REPF(l,1,m)        {            int u=temp[l].first;            int v=temp[l].second;            memcpy(mp,G,sizeof(G));            REPF(i,1,n)            {                if(mp[u][i])                    mp[u][i]=mp[i][u]=0;                if(mp[v][i])                    mp[v][i]=mp[i][v]=0;            }            if(solve(n)+1<s)                ans[k++]=l;        }        printf("%d\n",k);        if(k==0)  puts("");        sort(ans,ans+k);        for(int i=0;i<k;i++)        {            if(i!=k-1)  printf("%d ",ans[i]);            else  printf("%d\n",ans[k-1]);        }    }    return 0;}

URL 1099:

1099. Work SchedulingTime limit: 0.5 second
Memory limit: 64 MB
There is certain amount of night guards that are available to protect the local junkyard from possible junk robberies. these guards need to scheduled in pairs, so that each pair guards at different night. the junkyard CEO ordered you to write a program which given the guards characteristics determines the maximum amount of scheduled guards (the rest will be fired ). please note that each guard can be scheduled with only one of his colleagues and no guard can work alone. inputThe first line of the input contains one numberN≤0. 222 which is the amount of night guards. Unlimited number of lines consisting of unordered pairs (I,J) Follow, each such pair means that guard #IAnd guard #JCan work together, because it is possible to find uniforms that suit both of them (The junkyard uses different parts of uniforms for different guards I. e. helmets, pants, jackets. it is impossible to put small helmet on a guard with a big head or big shoes on guard with small feet ). the input ends with Eof. outputYou shoshould output one possible optimal assignment. on the first line of the output write the even numberC, The amount of scheduled guards. Then outputC/2 lines, each containing 2 integers (I,J) That denote thatIAndJWill work together. Sample
Input Output
31 22 31 3
21 2

Direct output matching:

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;deque<int>Q;const int MAXN=250;bool mp[MAXN][MAXN],inque[MAXN],inblossom[MAXN],G[MAXN][MAXN];int match[MAXN],pre[MAXN],base[MAXN];int findancestor(int u,int v){    bool inpath[MAXN]={false};    while(1)    {        u=base[u];        inpath[u]=true;        if(match[u]==-1)break;        u=pre[match[u]];    }    while(1)    {        v=base[v];        if(inpath[v])return v;        v=pre[match[v]];    }}void reset(int u,int anc){    while(u!=anc)    {        int v=match[u];        inblossom[base[u]]=1;        inblossom[base[v]]=1;        v=pre[v];        if(base[v]!=anc)pre[v]=match[u];        u=v;    }}void contract(int u,int v,int n){    int anc=findancestor(u,v);    memset(inblossom,0,sizeof(inblossom));    reset(u,anc);reset(v,anc);    if(base[u]!=anc)pre[u]=v;    if(base[v]!=anc)pre[v]=u;    for(int i=1;i<=n;i++)        if(inblossom[base[i]])        {            base[i]=anc;            if(!inque[i])            {                Q.push_back(i);                inque[i]=1;            }        }}bool dfs(int S,int n){    for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;    Q.clear();Q.push_back(S);inque[S]=1;    while(!Q.empty())    {        int u=Q.front();Q.pop_front();        for(int v=1;v<=n;v++)        {            if(mp[u][v]&&base[v]!=base[u]&&match[u]!=v)            {                if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);                else if(pre[v]==-1)                {                    pre[v]=u;                    if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1;                    else                    {                        u=v;                        while(u!=-1)                        {                            v=pre[u];                            int w=match[v];                            match[u]=v;                            match[v]=u;                            u=w;                        }                        return true;                    }                }            }        }    }    return false;}int solve(int n){    CLEAR(match,-1);    int sum=0;    for(int i=1;i<=n;i++)        if(match[i]==-1&&dfs(i,n))            sum++;    return sum;}int main(){    int n;    int x,y;    while(~scanf("%d",&n))    {        CLEAR(mp,0);        while(~scanf("%d%d",&x,&y))            mp[x][y]=mp[y][x]=1;        int s=solve(n);        printf("%d\n",s*2);        REPF(i,1,n)        {            for(int j=i+1;j<=n;j++)                if(match[j]==i)                  printf("%d %d\n",i,j);        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.