POJ 3041 Asteroids 二分图匹配
以行列为点建图,每个点(x,y) 对应一条边连接x,y。二分图的最小点覆盖=最大匹配
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else freopen("data.in","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} const int maxn = ;
vector<int> g[maxn];
int n, m;
void Init()
{
for(int i=; i<=n; i++)
g[i].clear();
} void add(int u, int v)
{
g[u].push_back(v);
} int match[maxn];
int vis[maxn]; bool dfs(int u)
{
vis[u] = true;
for(int i = ; i < g[u].size(); i++)
{
int v = g[u][i];
int w = match[v];
if(w<||!vis[w]&&dfs(w))
{
match[u] = v;
match[v] = u;
return true;
}
}
return false;
}
int solve()
{
memset(match,-,sizeof(match));
int res = ;
for(int u=; u <= n; u++)
{
if(match[u]<)
{
memset(vis, , sizeof(vis));
if(dfs(u))
res++;
}
}
return res;
}
int main()
{
debug();
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d", &u, &v);
add(u, v+n);
add(v+n, u);
} printf("%d\n",solve());
}
return ;
}
POJ 3041 Asteroids 二分图匹配的更多相关文章
- POJ 3041 Asteroids 二分图
原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 3041 Asteroids (二分图最小点覆盖)
题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...
- poj 3041 Asteroids (二分图的最大匹配 第一题)
题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题
学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...
- POJ 3041 Asteroids(二分图模板题)
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...
- POJ 3041 Asteroids 二分图之最大匹配
题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...
- poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16379 Accepted: 8930 Descri ...
- POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)
POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...
随机推荐
- /proc/net/tcp中各项参数说明
/proc/net/tcp中的内容由tcp4_seq_show()函数打印,该函数中有三种打印形式,我们这里这只列出状态是TCP_SEQ_STATE_LISTENING或TCP_SEQ_STATE_E ...
- oracle number型日期转date型日期
在搞数据库时,发现有这样的一个字段,类型是NUMBER(38),查看了一下里面的数据,都是这样的, 13239576781141321326994295132212930680413221297162 ...
- tomcat虚拟目录映射网络共享目录
<Host name="localhost" debug="0" appBase="webapps" unpackWARs=" ...
- redis集群同步迁移方法(一):通过redis replication实现
讲到redis的迁移,一般会使用rdb或者aof在主库做自动重载到目标库方法.但该方法有个问题就是无法保证源节点数据和目标节点数据保持一致,一般线上环境也不允许源库停机,所以要在迁移过程 ...
- Mysql字符类型比较
一. binary和char比较: binary 字节为单位,char字符为单位,字符占几个字节取决于字符集 binary 比较规则基于字节值,char基于字符,即使是_bin的比较规则 范围都0- ...
- python学习:环境搭建
1.图解eclipse环境下安装python3.x插件支持:http://www.tuicool.com/articles/M3Afyu 其中如果 然后,选择Add按钮,Name:Python3,Lo ...
- Django URL的命令空间
为避免在模板中使用URL的硬编码,可以使用{% url %}模板标签来解决 <li><a href="/task/{{ task.id }}/">{{ ta ...
- java jvm常用命令工具
[尊重原创文章出自:http://www.chepoo.com/java-jvm-command-tools.html] 一.概述 程序运行中经常会遇到各种问题,定位问题时通常需要综合各种信息,如系统 ...
- VS2010添加资源文件
VS2010中资源文件管理在 双击打开Resources.resx 选择左上角资源文件类型 然后复制资源文件 粘贴到空白区域 则会自动生成资源文件代码 在项目中使用该资源文件时,通过global::P ...
- 第三周作业--Word Counter
需求分析: 1.写出一个程序,模仿wc.exe,通过输入文件名,实现文件内容读取: 2.统计出文件内容的总字符数.总单词数.行数.每行字符数.每行单词数. 代码分析: 一.打开文件. FILE *fp ...