欢迎访问~原文出处——博客园-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. DEV Winform分页用户组件

    资源部分在QQ群:616945527基于服务端数据分页,你也可以修改成本地分页.调用方法添加用户控件到窗体 public int curPage = 1;public int pageSize = 1 ...

  2. json 不能 dumps datetime 解决办法

    backend.myviews.json_time.py from datetime import date import json from datetime import datetime cla ...

  3. 线程异步操作,更新其中一个报错不影响另一个的运行(Task )

    //子系统同步更新                BD001_BLL bll = new BD001_BLL();                List<BD001_Model> lis ...

  4. C++11模板友元语法

    第 1 类: 普通类A的 普通类B 友元(一对一友好关系): 无需前置声明class B,当class B第一次出现在friend声明中时,该名字被隐式地认为可见. class A { friend ...

  5. nginx反向代理实现前后端分离&跨域问题

    1.代理和跨域 1.1 正向代理 1)用户希望代理服务器帮助其和要访问服务器之间实现通信,需要: a.用户IP报文的目的IP=代理服务器IP: b.用户报文端口号=代理服务器监听端口号: c.HTTP ...

  6. 解决 ionic 中的 CORS(跨域)

    译者注:本人翻译功力有限,所以文中难免有翻译不准确的地方,凑合看吧,牛逼的话你看英文版的去,完事儿欢迎回来指正交流(^_^) 如果你通过 ionic serve 或者 ionic run 命令使用或 ...

  7. Javascript中Json对象与Json字符串互相转换方法汇总(4种转换方式)

    1.Json对象转Json字符串 JSON.stringify(obj); 2.Json字符串传Json对象 JSON.parse(str);//第一种 $.parseJSON(str);//第二种, ...

  8. iOS 中的Certificate,Provisioning Profile 的一些注意 (不断完善中)

    注册apple id 有1年多了,这些概念还是模模糊糊的,决定在这里总结一下. 请参阅官方文档 App Distribution Guide code singing的作用如下: Code signi ...

  9. OneNET麒麟座应用开发之十:空气质量数据监测站项目总结

    大气质量数据监测站用于测试空气质量监测及数据采集,实现野外或者室内空气质量的检测. 1.项目概述 本项目是一个定制项目,要求采集大气的压力.温度.湿度.PM25.位置等数据并上传到指定的后台服务器.但 ...

  10. 升级 php composer 版本

    在执行 composer update 时,报错 You made a reference to a non-existent script @php artisan package:discover ...