[USACO17FEB]Why Did the Cow Cross the Road III S
题目描述
Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them.
为什么牛过马路? 其中一个简单的原因就是农民约翰的农场有很多道路,使得他的母牛不得不穿越许多道路。
FJ's farm is arranged as an N×NN \times NN×N square grid of fields ( 2≤N≤1002 \leq N \leq 1002≤N≤100 ), Certain pairs of adjacent fields (e.g., north-south or east-west) are separated by roads, and a tall fence runs around the external perimeter of the entire grid, preventing cows from leaving the farm. Cows can move freely from any field to any other adjacent field (north, east, south, or west), although they prefer not to cross roads unless absolutely necessary.
FJ的农场在 N×NN\times NN×N 的网格中( 2≤N≤1002\le N\le 1002≤N≤100 ),某些相邻的区域(例如,南北或东西)由道路分隔,高大的围栏围绕着整个格栅的外围,防止牛离开农场。 牛可以从任何场地自由移动到任何其他相邻的区域(北,东,南或西),不过除非不得已,她们并不愿意穿越道路。
There are KKK cows ( 1≤K≤100,K≤N21 \leq K \leq 100, K \leq N^21≤K≤100,K≤N2 ) on FJ's farm, each located in a different field. A pair of cows is said to be "distant" if, in order for one cow to visit the other, it is necessary to cross at least one road. Please help FJ count the number of distant pairs of cows.
在FJ的农场有 KKK 头牛( 1≤K≤100,K≤N21\le K\le 100,K\le N^{2}1≤K≤100,K≤N2 ),每个位于不同的区域。 定义一对牛是“遥远的”,是指让一头牛访问另一头牛时,必须至少穿过一条路。 请帮助FJ计算有多少对牛是“遥远的”。
输入输出格式
输入格式:
The first line of input contains NNN , KKK , and RRR . The next RRR lines describe RRR roads that exist between pairs of adjacent fields. Each line is of the form rrr ccc r′r'r′ c′c'c′ (integers in the range 1…N1 \ldots N1…N ), indicating a road between the field in (row rrr , column ccc ) and the adjacent field in (row r′r'r′ , column c′c'c′ ). The final KKK lines indicate the locations of the KKK cows, each specified in terms of a row and column.
第一行输入包含 NNN , KKK 和 RRR 。 接下来的 RRR 行描述存在于相邻区域对之间的 RRR 条路。 每行的格式为 rrr ; ccc ; r′r'r′ ; c′c'c′ (都是在 1...N1...N1...N 中的整数),表示在两个相邻的区域(第 rrr 行第 ccc 列,和第 $r'$ 行第 $c'$ 列)之间的路。 最终的 KKK 行表示 KKK 头牛的位置,也用行列来表示。
输出格式:
Print the number of pairs of cows that are distant.
输出遥远的牛数量对。
输入输出样例
3 3 3
2 2 2 3
3 3 3 2
3 3 2 3
3 3
2 2
2 3
2 无脑爆搜;
首先我想到的是对于每一个牛都bfs一遍;
可想而知T的很惨
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; inline int read(){int res=;bool flag=;char ch=getchar();while(!isdigit(ch)){if(res=='-')flag=;ch=getchar();}while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=getchar();}return flag?-res:res;} int dx[] = {, -, , , }, dy[] = {, , , -, }; int n, k, r; bool can[][][]; int mp[][];
int cowx[], cowy[]; int ans[][];
bool vis[][]; struct date
{
int x;
int y;
};
inline void bfs(int sx, int sy)
{
queue <date> q;
while (!q.empty()) q.pop();
q.push((date){sx, sy}); while (!q.empty())
{
int x = q.front().x, y = q.front().y;
q.pop();
vis[x][y] = ;
for (register int i = ; i <= ; i ++)
{
if (can[x][y][i]) continue;
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > n) continue;
if (vis[tx][ty]) continue;
q.push((date){tx, ty});
}
} } int main()
{
n = read(), k = read(), r = read();
for (register int i = ; i <= r ; i++)
{
int a = read(), b = read(), x = read(), y = read();
if (a == x)
{
if (b == y + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
else
{
if (a == x + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
} for (register int i = ; i <= k ; i ++)
{
int x = read(), y = read();
mp[x][y] = ;
cowx[i] = x, cowy[i] = y;
} for (register int i = ; i <= k ; i ++)
{
memset(vis, , sizeof vis);
bfs(cowx[i], cowy[i]);
for (register int j = ; j <= k ; j ++)
{
if (i == j) continue;
if (vis[cowx[j]][cowy[j]] == )
{
ans[i][j] = ;
}
}
} int res = ;
for (register int i = ; i <= k ; i ++)
{
for (register int j = ; j <= i ; j ++)
{
if (ans[i][j]) res++;
}
}
cout << res << endl;
return ;
}
zZhBr
然后又想我们可以搜出图中所有的联通块, 然后直接暴力相加;great√!
可是现实却是这样
残酷现实;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; inline char nc()
{
static const int BS = << ;
static unsigned char buf[BS],*st,*ed;
if(st == ed) ed = buf + fread(st=buf,,BS,stdin);
return st == ed ? EOF : *st++;
} inline int read(){int res=;bool flag=;char ch=nc();while(!isdigit(ch)){if(res=='-')flag=;ch=nc();}while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=nc();}return flag?-res:res;} int dx[] = {, -, , , }, dy[] = {, , , -, }; int n, k, r; bool can[][][];
int mp[][];
int color[][];
int col;
int cowx[], cowy[]; struct date
{
int x;
int y;
};
inline void bfs(int sx, int sy)
{
queue <date> q;
while (!q.empty()) q.pop();
q.push((date){sx, sy}); while (!q.empty())
{
int x = q.front().x, y = q.front().y;
q.pop();
color[x][y] = col;
for (register int i = ; i <= ; i ++)
{
if (can[x][y][i]) continue;
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > n) continue;
if (color[tx][ty]) continue;
q.push((date){tx, ty});
}
} } int main()
{
n = read(), k = read(), r = read();
for (register int i = ; i <= r ; i++)
{
int a = read(), b = read(), x = read(), y = read();
if (a == x)
{
if (b == y + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
else
{
if (a == x + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
} for (register int i = ; i <= k ; i ++)
{
int x = read(), y = read();
mp[x][y] = ;
cowx[i] = x, cowy[i] = y;
} for (register int i = ; i <= k ; i ++)
{
if (!color[cowx[i]][cowy[i]])
{
col++;
bfs(cowx[i], cowy[i]);
}
}
int res = ;
for (register int i = ; i <= k ; i ++)
{
for (register int j = i + ; j <= k ; j ++)
{
if (color[cowx[i]][cowy[i]] != color[cowx[j]][cowy[j]]) res++;
}
}
printf("%d\n", res);
return ;
}
zZhBr
咳咳!stl队列常数巨大!
所以又手写队列;
WA!
what?
查了一年...
woc
我染色的时候顺序写错了! 真正代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; #define nc getchar
inline int read(){int res=;bool flag=;char ch=nc();while(!isdigit(ch)){if(res=='-')flag=;ch=nc();}while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=nc();}return flag?-res:res;} int dx[] = {, -, , , }, dy[] = {, , , -, }; int n, k, r; bool can[][][];
int color[][];
int col;
int cowx[], cowy[]; struct date
{
int x;
int y;
}q[*];
inline void bfs(int sx, int sy)
{
int l = , r = ;
q[l] = (date){sx, sy};
color[sx][sy] = col; while (l <= r)
{
int x = q[l].x, y = q[l++].y;
for (register int i = ; i <= ; i ++)
{
if (can[x][y][i]) continue;
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > n) continue;
if (color[tx][ty]) continue;
color[tx][ty] = col;
q[++r] = (date){tx, ty};
}
}
} int main()
{
n = read(), k = read(), r = read();
for (register int i = ; i <= r ; i++)
{
int a = read(), b = read(), x = read(), y = read();
if (a == x)
{
if (b == y + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
else
{
if (a == x + )
{
can[a][b][] = ;
can[x][y][] = ;
}
else
{
can[a][b][] = ;
can[x][y][] = ;
}
}
} for (register int i = ; i <= k ; i ++)
{
int x = read(), y = read();
cowx[i] = x, cowy[i] = y;
} for (register int i = ; i <= k ; i ++)
{
if (!color[cowx[i]][cowy[i]])
{
col++;
bfs(cowx[i], cowy[i]);
}
}
int res = ;
for (register int i = ; i <= k ; i ++)
{
for (register int j = i + ; j <= k ; j ++)
{
if (color[cowx[i]][cowy[i]] != color[cowx[j]][cowy[j]]) res++;
}
}
printf("%d\n", res);
return ;
}
颓颓颓了一晚上, 明天上课, 现在作业还没动, 凉凉;
不说了颓作业去;
[USACO17FEB]Why Did the Cow Cross the Road III S的更多相关文章
- 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S
P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...
- 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...
- P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...
- [USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)
题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置, ...
- [USACO17FEB]Why Did the Cow Cross the Road III G
嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...
- [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)
题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...
- P3660 [USACO17FEB]Why Did the Cow Cross the Road III G
Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...
随机推荐
- hadoop snapshot 备份恢复 .
1.允许创建快照 首先,在你想要进行备份的文件夹下面 执行命令,允许该文件夹创建快照 hdfs dfsadmin -allowSnapshot <path> 例如:hdfs dfsadmi ...
- JAVA多线程高并发面试题总结
ReadMe : 括号里的内容为补充或解释说明. 多线程和高并发是毕业后求职大厂面试中必问的知识点,自己之前总是面试前才去找相关的知识点面试题来背背,隔段时间又忘了,没有沉淀下来,于是自己总结了下相关 ...
- Cabloy全栈JS框架微创新之一:不一样的“移动优先 PC适配”
前言 目前流行的前端UI组件库都支持移动设备优先的响应式布局特性.但基于Mobile和PC两个场景的不同用户体验,也往往会实现Mobile和PC两个版本. PC场景下的Web工程,如大量的后台前端管理 ...
- charles 启用/禁用断点
本文参考:charles 启用/禁用断点 1.3. enable/disable breakpoints 和 2.3 breakpoints settings 断点设置是常用的了,没啥好说的了,可以设 ...
- 关于git的认知
Git,官方的解释为一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.是一个开放源码的版本控制软件. 就个人而言,这是一种不同开发者之间的代码交流.合并的途径,进而完成目 ...
- nginx 模块
8.nginx开启目录浏览 提供下载功能 默认情况下,网站返回index指定的主页,但如果该网站不存在主页,则将请求交给autoindex模块 ##### 如果开启autoindex模块,则提供一个下 ...
- Loadrunner录制步骤及说明
做好业务准备,如环境配置.服务启动等 打开Virtual User Generator界面---->New---->选择协议,录制网页时一般选择Web(HTTP/HTML) Start R ...
- Spring boot使用log4j打印日志
先将maven中spring-boot-starter的日志spring-boot-starter-logging去掉 <dependency> <groupId>org.sp ...
- C#连接Mongo报Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1错的解决方案
---恢复内容开始--- 最近做一个基于ABP的.net Core的项目,数据库选了MongoDB,但是返现无法给数据库设置认证,只要设置了账号密码连接就报错 连接串如下: mongodb://roo ...
- OpenGL在ubuntu下的成功配置
sudo apt-get update sudo apt-get install build-essential sudo apt-get install libgl1-mesa-dev sudo a ...