POJ 1719 二分图最大匹配(记录路径)
| Time Limit: 1000MS | Memory Limit: 10000K | |||
| Total Submissions: 4097 | Accepted: 1499 | Special Judge | ||
Description
A volley of c shots is correct if exactly one white square is hit in each column and there is no row without white square being hit. Help the shooter to find a correct volley of hits if such a volley exists.
Example
Consider the following target: 
Volley of hits at white squares in rows 2, 3, 1, 4 in consecutive columns 1, 2, 3, 4 is correct.
Write a program that: verifies whether any correct volley of hits exists and if so, finds one of them.
Input
The first line of each block contains two integers r and c separated by a single space, 2 <= r <= c <= 1000. These are the numbers of rows and columns, respectively. Each of the next c lines in the block contains two integers separated by a single space. The integers in the input line i + 1 in the block, 1 <= i <= c, are labels of rows with white squares in the i-th column.
Output
Sample Input
2
4 4
2 4
3 4
1 3
1 4
5 5
1 5
2 4
3 4
2 4
2 3
Sample Output
2 3 1 4
NO
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 1005
#define inf 999999999 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int from[N];
int to[N];
bool visited[N];
int n, m;
vector<int>ve[N]; int march(int u){
int i, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
to[u]=v;
return ;
}
}
}
return ;
} main()
{
int t, i, j, k, u, v;
cin>>t;
while(t--){
scanf("%d %d",&n,&m);
for(i=;i<=m;i++) ve[i].clear();
for(i=;i<=m;i++){
scanf("%d %d",&u,&v);
ve[i].push_back(u);
ve[i].push_back(v);
}
if(n>m) {
printf("NO\n");continue;
}
memset(from,-,sizeof(from));
int num=;
for(i=;i<=m;i++){
memset(visited,false,sizeof(visited));
march(i);
}
int f=;
for(i=;i<=n;i++){
if(from[i]==-){
f=;break;
}
}
if(!f){
printf("NO\n");continue;
}
for(i=;i<=m;i++){
if(!to[i]){
to[i]=ve[i][];
}
}
printf("%d",to[]);
for(i=;i<=m;i++) printf(" %d",to[i]);
cout<<endl;
}
}
POJ 1719 二分图最大匹配(记录路径)的更多相关文章
- POJ 2226二分图最大匹配
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...
- poj 2239 二分图最大匹配,基础题
1.poj 2239 Selecting Courses 二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
- POJ Evacuation /// 二分图最大匹配
题目大意: 在一个n*m的房间中 ‘X’为墙 ‘D’为门 ‘.’为人 门只存在与外围 人每秒钟只能向四连通区域走一步 门比较狭窄 每秒钟只能通过一个人 求所有人逃脱的最短时间 如果不可能则输出impo ...
- Pots POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- poj 2724 二分图最大匹配
题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...
- Asteroids - poj 3041(二分图最大匹配问题)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17258 Accepted: 9386 Description Be ...
- POJ 1422 二分图(最小路径覆盖)
Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7278 Accepted: 4318 Descript ...
- poj 2446 二分图最大匹配
思路:由(i+j)为偶数的点向(i+j)为奇数的点建边.求一次最大匹配,若正好为空格数(不包含洞)的一半,即输出YES. #include<iostream> #include<cs ...
随机推荐
- poj2451Uyuw's Concert(半平面交)
链接 逆时针给出线段,如果模板是顺时针的修改下系数的符号进行平面交即可. #include <iostream> #include<cstdio> #include<cs ...
- Java存储过程调用CallableStatement
什么是存储过程? 一个存储过程是一个可编程的函数,它在数据库中创建并保存.它可以有SQL语句和一些特殊的控制结构组成.当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有 ...
- 使用bufferevent进行libevent服务端和客户端的开发
参考了网上的一些例子,实验了基于bufferevent的开发. 首先是服务端: #include <netinet/in.h> #include <sys/socket.h> ...
- Css3_浏览器支持
#box{ color:red; ⁄* 所有浏览器都支持 *⁄ color:red !important; ⁄* Firefox.IE7支持 *⁄ _color:re ...
- python操作mongodb之五大量写操作
import pymongo #库名 db = pymongo.MongoClient('192.168.30.252',27017).bulk_example #test集合插入 db.test.i ...
- django在pyhton2.7 和 python3.* 之间代码和睦相处的方法
“祥”龙第一掌: from __future__ import unicode_literals from django.utils.encoding import python_2_unicode_ ...
- Python项目实战
编程只有不断练习才能掌握其精髓,多练练网上的习题和项目,才能掌握python的精髓. Python的模块和包是出了名的多,因此你不必自己从底层开始写起,只需要看懂模块和包的使用文档就可以了,因此掌握一 ...
- WEB网页插件 如何实现 选择上传图片路径 【高级问题】
发表于 2010-10-22 12:11 | |只看楼主 按键精灵程序里面的WEB网页插件 如何实现 选择上传图片路径 我想在上传图片的选框设置图片路径为 C:\fakepath\001. ...
- VR应用里面的Photogrammetry技术是什么
http://www.manew.com/thread-49556-1-1.html 具体使用 http://www.didayin.com/archives/632 软件下载 http://labs ...
- Python 练习 12
#!/usr/bin/python # -*- coding: UTF-8 -*- year = int(raw_input('year:\n')) month = int(raw_input('mo ...