欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ4997


题意概括

  在n*n的区域里,每一个1*1的块都是一个格子。

  有k头牛在里面。

  有r个篱笆把格子分开。

  如果两头牛可以不经过篱笆走到一起(过程中不能出界),那么他们就是不互相远离的,反之就是互相远离的。

  问有多少对牛是互相远离的。注意(x,y)和(y,x)算作同样的。


题解

  对于同一区域的牛,我们可以相同对待。

  所以我们dfs给各自连通的区域分开来,分别统计每一块的牛数,然后乘法原理+加法原理就可以了。


代码

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=100+5;
const int dx[4]={ 0, 0,-1, 1};
const int dy[4]={-1, 1, 0, 0};
int n,m,k,r,v[N][N],cnt,tot[N*N];
bool f[N][N][4];
int find_d(int x,int y){
for (int i=0;i<4;i++)
if (x==dx[i]&&y==dy[i])
return i;
return -1;
}
void dfs(int x,int y){
if (v[x][y])
return;
v[x][y]=cnt;
for (int i=0;i<4;i++)
if (!f[x][y][i])
dfs(x+dx[i],y+dy[i]);
}
int main(){
scanf("%d%d%d",&n,&k,&r);
memset(f,0,sizeof f);
for (int i=1;i<=n;i++){
f[i][1][0]=1;
f[i][n][1]=1;
f[1][i][2]=1;
f[n][i][3]=1;
}
cnt=0;
for (int i=1;i<=r;i++){
int x_1,y_1,x_2,y_2,x,y;
scanf("%d%d%d%d",&x_1,&y_1,&x_2,&y_2);
x=x_2-x_1,y=y_2-y_1;
f[x_1][y_1][find_d(x,y)]=1;
f[x_2][y_2][find_d(-x,-y)]=1;
}
cnt=0;
memset(v,0,sizeof v);
memset(tot,0,sizeof tot);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (!v[i][j]){
cnt++;
dfs(i,j);
}
for (int i=1,x,y;i<=k;i++){
scanf("%d%d",&x,&y);
tot[v[x][y]]++;
}
m=cnt;
int sum=0,ans=0;
for (int i=1;i<=m;i++)
sum+=tot[i];
for (int i=1;i<=m;i++){
sum-=tot[i];
ans+=tot[i]*sum;
}
printf("%d",ans);
return 0;
}

  

BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III的更多相关文章

  1. BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...

  2. 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...

  3. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  4. [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)

    Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...

  5. bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)

    题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...

  6. bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序

    Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...

  7. 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp

    题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...

  8. 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...

  9. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

随机推荐

  1. fcn16s

  2. caffe 中 python 数据层

    caffe中大多数层用C++写成. 但是对于自己数据的输入要写对应的输入层,比如你要去图像中的一部分,不能用LMDB,或者你的label 需要特殊的标记. 这时候就需要用python 写一个输入层. ...

  3. adb查看安卓设备系统Android版本

    adb shell getprop "ro.build.version" 结果中如下两项便是版本信息: [ro.build.version.release]: [4.4.4][ro ...

  4. URLSession

    URLSession时ios7中的心得网络接口,与NSURLConnection是并列的. 当程序在前台时,URLSession与NSURLConnection大部分可以互相替代. URLSessio ...

  5. oaracel 函数_行转列

    wm_concat(varchar2) 组函数

  6. Docker相关

    1.理念 通过对应用组件的封装.分发.部署.运行等生命周期的管理,使用户的App(可以是一个Web应用或数据库应用等)及其运行环境能够做到“一次封装,处处运行”. 2.一句话总结 解决运行环境和配置问 ...

  7. sublime3添加python编译系统

    好记性不如烂笔头 为sublime3添加python编译系统,这里使用的anonconda2中的python.exe(即python2.7版本) 步骤: (1)打开sublime,打开“工具-> ...

  8. 通过全备+binlog_server同步恢复被drop的库或表

    MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步 :伪master+Binlog+同步(本文 ...

  9. centos6.5环境下zookeeper-3.4.6集群环境部署及单机部署详解

    centos6.5环境下Zookeeper-3.4.6集群环境部署 [系统]Centos 6.5 集群部署 [软件]准备好jdk环境,此次我们的环境是open_jdk1.8.0_101 zookeep ...

  10. zabbix系列(三)zabbix3.0.4微信告警配置详解

    一.准备工作 申请微信公众号,并且是可以有发送消息的接口.添加有个脚本去调用微信的api. 之后可以参考下zabbix 的搭建,然后了解下脚本报警,之后再考虑报警方式的多样化. 个人微信一个 个人邮箱 ...