国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU1498-二分圖行列匹配

2019-11-08 19:37:23
字體:
來源:轉載
供稿:網友

50 years, 50 colors

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2498    Accepted Submission(s): 1410PRoblem DescriptionOn Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nice, isn't it? To celebrate this meaningful day, the ACM team of HDU hold some fuuny games. Especially, there will be a game named "crashing color balloons".There will be a n*n matrix board on the ground, and each grid will have a color balloon in it.And the color of the ballon will be in the range of [1, 50].After the referee shouts "go!",you can begin to crash the balloons.Every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind.What's more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. Of course, a lot of students are waiting to play this game, so we just give every student k times to crash the balloons.Here comes the problem: which kind of balloon is impossible to be all crashed by a student in k times.
 InputThere will be multiple input cases.Each test case begins with two integers n, k. n is the number of rows and columns of the balloons (1 <= n <= 100), and k is the times that ginving to each student(0 < k <= n).Follow a matrix A of n*n, where Aij denote the color of the ballon in the i row, j column.Input ends with n = k = 0. OutputFor each test case, print in ascending order all the colors of which are impossible to be crashed by a student in k times. If there is no choice, print "-1". Sample Input
1 112 11 11 22 11 22 25 41 2 3 4 52 3 4 5 13 4 5 1 24 5 1 2 35 1 2 3 43 350 50 5050 50 5050 50 500 0 Sample Output
-1121 2 3 4 5-1 題目大意:

                  給你一個n*n的矩陣,矩陣中每個格子對應一種顏色的氣球,問你踩n次,每次只能選擇一行或一列踩掉一種顏色的所有氣球,最后還剩余的氣球顏色有哪些

題目思路:

                  首先看下數據范圍,氣球顏色只有50種最多,矩陣大小100*100最大,所以我們可以枚舉每種顏色,對于每種顏色,我們進行行列匹配,x為一個集合,y為一個集合,最后匹配到的最大匹配數就是踩掉所有氣球所需的最小次數,這個應該不難想到,如果畫個圖來理解就是最小點覆蓋,而最小點覆蓋=最大匹配!

AC代碼:

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn = 105;int n,k;int link[maxn],a[55],matrix[maxn][maxn];bool vis[maxn],mp[maxn][maxn];bool dfs(int u){    for(int i=1;i<=n;i++){        if(!vis[i]&&mp[u][i]){            vis[i]=true;            if(link[i]==-1||dfs(link[i])){                link[i]=u;                return true;            }        }    }    return false;}int main(){    while(scanf("%d%d",&n,&k),n+k){        memset(a,0,sizeof(a));        for(int i=1;i<=n;i++){            for(int j=1;j<=n;j++){                scanf("%d",&matrix[i][j]);                a[matrix[i][j]]=1;            }        }        int flag=1;        for(int i=1;i<=50;i++){                if(!a[i])continue;        //枚舉每種顏色,a[i]記錄該種顏色有沒有出現過            memset(mp,false,sizeof(mp));            memset(link,-1,sizeof(link));            int num = 0;            for(int j=1;j<=n;j++)                for(int l=1;l<=n;l++)                   if(matrix[j][l]==i)mp[j][l]=true;            for(int j=1;j<=n;j++){                memset(vis,false,sizeof(vis));                if(dfs(j))num++;            }            if(num>k){                if(flag){printf("%d",i);flag=0;}                else printf(" %d",i);            }        }        if(flag)printf("-1");        printf("/n");    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盱眙县| 珲春市| 烟台市| 金川县| 贡觉县| 神木县| 娄烦县| 昔阳县| 胶南市| 三都| 巴青县| 广平县| 新宾| 竹北市| 长白| 利川市| 广饶县| 涞水县| 昔阳县| 平南县| 达拉特旗| 大同县| 青冈县| 通渭县| 固始县| 屏东市| 濮阳市| 汤阴县| 大余县| 平果县| 香港 | 揭东县| 长阳| 民和| 高邑县| 定边县| 晋州市| 杭州市| 岑溪市| 灵丘县| 深泽县|