poj 1719Shooting Contest
//本题大意是对于一个r*c的矩阵,每一列有两个是白色的
//如今选c个位置,要求每一行至少有一个白色的方格被选上
//每一列仅仅能选一个
//用二分匹配求出最大匹配,假设最大匹配等于r,则满足
//每一行至少有一个白色的格子被选上
//注意c>r的情况
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1010;
int line[maxn][maxn];
int match[maxn];
int match_ans[maxn];
int vis[maxn];
int r,c;
int find(int start)
{
int i ;
for(i=1;i<=r;i++)
{
if(line[start][i]&&!vis[i])
{
vis[i]=1;
if(match[i]==-1||find(match[i]))
{
match[i] = start ;
match_ans[start] = i;
return 1;
}
}
}
return 0;
}
void Match()
{
int i;
memset(match , -1 ,sizeof(match));
int ans = 0;
for(i = 1; i <= c;i++)
{
memset(vis , 0 , sizeof(vis));
if(find(i))
ans++;
}
if(ans == r)
{
for(i = 1;i <= c; i++)
{
if(!match_ans[i])
{
for(int j = 1;j <= r ;j++)
if(line[i][j])
{
printf("%d%c",j,i == c?'\n':' ');
break;
}
}
else
printf("%d%c",match_ans[i],i == c?
'\n':' ');
}
}
else
printf("NO\n");
}
int main()
{
//freopen("in.txt", "r" ,stdin);
int T;
scanf("%d", &T);
while(T--)
{
memset(line, 0 ,sizeof(line));
memset(match_ans , 0 ,sizeof(match_ans));
scanf("%d%d" ,&r , &c);
int a,b;
for(int i = 1; i <= c ;i++)
{
scanf("%d%d",&a ,&b);
line[i][a] = line[i][b] = 1;
}
Match();
}
return 1;
}
poj 1719Shooting Contest的更多相关文章
- poj 3660Cow Contest
题目链接:http://poj.org/problem?id=3660 有n头奶牛还有m种关系a,b表示a牛逼b彩笔,所以a排名比b高 最后问你给出的关系最多能确定多少头奶牛的排名,而且给出的数据不会 ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
- POJ 3278 The merchant
传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there i ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- poj 3744 Scout YYF I (矩阵)
Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...
- [POJ 3734] Blocks (矩阵高速幂、组合数学)
Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3997 Accepted: 1775 Descriptio ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...
随机推荐
- ping & traceroute 原理
说明: 忘记从哪里看到的原文了. 不过我应该进行了大刀阔斧的删选. ping 用类型码为0的ICMP发请 求,受到请求的主机则用类型码为8的ICMP回应. ping程序来计算间隔时间,并计算有多少个包 ...
- android.useDeprecatedNdk=true
android.useDeprecatedNdk=true ndk{ moduleName "aa" abiFilter "armeabi-v7a" }
- centos 解决:Another app is currently holding the yum lock; waiting for it to exit
centos执行yum时出现错误: Loaded plugins: fastestmirror, refresh-packagekit, security Existing lock /var/run ...
- django怎么自己创建一个中间件
中间件是什么? 中间件是类似flask函数中钩子函数的东西.可以在请求视图函数前,或者视图函数响应后处理某些事情.中间件对全部视图都有效! 中间件一般会有两个方法,process_request和pr ...
- Django简单设置cookies和session
一.Cookie cookie及特点 Cookie是由服务器(网站)生成的,存储在浏览器端的 键值对数据(通常经过加密) 在响应请求时,服务器会把生成 Cookie数据 发给浏览器,浏览器会自动保存( ...
- MVC5 ModelState
ModelState.IsValid 总是false的原因 在做添加功能的时候,发现这个IsValid总是false,这个是它自己的验证机制. 因为是添加,就是说主键是自增的,添加的时候不需要指定这个 ...
- CSS,HTML页面定制
/*simplememory*/ #google_ad_c1, #google_ad_c2 {display:none;} .syntaxhighlighter a, .syntaxhighlight ...
- kibana- Timelion
1. Visualize 新建图形 2. 选择图形类型 3. 选择索引 4. 设置Timelion表达式 5. 保存图形
- Super Ugly Number -- LeetCode
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- luogu P1417 烹调方案
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...