HDOJ 1498 – 50 years, 50 colors 水二分圖..二分圖最小點覆蓋

來源:互聯網
上載者:User

            題意:

                     在一個n*n的矩陣中(n<=100)..每個格子放了一種顏色的氣球(顏色用數字1~50表示)...每次的操作是消除一行或者一列顏色相同的氣球.問經過k次操作..哪些顏色的氣球還存在...

            題解:

                     每種顏色分開來求...做邊(x,y)....由於對於一個氣球(x,y)..只要它x被用了或者y被用了..那麼這個氣球也就消除了...題目轉化為最少要用多少個點..使得所有的邊都覆蓋..也就是經典的二分圖最小點覆蓋問題...等價為求二分圖的最大匹配數...

Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<queue>#define ll long long#define MAXN 105using namespace std;int n,match[MAXN];bool arc[MAXN][MAXN],inmap[MAXN],used[MAXN],s[MAXN][MAXN]; bool dfs(int x){       int i;       for (i=1;i<=n;i++)          if (arc[x][i] && !used[i])          {                 used[i]=true;                 if (!match[i] || dfs(match[i]))                 {                       match[i]=x;                       return true;                 }          }              return false; }int getmax(){       int sum=0;       memset(match,0,sizeof(match));       for (int i=1;i<=n;i++)       {               memset(used,false,sizeof(used));               sum+=dfs(i);       }       return sum;}int main(){       int i,j,k,ans;       bool f;        while (~scanf("%d%d",&n,&k) && n)       {               memset(inmap,false,sizeof(inmap));               for (i=1;i<=n;i++)                  for (j=1;j<=n;j++)                     scanf("%d",&s[i][j]),inmap[s[i][j]]=true;               f=false;               for (int t=1;t<=50;t++)                  if (inmap[t])                  {                         memset(arc,false,sizeof(arc));                         for (i=1;i<=n;i++)                            for (j=1;j<=n;j++)                                if (s[i][j]==t) arc[i][j]=true;                         if (getmax()>k)                          {                                 if (!f) printf("%d",t);                                    else printf(" %d",t);                                 f=true;                         }                  }               if (!f) printf("-1");               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.