【wikioi】1922 骑士共存问题(网络流/二分图匹配)
用匈牙利tle啊喂?和网络流不都是n^3的吗(匈牙利O(nm), isap O(n^2m) 但是isap实际复杂度很优的(二分图匹配中,dinic是O(sqrt(V)*E),不知道isap是不是一样。。。)。。)。。。。 (更新:what!!!!!!发现个无语的问题,。!!!!结构比数组快啊orz,这节奏不对啊。。。。以后图都写结构的节奏啊。。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define read(a) scanf("%d", &a)
#define print(a) printf("%d", a)
#define num(i, j) ((i-1)*n+j)
#define who(i, j) (i%2==j%2)
#define arr(a, n) for1(i, 1, n) { for1(j, 1, n) print(a[i][j]); printf("\n"); }
#define arr2(a, n) for1(i, 1, n) print(a[i]); printf("\n")
inline int getnum() { int ret=0; char c; int k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return k*ret; } const int fx[8]={1, 1, 2, 2, -1, -1, -2, -2}, fy[8]={-2, 2, -1, 1, -2, 2, -1, 1};
const int N=205*205, M=N*8+100;
int ihead[N], inext[M], to[M], cnt, x[N], cont, ly[N], n, m, mm[205][205];
bool vis[N]; inline void add(const int &u, const int &v) {
inext[++cnt]=ihead[u]; ihead[u]=cnt; to[cnt]=v;
} bool ifind(const int &x) {
vis[x]=true;
for(int i=ihead[x]; i; i=inext[i]) if(!vis[to[i]]) {
vis[to[i]]=true;
if(!ly[to[i]] || ifind(ly[to[i]])) {
ly[to[i]]=x;
return true;
}
}
return false;
} int main() {
read(n); read(m);
int a, b, nx, ny, ans=0;
rep(i, m) read(a), read(b), mm[a][b]=1;
for1(i, 1, n) for1(j, 1, n) if(!mm[i][j] && who(i, j)) {
rep(k, 8) {
nx=i+fx[k], ny=j+fy[k];
if(mm[nx][ny] || nx<1 || nx>n || ny<1 || ny>n) continue;
add(num(i, j), num(nx, ny));
}
x[++cont]=num(i, j);
}
for1(i, 1, cont) {
CC(vis, 0);
if(ifind(x[i])) ans++;
}
print(n*n-ans-m); return 0;
}
囧,,只能码网络流了。
tle了无数次的ac orz(和上面一样,数组和结构的问题。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define read(a) a=getnum()
#define print(a) printf("%d", a)
#define num(i, j) ((i-1)*n+j)
#define who(i, j) (!((i+j)%2))
#define arr(a, n) for1(i, 1, n) { for1(j, 1, n) print(a[i][j]); printf("\n"); }
#define arr2(a, n) for1(i, 1, n) print(a[i]); printf("\n")
inline int getnum() { int ret=0; char c; int k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return k*ret; } const int fx[8]={1, 1, 2, 2, -1, -1, -2, -2}, fy[8]={2, -2, 1, -1, 2, -2, 1, -1};
const int N=205*205, M=500001, oo=~0u>>1;
int ihead[N], cnt=1, n, m;
int gap[N], p[N], d[N], cur[N];
bool mm[205][205];
struct dd { int to, from, cap, next; }e[M]; inline void add(const int &u, const int &v, const int &c) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].from=u; e[cnt].to=v; e[cnt].cap=c;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].from=v; e[cnt].to=u; e[cnt].cap=0;
} int isap(const int &s, const int &t, const int &n) {
int flow=0, u=s, f, i, v;
for1(i, 0, n) cur[i]=ihead[i];
gap[0]=n;
while(d[s]<n) {
for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]==d[v=e[i].to]+1) break;
if(i) {
cur[u]=i; p[v]=i; u=v;
if(u==t) {
for(f=oo; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
flow+=f;
}
}
else {
if(!(--gap[d[u]])) break;
d[u]=n;
cur[u]=ihead[u];
for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].to]+1)
d[u]=d[e[i].to]+1;
++gap[d[u]]; if(u!=s) u=e[p[u]].from;
}
}
return flow;
} int main() {
read(n); read(m);
int a, b, nx, ny, s=0, t=n*n+5;
rep(i, m) read(a), read(b), mm[a][b]=1;
for1(i, 1, n) for1(j, 1, n) if(!mm[i][j]) {
if(who(i, j)) {
rep(k, 8) {
nx=i+fx[k], ny=j+fy[k];
if(mm[nx][ny] || nx<1 || nx>n || ny<1 || ny>n) continue;
add(num(i, j), num(nx, ny), oo);
}
add(s, num(i, j), 1);
}
else add(num(i, j), t, 1);
}
print(n*n-isap(s, t, t+1)-m); return 0;
}
题目描述 Description
在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示。棋盘
上某些方格设置了障碍,骑士不得进入。
对于给定的n*n个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个骑
士,使得它们彼此互不攻击。
输入描述
Input Description
第一行有2 个正整数n 和m (1<=n<=200, 0<=m<n^2),
分别表示棋盘的大小和障碍数。接下来的m 行给出障碍的位置。每行2 个正整数,表示障
碍的方格坐标。
输出描述
Output Description
将计算出的共存骑士数输出
样例输入
Sample Input
3 2
1 1
3 3
样例输出
Sample Output
5
数据范围及提示
Data Size & Hint
详见试题
【wikioi】1922 骑士共存问题(网络流/二分图匹配)的更多相关文章
- codevs 1922 骑士共存问题 网络流
题目链接 给一个n*n的棋盘, 上面有障碍物, 有障碍物的不能放东西.然后往上面放马, 马不能互相攻击, 问最多可以放多少个马. 按x+y的奇偶来划分, 如果两个格子可以互相攻击, 就连一条权值为1的 ...
- [luoguP3355] 骑士共存问题(二分图最大独立集)
传送门 模型 二分图最大独立集,转化为二分图最大匹配,从而用最大流解决. 实现 首先把棋盘黑白染色,使相邻格子颜色不同. 把所有可用的黑色格子看做二分图X集合中顶点,可用的白色格子看做Y集合顶点. 建 ...
- P3355 骑士共存问题 网络流
骑士共存 题目描述 在一个 n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最 ...
- 「CODVES 1922 」骑士共存问题(二分图的最大独立集|网络流)&dinic
首先是题目链接 http://codevs.cn/problem/1922/ 结果发现题目没图(心情复杂 然后去网上扒了一张图 大概就是这样了. 如果把每个点和它可以攻击的点连一条边,那问题就变成了 ...
- LUOGU P3355 骑士共存问题(二分图最大独立集)
传送门 因为骑士只能走"日"字,所以一定是从一个奇点到偶点或偶点到奇点,那么这就是一张二分图,题目要求的其实就是二分图的最大独立集.最大独立集=n-最大匹配. #include&l ...
- 【BZOJ】1934: [Shoi2007]Vote 善意的投票(网络流/-二分图匹配)
http://www.lydsy.com/JudgeOnline/problem.php?id=1934 一开始我想到了这是求最小割,但是我认为这题二分图可做,将1的放在左边,0的放在右边,然后朋友连 ...
- 【CODEVS】1922 骑士共存问题
[算法]二分图最大匹配(最大流) [题解]按(i+j)奇偶性染色后,发现棋子跳到的地方刚好异色. 然后就是二分图了,对于每个奇点向可以跳到的地方连边,偶点不需连(可逆). 所以题目要求转换为求二分图上 ...
- Codevs1922 骑士共存问题
1922 骑士共存问题 题目描述 Description 在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的n*n个方格的国 ...
- Cogs 746. [网络流24题] 骑士共存(最大独立集)
[网络流24题] 骑士共存 ★★☆ 输入文件:knight.in 输出文件:knight.out 简单对比 时间限制:1 s 内存限制:128 MB 骑士共存问题 «问题描述: 在一个n*n个方格的国 ...
随机推荐
- MVC NonAction属性
3.1. NonAction属性 若将NonAction属性应用在Controller中的Action方法上,即使该Action方法是公共方法,也会告知ActionInvoker不要选取这个Actio ...
- Java for LeetCode 075 Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 多个list合并
需要多个list合并,如图 @SuppressWarnings("unchecked") @Override public List<CwInfo> get ...
- HDU 2.1.7 (求定积分公式)
The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Java性能优化权威指南-读书笔记(四)-JVM性能调优-延迟
延迟指服务器处理一个请求所花费的时间,单位一般是ms.s. 本文主要讲降低延迟可以做的服务器端JVM优化. JVM延迟优化 新生代 新生代大小决定了应用平均延迟 如果平均Minor GC持续时间大于应 ...
- Linux vim 底下显示行号
使用 vim /etc/vimrc 然后进入之后再 set ruler" show the cursor position all the time 底下再加上 set nu 保存退出 :w ...
- Linux C 知识 char型数字转换为int型 int型 转换为Char
前言 在九度oj做acm的时候,经常会遇到了char类型和int类型相互转化的问题,这里进行一下总结.今后,可能会多次更新博客,因为半年做了很多总结,但是都是保存在word文档上了,现在开始慢慢向CS ...
- Android之Tab类总结
本文主要包括以下Tab类实现方式 FragmentTabHost+Fragment实现 传统的ViewPager实现 FragmentManager+Fragment实现 ViewPager+Frag ...
- php 简单操作数据库
<?php header("content-type:text/html;charset=utf-8"); /*//造一个连接 $connect = @mysql_conne ...
- svn update -r m path 代码还原到某个版本(这样之前的log日志也就没了,也就是清空log日志)
[root@ok 资料库]# svn log 简历 ------------------------------------------------------------------------ r ...
