50 years, 50 colorsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3849 Accepted Submission(s): 2132 Problem Description On 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.
Input There 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.
Output For 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 1 1 2 1 1 1 1 2 2 1 1 2 2 2 5 4 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 5 1 2 3 4 3 3 50 50 50 50 50 50 50 50 50 0 0
Sample Output -1 1 2 1 2 3 4 5 -1
Author 8600
Source “2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛
Recommend LL |
给出一个n*n矩阵,每个数字代表一种颜色,每次只能消灭同一行和同一列的气球,问有多少种气球需要>k次才能消灭。
对样例来说:(对于颜色1来说)把每种颜色的点 横坐标看作左部,纵坐标看作右部,形成一个二分图。
用最小的行或列消灭气球,其实就是求每个颜色的最小覆盖点。图中最小的覆盖点为2.
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 1000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m;
int vis[maxn];
int pre[maxn];
int num[maxn][maxn];
int mark[maxn][maxn];
bool finds(int x,int v)
{
for(int i=1; i<=n; i++)
{
if(mark[x][i]==v&&!vis[i])
{
vis[i]=1;
if(pre[i]==0||finds(pre[i],v))
{
pre[i]=x;
return 1;
}
}
}
return 0;
}
set<int>s;
set<int>::iterator it;
int ans[maxn];
int dfs(int x)
{
for(int i=1; i<=n+10; i++)
{
pre[i]=0;
}
int ans=0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n+10; j++)
vis[j]=0;
if(finds(i,x))
ans++;
}
return ans;
}
int main()
{
while(scanf("%d %d",&n,&m)&&n&&m)
{
while(s.size())
s.erase(s.begin());
int cnt=0;
for(int i=1; i<=n+10; i++)
{
pre[i]=0;
for(int j=1; j<=n+10; j++)
{
num[i][j]=mark[i][j]=0;
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
int v;
scanf("%d",&mark[i][j]);
s.insert(mark[i][j]);
}
}
for(it=s.begin(); it!=s.end(); it++)
{
if(dfs(*it)>m)
{
ans[++cnt]=*it;
}
//cout<<dfs(*it)<<endl;
}
if(!cnt)
{
printf("-1\n");
continue;
}
for(int i=1; i<=cnt; i++)
{
if(i!=cnt)
printf("%d ",ans[i]);
else
printf("%d\n",ans[i]);
}
}
return 0;
}