[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&Hbase 双机热备--Pacemaker&DRBD部署
相关文章 DRBD的介绍请参考http://blog.csdn.net/rzhzhz/article/details/7103772 DRBD的部署请参考http://blog.csdn.ne ...
- springboot---redis缓存的使用
1.下载redis安装包,解压到电脑 2.启动redis 3.springboot application.properties中配置redis缓存 spring.redis.host=127.0. ...
- Linux 笔记 - 第十三章 Linux 系统日常管理之(一)系统状态监控
博客地址:http://www.moonxy.com 一.前言 如果你是一名 Linux 运维人员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.系统运行状态主要包括:系统负载.内存状态 ...
- .netCore+Vue 搭建的简捷开发框架 (3)-- Services层实现
继续交作业: 上一篇作业中我们实现了 Repository仓储层的应用.并为我们的框架引入了EFCore 详见: .netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使 ...
- ES6中的迭代器、Generator函数以及Generator函数的异步操作
最近在写RN相关的东西,其中涉及到了redux-saga ,saga的实现原理就是ES6中的Generator函数,而Generator函数又和迭代器有着密不可分的关系.所以本篇博客先学习总结了ite ...
- linux iconv 转换文件编码
查看文件编码file -i filename 递归转换(包括子文件夹)find default -type d -exec mkdir -p utf/{} \;find default -type f ...
- 【深入学习MySQL】MySQL的索引为什么使用B+树?
前言 在MySQL中,无论是Innodb还是MyIsam,都使用了B+树作索引结构(这里不考虑hash等其他索引).本文将从最普通的二叉查找树开始,逐步说明各种树解决的问题以及面临的新问题,从而说明M ...
- 制定一个学习liunx的目标
制定一个学习liunx的目标 学习目标方法 1.在这五个月的学习时间里,制定一套自己的学习方式. 2.养成做笔记以及写博客的习惯 . 3.坚持上课前预习,自习时间总结 . 4.紧跟 ...
- Python学习笔记整理总结【Django】:中间件、CSRF、缓存
一.中间件 中间件是一类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法:在django项目的settings模块中,有一个 MIDDLEWARE 变量,其中每 ...
- Build step 'Invoke top-level Maven targets' marked build as failure Finished解决
最近用法 jenkins部署maven项目时候,突然出现Build step 'Invoke top-level Maven targets' marked build as failure Fini ...