国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安溪县| 灵台县| 罗城| 道真| 将乐县| 广水市| 博白县| 中方县| 旌德县| 民权县| 开江县| 博白县| 南木林县| 齐齐哈尔市| 石泉县| 根河市| 怀化市| 新晃| 长寿区| 南澳县| 江西省| 和硕县| 东乌| 微山县| 托克托县| 济宁市| 镇雄县| 大同县| 全椒县| 威信县| 中牟县| 卓资县| 理塘县| 莱西市| 安远县| 酒泉市| 吉木萨尔县| 金堂县| 望城县| 周至县| 四川省|