UVALive 6525
二分图最大匹配
#include<cstdio>
#include<iostream>
#include<cstring>
#define MAX 10010 using namespace std ; int n, t, n1, n2, g1[1100][1100], g2[1100][1100];
char s[110][110]; bool visit[MAX];
int match[MAX];
int head[MAX]; struct edge
{
int to,next;
}e[300005]; void addedge(int u,int v)
{
e[++t].to=v;
e[t].next=head[u];
head[u]=t;
} void init ( )
{
t = n1 = n2 = 0;
memset(head, 0, sizeof(head));
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(s[i][j] == '.') g1[i][j] = (s[i][j] != s[i][j-1]) ? ++ n1 : g1[i][j-1];
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(s[i][j] == '.') g2[i][j] = (s[i][j] != s[i-1][j]) ? ++ n2: g2[i-1][j];
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(s[i][j] == '.') addedge(g1[i][j], g2[i][j]);
}
bool dfs(int u)
{
int i,v;
for(i = head[u]; i != 0; i = e[i].next)
{
v = e[i].to;
if(!visit[v])
{
visit[v] = true;
if(match[v] == -1 || dfs(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int i,sum=0;
memset(match,-1,sizeof(match));
for(i = 1 ; i <= n1 ; ++i)
{
memset(visit,false,sizeof(visit));
if( dfs(i) )
{
sum++;
}
}
return sum;
}
int main ( )
{
while(scanf("%d", &n) == 1)
{
for(int i = 1; i <= n; ++ i)
scanf("%s", s[i]+1);
init();
int ans = MaxMatch();
printf("%d\n", ans);
}
return 0 ;
}
UVALive 6525的更多相关文章
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- UVaLive 6525 Attacking rooks (二分图最大匹配)
题意:给定一个 n * n的图,X是卒, . 是空位置,让你放尽量多的车,使得他们不互相攻击. 析:把每行连续的 . 看成X集体的一个点,同理也是这样,然后求一个最大匹配即可. 代码如下: #prag ...
- The 2013 South America/Brazil Regional Contest 题解
A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...
- BUPT2017 wintertraining(15) #3 题解
我觉得好多套路我都不会ヘ(;´Д`ヘ) 题解拖到情人节后一天才完成,还有三场没补完,真想打死自己.( ˙-˙ ) A - 温泉旅店 UESTC - 878 题意 有n张牌,两人都可以从中拿出任意 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- Ubuntu上OpenStack DashBoard主题修改的方案
最近终于把OpenStack的整个初步环境安装部署并配置成功,后续工作就是安装一些可选项了,如Cinder.Swift部件了.我们的整个项目采用的颜色方案主要为清淡色系,蓝色为主,可OpenStack ...
- 20141128—JavaScript对象
JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... String 对象的 length 属性来获得字符串的长度: var message="Hello World!& ...
- linux工程管理工具make入门
一.make工具的功能 1.主要负责一个软件工程中多个源代码的自动编译工作 2.还能进行环境检测.后期处理等工作: 3.make工具可以识别出工程中哪些文件已经被修改,并且在再次编译的时候只编译这些文 ...
- java检测端口号是否配占用
java检测端口号是否被占用的工具类: package com.frank.util; import java.io.IOException; import java.net.InetAd ...
- gcc与g++的区别
一:gcc与g++比较 编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而 c++代码用g++,或者说编译用gcc,链接用g++,一时也不知哪个说法正确, ...
- C#打开mdb文件,获取文件下的所有表格,以及获取表格下的所有字段
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aspxWeb2 ...
- VIM小技巧之文件名补全
恩,这两天在看<简明Python教程>,那里面作者建议写代码的时候前面的注释写上文件名,写上调用的解释器,比如这样: 恩,然后我当然不可能每回新建一个文件,就要在开头写上一大串东西啊,vi ...
- WP开发笔记——程序的退出方法
Windows Phone程序中,并没有之前的类似于“App.Exit()”之类的函数用来让你退出程序.这是怎么回事儿呢? 很简单,在Windows Phone 7中系统要求配备了硬件的“Back”键 ...
- MySql安装时在Start Service处失败
安装MySql时在最后Start Service时失败: 首先先卸载掉MySql. 查看MySql服务有没有启动,若启动,则先停止MySql服务. 打开注册表,到HKEY_LOCAL_MACHIN ...
- WPF 绑定四(层级绑定)
xaml: <Window x:Class="WpfApplication1.Window4" xmlns="http://schemas.microsoft.co ...