HDOJ 4687 Boke and Tsukkomi 一般圖最大匹配帶花樹+暴力,hdojtsukkomi

來源:互聯網
上載者:User

HDOJ 4687 Boke and Tsukkomi 一般圖最大匹配帶花樹+暴力,hdojtsukkomi


一般圖最大匹配帶花樹+暴力:

先算最大匹配 C1

在枚舉每一條邊,去掉和這條邊兩個端點有關的邊.....再跑Edmonds得到匹配C2

如果C2+2==C1則這條邊再某個最大匹配中

Boke and TsukkomiTime Limit: 3000/3000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 649    Accepted Submission(s): 202


Problem DescriptionA new season of Touhou M-1 Grand Prix is approaching. Girls in Gensokyo cannot wait for participating it. Before the registration, they have to decide which combination they are going to compete as. 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 many formal combinations as possible can participate 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 Gensokyo, 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) 
Source2013 Multi-University Training Contest 9 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int maxn=50;vector<int> ans;bool G[maxn][maxn],TG[maxn][maxn];int n,m;int Match[maxn];int Start,Finish,NewBase;int Father[maxn],Base[maxn];bool InQueue[maxn],InPath[maxn],InBlossom[maxn];int Count;queue<int> q;int FindCommonAncestor(int u,int v){  memset(InPath,false,sizeof(InPath));  while(true)    {      u=Base[u];      InPath[u]=true;      if(u==Start) break;      u=Father[Match[u]];    }  while(true)    {      v=Base[v];      if(InPath[v]) break;      v=Father[Match[v]];    }  return v;}void ResetTrace(int u){  int v;  while(Base[u]!=NewBase)    {      v=Match[u];      InBlossom[Base[u]]=InBlossom[Base[v]]=true;      u=Father[v];      if(Base[u]!=NewBase) Father[u]=v;    }}void BlossomContract(int u,int v){  NewBase=FindCommonAncestor(u,v);  memset(InBlossom,false,sizeof(InBlossom));  ResetTrace(u); ResetTrace(v);  if(Base[u]!=NewBase) Father[u]=v;  if(Base[v]!=NewBase) Father[v]=u;  for(int tu=1;tu<=n;tu++)    {      if(InBlossom[Base[tu]])        {          Base[tu]=NewBase;          if(!InQueue[tu])            {              q.push(tu);              InQueue[tu]=true;            }        }    }}void FindAugmentingPath(){  memset(InQueue,false,sizeof(InQueue));  memset(Father,0,sizeof(Father));  for(int i=1;i<=n;i++)    Base[i]=i;  while(!q.empty()) q.pop();  q.push(Start); InQueue[Start]=true;  Finish=0;  while(!q.empty())    {      int u=q.front(); q.pop();      InQueue[u]=false;      for(int i=1;i<=n;i++)        {          if(i==u||G[u][i]==false) continue;          int v=i;          if(Base[u]!=Base[v]&&Match[u]!=v)            {              if(v==Start||(Match[v]>0&&Father[Match[v]]>0))                BlossomContract(u,v);              else if(Father[v]==0)                {                  Father[v]=u;                  if(Match[v]>0)                    {                      q.push(Match[v]);                      InQueue[Match[v]]=true;                    }                  else                    {                      Finish=v;                      return ;                    }                }            }        }    }}void AugmentPath(){  int u,v,w;  u=Finish;  while(u>0)    {      v=Father[u];      w=Match[v];      Match[v]=u;      Match[u]=v;      u=w;    }}void Edmonds(){  memset(Match,0,sizeof(Match));  for(int u=1;u<=n;u++)    {      if(Match[u]==0)        {          Start=u;          FindAugmentingPath();          if(Finish>0) AugmentPath();        }    }}int bian[200][2];int main(){  while(scanf("%d%d",&n,&m)!=EOF)    {      memset(G,0,sizeof(G));      memset(TG,0,sizeof(TG));      ans.clear();      for(int i=0;i<m;i++)        {          int u,v;          scanf("%d%d",&u,&v);          bian[i][0]=u;bian[i][1]=v;          G[u][v]=G[v][u]=1;          TG[u][v]=TG[v][u]=1;        }      Edmonds();      Count=0;      for(int i=1;i<=n;i++)        if(Match[i]) Count++;      for(int i=0;i<m;i++)        {          int u=bian[i][0],v=bian[i][1];          ///clear about u,v          for(int j=1;j<=n;j++)            {              G[u][j]=G[j][u]=G[j][v]=G[v][j]=0;            }          Edmonds();          int C2=0;          for(int j=1;j<=n;j++)              if(Match[j]) C2++;          if(C2<Count-2)            ans.push_back(i+1);          ///Recover          for(int j=1;j<=n;j++)            {              G[u][j]=TG[u][j]; G[j][u]=TG[j][u];              G[v][j]=TG[v][j]; G[j][v]=TG[j][v];            }        }        int sz = ans.size();        printf("%d\n",sz);        for(int i=0;i<sz;i++)        {            printf("%d",ans[i]);            if(i<sz-1)printf(" ");        }        printf("\n");    }  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.